Swing Components

Team-Fly

Following are the current key components of Swing:

  • JApplet. This component is an extended version of the applet that supports multiple panes (just as JFrame does) and also supports a Swing menu bar.

  • JButton. This is a lightweight, pluggable version of the familiar AWT button. Note that all JFC components are subclasses of JComponent. JButton supports many variations of a button (for example, an image button or variable placement of text). All JFC components, even those similar to AWT components, are generally flexible. The model for this component is the ButtonModel, which is also the model for JButton, JCheckbox, and JRadioButton.

  • JCheckbox, JCheckboxMenuItem. This component is a lightweight, pluggable version of Checkbox and Checkbox Menu Item.

  • JColorChooser. This is a pane or dialog of controls that allow a user to graphically select a color.

  • JComboBox. A combo box is a drop-down list of choices. This class has a custom combo box, an editable combo box, and a classic combo box. The model for this component is the ComboBoxDataModel.

  • JComponent. All JFC components subclass (or inherit) the JComponent that extends java.awt.Container.

  • JDesktopPane. This is a container that allows you to create a virtual desktop by adding JInternalFrames. The result is the familiar MDI (Multiple Document Interface) common in Windows applications.

  • JDialog. This component is a class to create a dialog that allows multiple panes, just as JFrame does.

  • JEditorPane. This is a content pane that allows you to edit different types of content, such as HTML (Hypertext Markup Language), RTF (Rich Text Format), or even the Java language, by use of an installable EditorKit class. This set of classes allows you to create powerful editors, and is well worth exploring.

  • JFileChooser. This component is a pure Java version of a dialog to select and choose a file.

  • JFrame. This is a lightweight, pluggable version of the AWT frame. The frame has a single child, JRootPane.

  • JInternalFrame. This component allows frames within frames. This convention enables Java applications to create Windows MDI-type applications. You create and add these frames to the JDesktopPane.

  • JLabel. This is a lightweight, pluggable version of the AWT label. Like other JFC components, JLabel is flexible, allowing various placements of the text and even images. JLabel has no model.

  • JList. This component is a lightweight, pluggable version of the AWT list. JList has two models: ListDataModel and ListSelectionModel. JListBox is designed to make it easy to view the common collection types (for example, arrays, vectors, and hash tables).

  • JMenu, JMenuBar, JMenuItem. These are lightweight, pluggable versions of the menu and menu items. The JFC Menu Items allow images on a menu and menu item.

  • JPanel. This component is a lightweight, pluggable version of the AWT panel.

  • JPasswordField. This is a subclass of JTextField that allows you to enter a word without revealing what is typed. Instead of showing the characters typed, the field displays an echo character, such as an asterisk.

  • JPopupMenu. This menu can pop up anywhere on the screen. The best example is the right-click contextual pop-up menu in Windows, which was also recently added to Mac OS 8 via a Ctrl-click.

  • JProgressBar. This is a new component that presents a graphical view of an action's progress. This element uses the BoundedRangeModel, the same model used by JSlider and JScrollBar.

  • JRadioButton. This subclass of JToggleButton can be selected or de-_selected and maintains (and displays) its state. It is normally used with a ButtonGroup so that only one button of a set of buttons is selected at any time.

  • JRootPane. This is the fundamental component of the Swing frame, window, and pane classes. The parent containers delegate their operations to an instance of JRootPane, which has a glass pane and a layered pane. (A glass pane is a component that is laid out to fill the entire pane. By default, it is an instance of JPanel, but it can be replaced with any Component.) The glass pane sits on top of everything so that it can intercept mouse events. The layered pane manages the content pane and an optional JMenuBar. The three primary components of JRootPane are glass panes, content panes, and an optional menu bar. JRootPane is used by JFrame, JWindow, JApplet, JDialog, and JInternalFrame. Except for JInternalFrame, these elements are heavyweight components that extend their AWT counterparts.

  • JScrollBar, JScrollPane, JViewPort. JScrollBar is a lightweight, pluggable version of the AWT scroll bar. It is intended to be a replacement for the AWT scroll bars. The model for this component is the BoundedRangeModel. JViewPort displays a view that is potentially larger than the viewport. JScrollPane combines JScrollBars with a JViewPort and maps them accordingly.

  • JSeparator. This component is a lightweight, pluggable menu separator.

  • JSlider. This element is a control that allows the user to select a value from a bounded range by dragging a selector knob. The operation is similar to that of a scroll bar. This component uses the same model as the ScrollBar: the BoundedRangeModel.

  • JSplitPane. This component is used to visually divide two other components. Many applications use this element to separate a hierarchical view (using a JTree) from a second view that represents a view of one of the JTree nodes.

  • JTabbedPane. A tabbed pane displays a set of labels that resemble file folders, and enables the user to store any component under that tab. When the user selects the tab, the component comes to the front. The model for this component is the SingleSelectionModel.

  • JTable. This component resides in its own package, called table, and has numerous support classes and interfaces. A table is a graphical element to display relational data in a two-dimensional format. This format is perfect for the display of a database table.

  • JTextComponent, JTextArea, JTextField, JTextPane. These components have an associated package called text, and numerous support classes and interfaces. The JTextComponent goes far beyond the AWT TextComponent by providing the building blocks for a fully internationalized text editor. JTextComponent works with the help of several other classes, such as Document interface, DocumentListener, and DocumentEvent, as well as various view classes and interfaces.

  • JToggleButton. This is an implementation of the two-state button and uses a ToggleButtonModel. JRadioButton and JCheckBox extend this class.

  • JToolBar. This graphics component displays icons for commonly used actions. The Action class enables the toolbar and menu bar to share common action classes.

  • JTree. This class displays hierarchical data in an outline format. The most common example of a tree is the tree used in Windows Explorer to navigate Microsoft Windows file systems. JTree supports a more generic model that includes this type of view. The support classes are the TreeModel, the ListSelectionModel, and the TreeNode classes. The

  • TreeNode class is a simple data structure for storing hierarchical data. A JTree component will provide a default view for a TreeNode structure.

  • JWindow. This is a lightweight version of a window that has a JRootPane as a single child.

Using JButton, JTextArea, and JScrollPane

The following example demonstrates the use of JButton, JTextArea, and JScrollPane:

import java.awt.*; import java.awt.event.*; import javax.swing.*; class TextAreaFrame extends JFrame    implements ActionListener {       public TextAreaFrame()     {         JPanel p = new JPanel();         insertButton = new JButton("Insert");         p.add(insertButton);         insertButton.addActionListener(this);         wrapButton = new JButton("Wrap");         p.add(wrapButton);         wrapButton.addActionListener(this);         noWrapButton = new JButton("No wrap");         p.add(noWrapButton);         noWrapButton.addActionListener(this);         getContentPane().add(p, "South");         textArea = new JTextArea(8, 40);         scrollPane = new JScrollPane(textArea);        getContentPane().add(scrollPane, "Center");         setTitle("TextAreaTest");         setSize(300, 300);         addWindowListener(new WindowAdapter()         {             public void windowClosing(WindowEvent e)             {                 System.exit(0);             }         } );    }    public void actionPerformed(ActionEvent evt)    {         Object source = evt.getSource();         if (source == insertButton)             textArea.append                 ("The quick brown fox jumps over the lazy dog. ");         else if (source == wrapButton)         {             textArea.setLineWrap(true);             scrollPane.validate();         }         else if (source == noWrapButton)         {             textArea.setLineWrap(false);             scrollPane.validate();         }    }    private JButton insertButton;    private JButton wrapButton;       private JButton noWrapButton;    private JTextArea textArea;    private JScrollPane scrollPane; } public class TextAreaTest {    public static void main(String[] args)    {         JFrame f = new TextAreaFrame();         f.show();    } }

Compile TextAreaTest and TextAreaFrame and run the class in MS-DOS by typing Javac TextAreaTest. The output is shown in Figure 13.2.

Figure 13.2: The results of running TextAreaTest in MS-DOS

Using JTextArea, JScrollPane, and JTextField

This example demonstrates the use of JTextArea, JScrollPane, and JTextField:

import java.awt.*; import java.awt.event.*; import javax.swing.*; class TextEditFrame extends JFrame {     public TextEditFrame()     {        setTitle("TextEditTest");         setSize(300, 300);         addWindowListener(new WindowAdapter()             {                   public void windowClosing(WindowEvent e)                 {                     System.exit(0);                 }              }         );       Container contentPane = getContentPane();       JPanel panel = new JPanel();       JButton replaceButton = new JButton("Replace");       panel.add(replaceButton);       replaceButton.addActionListener(new ActionListener()          {  public void actionPerformed(ActionEvent evt)             {                 String f = from.getText();                 int n = textArea.getText().indexOf(f);                 if (n >= 0 && f.length() > 0)                   textArea.replaceRange(to.getText(), n,                       n + f.length());             }          });       from = new JTextField(8);       panel.add(from);       panel.add(new JLabel("with"));       to = new JTextField(8);       panel.add(to);       textArea = new JTextArea(8, 40);       scrollPane = new JScrollPane(textArea);       contentPane.add(panel, "South");       contentPane.add(scrollPane, "Center");    }    private JScrollPane scrollPane;    private JTextArea textArea;    private JTextField from, to; } public class TextEditTest  {    public static void main(String[] args)    {         JFrame f = new TextEditFrame();         f.show();    } }

Compile TextEditFrame and TextEditTest and run the class in MS-DOS by typing Javac TextEditTest. The output is shown in Figure 13.3.

Figure 13.3: The results of running TextEditTest in MS-DOS


Team-Fly


Java & BAPI Technology for SAP
Java & BAPI Technology for SAP
ISBN: 761523057
EAN: N/A
Year: 1998
Pages: 199

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net