31.10. TreeNode, MutableTreeNode, and DefaultMutableTreeNode

 
[Page 949 ( continued )]

28.7. JSplitPane

JSplitPane is a convenient Swing container that contains two components with a separate bar known as a divider , as shown in Figure 28.22.

Figure 28.22. JSplitPane divides a container into two parts .

The bar can divide the container horizontally or vertically, and can be dragged to change the amount of space occupied by each component. Figure 28.23 shows the frequently used properties, constructors, and methods in JSplitPane .

Figure 28.23. JSplitPane provides methods to specify the properties of a split pane and for manipulating the components in a split pane.
(This item is displayed on page 951 in the print version)

Listing 28.11 gives an example that uses radio buttons to let the user select a FlowLayout , GridLayout , or BoxLayout manager dynamically for a panel. The panel contains four buttons, as shown in Figure 28.24. The description of the currently selected layout manager is displayed in a text area. The radio buttons, buttons, and text area are placed in two split panes.

Figure 28.24. The split pane lets you adjust the component size in the split panes.
(This item is displayed on page 952 in the print version)

Listing 28.11. ShowLayout.java
(This item is displayed on pages 949 - 951 in the print version)
 1   import   java.awt.*; 2   import   java.awt.event.*; 3   import   java.net.*; 4   import   javax.swing.*; 5 6   public class   ShowLayout   extends   JApplet { 7  // Get the url for HTML files  8   private   String flowLayoutDesc =   "FlowLayout arranges components "   + 9   "according to their preferredSize in "   + 10   "a left-to-right flow, much like lines of text in a paragraph."   ; 11   private   String gridLayoutDesc =   "GridLayout arranges ..."   ; 

[Page 950]
 12   private   String boxLayoutDesc =   "BoxLayout arranges ..."   ; 13 14   private   JRadioButton jrbFlowLayout = 15   new   JRadioButton(   "FlowLayout"   ); 16   private   JRadioButton jrbGridLayout = 17   new   JRadioButton(   "GridLayout"   ,   true   ); 18   private   JRadioButton jrbBoxLayout = 19   new   JRadioButton(   "BoxLayout"   ); 20 21   private   JPanel jpComponents =   new   JPanel(); 22   private   JTextArea jtfDescription =   new   JTextArea(); 23 24  // Create layout managers  25   private   FlowLayout flowLayout =   new   FlowLayout(); 26   private   GridLayout gridLayout =   new   GridLayout(   2   ,   2   ,   3   ,   3   ); 27   private   BoxLayout boxLayout = 28   new   BoxLayout(jpComponents, BoxLayout.X_AXIS); 29 30   public   ShowLayout() { 31  // Create a box to hold radio buttons  32 Box jpChooseLayout = Box.createVerticalBox(); 33 jpChooseLayout.add(jrbFlowLayout); 34 jpChooseLayout.add(jrbGridLayout); 35 jpChooseLayout.add(jrbBoxLayout); 36 37  // Group radio buttons  38 ButtonGroup btg =   new   ButtonGroup(); 39 btg.add(jrbFlowLayout); 40 btg.add(jrbGridLayout); 41 btg.add(jrbBoxLayout); 42 43  // Wrap lines and words  44 jtfDescription.setLineWrap(   true   ); 45 jtfDescription.setWrapStyleWord(   true   ); 46 47  // Add fours buttons to jpComponents  48 jpComponents.add(   new   JButton(   "Button 1"   )); 49 jpComponents.add(   new   JButton(   "Button 2"   )); 50 jpComponents.add(   new   JButton(   "Button 3"   )); 51 jpComponents.add(   new   JButton(   "Button 4"   )); 52 53  // Create two split panes to hold jpChooseLayout, jpComponents,  54  // and jtfDescription  55  JSplitPane jSplitPane2 =   new   JSplitPane(  56  JSplitPane.VERTICAL_SPLIT, jpComponents,  57    new   JScrollPane(jtfDescription));  58  JSplitPane jSplitPane1 =   new   JSplitPane(  59  JSplitPane.HORIZONTAL_SPLIT, jpChooseLayout, jSplitPane2);  60 61  // Set FlowLayout as default  62 jpComponents.setLayout(flowLayout); 63 jpComponents.validate(); 64 jtfDescription.setText(flowLayoutDesc); 65 66 add(  jSplitPane1  , BorderLayout.CENTER); 67 68  // Register listeners  69 jrbFlowLayout.addActionListener(   new   ActionListener() { 70   public void   actionPerformed(ActionEvent e) { 71 jpComponents.setLayout(flowLayout); 

[Page 951]
 72 jtfDescription.setText(flowLayoutDesc); 73 jpComponents.revalidate(); 74 } 75 }); 76 jrbGridLayout.addActionListener(   new   ActionListener() { 77   public void   actionPerformed(ActionEvent e) { 78 jpComponents.setLayout(gridLayout); 79 jtfDescription.setText(gridLayoutDesc); 80 jpComponents.revalidate(); 81 } 82 }); 83 jrbBoxLayout.addActionListener(   new   ActionListener() { 84   public void   actionPerformed(ActionEvent e) { 85 jpComponents.setLayout(boxLayout); 86 jtfDescription.setText(boxLayoutDesc); 87 jpComponents.revalidate(); 88 } 89 }); 90 } 91 } 


[Page 952]

Split panes can be embedded. Adding a split pane to an existing split results in three split panes. The program creates two split panes (lines 55 “59) to hold a panel for radio buttons, a panel for buttons, and a scroll pane.

The radio buttons are used to select layout managers. A selected layout manager is used in the panel for laying out the buttons (lines 69 “89). The scroll pane contains a JTextField for displaying the text that describes the selected layout manager (line 57).

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

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