31.11. TreePath and TreeSelectionModel

 
[Page 952 ( continued )]

28.8. Swing Borders

Swing provides a variety of borders that you can use to decorate components. You learned how to create titled borders and line borders in §12.9, "Common Features of Swing GUI Components ." This section introduces borders in more detail.

A Swing border is defined in the Border interface. Every instance of JComponent can set a border through the border property defined in JComponent . If a border is present, it replaces the inset. The AbstractBorder class implements an empty border with no size . This provides a convenient base class from which other border classes can easily be derived. There are eight concrete border classes, BevelBorder , SoftBevelBorder , CompoundBorder , EmptyBorder , EtchedBorder , LineBorder , MatteBorder , and TitledBorder , as shown in Figure 28.25.

Figure 28.25. Every instance of JComponent can have a border derived from the Border interface.


  • [Page 953]
  • BevelBorder is a 3D-look border that can be lowered or raised. BevelBorder has the following constructors, which create a BevelBorder with the specified bevelType ( BevelBorder.LOWERED or BevelBorder.RAISED ) and colors:

     BevelBorder(   int   bevelType) BevelBorder(   int   bevelType, Color highlight, Color shadow) BevelBorder(   int   bevelType, Color highlightOuterColor, Color highlightInnerColor, Color shadowOuterColor, Color shadowInnerColor) 
  • SoftBevelBorder is a raised or lowered bevel with softened corners. SoftBevelBorder has the following constructors:

     SoftBevelBorder(   int   bevelType) SoftBevelBorder(   int   bevelType, Color highlight, Color shadow) SoftBevelBorder(   int   bevelType, Color highlightOuterColor, Color highlightInnerColor, Color shadowOuterColor, Color shadowInnerColor) 
  • EmptyBorder is a border with border space but no drawings. EmptyBorder has the following constructors:

     EmptyBorder( Insets borderInsets) EmptyBorder(   int   top,   int   left,   int   bottom,   int   right) 
  • EtchedBorder is an etched border that can be etched-in or etched-out. EtchedBorder has the property etchType with the value LOWERED or RAISED . EtchedBorder has the following constructors:

     EtchedBorder()  // default constructor with a lowered border  EtchedBorder(Color highlight, Color shadow) EtchedBorder(   int   etchType) EtchedBorder(   int   etchType, Color highlight, Color shadow) 
  • LineBorder draws a line of arbitrary thickness and a single color around the border. LineBorder has the following constructors:

     LineBorder(Color color)  // Thickness 1  LineBorder(Color color, int thickness) LineBorder(Color color, int thickness, boolean roundedCorners) 
  • MatteBorder is a matte-like border padded with the icon images. MatteBorder has the following constructors:

     MatteBorder(Icon tileIcon) MatteBorder(Insets borderInsets, Color matteColor) MatteBorder(Insets borderInsets, Icon tileIcon) MatteBorder(   int   top,   int   left,   int   bottom,   int   right, Color matteColor) MatteBorder(   int   top,   int   left,   int   bottom,   int   right, Icon tileIcon) 
  • CompoundBorder is used to compose two Border objects into a single border by nesting an inside Border object within the insets of an outside Border object using the following constructor:

     CompoundBorder(Border outsideBorder, Border insideBorder) 

    [Page 954]
  • TitledBorder is a border with a string title in a specified position. Titled border can be composed with other borders. TitledBorder has the following constructors:

     TitledBorder(String title) TitledBorder(Border border)  // Empty title on another border  TitledBorder(Border border, String title) TitledBorder(Border border, String title,   int   titleJustification,   int   titlePosition) TitledBorder(Border border, String title,   int   titleJustification,   int   titlePosition, Font titleFont) TitledBorder(Border border, String title,   int   titleJustification,   int   titlePosition, Font titleFont, Color titleColor) 

For convenience, Java also provides the javax.swing.BorderFactory class, which contains the static methods for creating borders shown in Figure 28.26.

Figure 28.26. BorderFactory contains the static methods for creating various types of borders.

For example, to create an etched border, use the following statement:

 Border border = BorderFactory.createEtchedBorder(); 

Note

All the border classes and interfaces are grouped in the package javax.swing.border except javax.swing.BorderFactory .



[Page 955]

Note

Borders and icons can be shared. Thus you can create a border or icon and use it to set the border or icon property for any GUI component. For example, the following statements set a border b for two panels p1 and p2 :

 p1.setBorder(b); p2.setBorder(b); 


Listing 28.12 gives an example that creates and displays various types of borders. You can select a border with a title or without a title. For a border without a title, you can choose a border style from Lowered Bevel, Raised Bevel, Etched, Line, Matte, or Empty. For a border with a title, you can specify the title position and justification. You can also embed another border into a titled border. Figure 28.27 displays a sample run of the program.

Figure 28.27. The program demonstrates various types of borders.


Here are the major steps in the program:

  1. Create the user interface.

    1. Create a JLabel object and place it in the center of the frame.

    2. Create a panel named jpPosition to group the radio buttons for selecting the border title position. Set the border of this panel in the titled border with the title "Position".

    3. Create a panel named jpJustification to group the radio buttons for selecting the border title justification. Set the border of this panel in the titled border with the title "Justification".

    4. Create a panel named jpTitleOptions to hold the jpPosition panel and the jpJustification panel.

    5. Create a panel named jpTitle to hold a check box named "Titled" and the jpTitleOptions panel.

    6. Create a panel named jpBorderStyle to group the radio buttons for selecting border styles.

    7. Create a panel named jpAllChoices to hold the panels jpTitle and jpBorderStyle . Place jpAllChoices in the south of the frame.

  2. Process the event.

    Create and register listeners to implement the actionPerformed handler to set the border for the label according to the events from the check box, and from all the radio buttons.


    [Page 956]
Listing 28.12. BorderDemo.java
(This item is displayed on pages 956 - 959 in the print version)
 1   import   java.awt.*; 2   import   java.awt.event.ActionListener; 3   import   java.awt.event.ActionEvent; 4   import   javax.swing.*; 5   import   javax.swing.border.*; 6 7   public class   BorderDemo   extends   JApplet { 8  // Declare a label for displaying message  9   private   JLabel jLabel1 =   new   JLabel(   "Display the border type"   , 10 JLabel.CENTER); 11 12  // A check box for selecting a border with or without a title  13   private   JCheckBox jchkTitled; 14 15  // Radio buttons for border styles  16   private   JRadioButton jrbLoweredBevel, jrbRaisedBevel, 17 jrbEtched, jrbLine, jrbMatte, jrbEmpty; 18 19  // Radio buttons for titled border options  20   private   JRadioButton jrbAboveBottom, jrbBottom, 21 jrbBelowBottom, jrbAboveTop, jrbTop, jrbBelowTop, 22 jrbLeft, jrbCenter, jrbRight; 23 24  // TitledBorder for the label  25   private   TitledBorder jLabel1Border; 26 27  /** Constructor */  28   public   BorderDemo() { 29  // Create a JLabel instance and set colors  30 jLabel1.setBackground(Color.yellow); 31 jLabel1.setBorder(jLabel1Border); 32 33  // Place title position radio buttons  34 JPanel jpPosition =   new   JPanel(); 35 jpPosition.setLayout(   new   GridLayout(   3   ,   2   )); 36 jpPosition.add( 37 jrbAboveBottom =   new   JRadioButton(   "ABOVE_BOTTOM"   )); 38 jpPosition.add(jrbAboveTop =   new   JRadioButton(   "ABOVE_TOP"   )); 39 jpPosition.add(jrbBottom =   new   JRadioButton(   "BOTTOM"   )); 40 jpPosition.add(jrbTop =   new   JRadioButton(   "TOP"   )); 41 jpPosition.add( 42 jrbBelowBottom =   new   JRadioButton(   "BELOW_BOTTOM"   )); 43 jpPosition.add(jrbBelowTop =   new   JRadioButton(   "BELOW_TOP"   )); 44 jpPosition.setBorder(   new   TitledBorder(   "Position"   )); 45 46  // Place title justification radio buttons  47 JPanel jpJustification =   new   JPanel(); 48 jpJustification.setLayout(   new   GridLayout(   3   ,   1   )); 49 jpJustification.add(jrbLeft =   new   JRadioButton(   "LEFT"   )); 50 jpJustification.add(jrbCenter =   new   JRadioButton(   "CENTER"   )); 51 jpJustification.add(jrbRight =   new   JRadioButton(   "RIGHT"   )); 52 jpJustification.setBorder(   new   TitledBorder(   "Justification"   )); 53 54  // Create panel jpTitleOptions to hold jpPosition and  55  // jpJustification  56 JPanel jpTitleOptions =   new   JPanel(); 57 jpTitleOptions.setLayout(   new   BorderLayout()); 

[Page 957]
 58 jpTitleOptions.add(jpPosition, BorderLayout.CENTER); 59 jpTitleOptions.add(jpJustification, BorderLayout.EAST); 60 61  // Create Panel jpTitle to hold a check box and title position  62  // radio buttons, and title justification radio buttons  63 JPanel jpTitle =   new   JPanel(); 64 jpTitle.setBorder(   new   TitledBorder(   "Border Title"   )); 65 jpTitle.setLayout(   new   BorderLayout()); 66 jpTitle.add(jchkTitled =   new   JCheckBox(   "Titled"   ), 67 BorderLayout.NORTH); 68 jpTitle.add(jpTitleOptions, BorderLayout.CENTER); 69 70  // Group radio buttons for title position  71 ButtonGroup btgTitlePosition =   new   ButtonGroup(); 72 btgTitlePosition.add(jrbAboveBottom); 73 btgTitlePosition.add(jrbBottom); 74 btgTitlePosition.add(jrbBelowBottom); 75 btgTitlePosition.add(jrbAboveTop); 76 btgTitlePosition.add(jrbTop); 77 btgTitlePosition.add(jrbBelowTop); 78 79  // Group radio buttons for title justification  80 ButtonGroup btgTitleJustification =   new   ButtonGroup(); 81 btgTitleJustification.add(jrbLeft); 82 btgTitleJustification.add(jrbCenter); 83 btgTitleJustification.add(jrbRight); 84 85  // Create Panel jpBorderStyle to hold border style radio buttons  86 JPanel jpBorderStyle =   new   JPanel(); 87 jpBorderStyle.setBorder(   new   TitledBorder(   "Border Style"   )); 88 jpBorderStyle.setLayout(   new   GridLayout(   6   ,   1   )); 89 jpBorderStyle.add(jrbLoweredBevel = 90   new   JRadioButton(   "Lowered Bevel"   )); 91 jpBorderStyle.add(jrbRaisedBevel = 92   new   JRadioButton(   "Raised Bevel"   )); 93 jpBorderStyle.add(jrbEtched =   new   JRadioButton(   "Etched"   )); 94 jpBorderStyle.add(jrbLine =   new   JRadioButton(   "Line"   )); 95 jpBorderStyle.add(jrbMatte =   new   JRadioButton(   "Matte"   )); 96 jpBorderStyle.add(jrbEmpty =   new   JRadioButton(   "Empty"   )); 97 98  // Group radio buttons for border styles  99 ButtonGroup btgBorderStyle =   new   ButtonGroup(); 100 btgBorderStyle.add(jrbLoweredBevel); 101 btgBorderStyle.add(jrbRaisedBevel); 102 btgBorderStyle.add(jrbEtched); 103 btgBorderStyle.add(jrbLine); 104 btgBorderStyle.add(jrbMatte); 105 btgBorderStyle.add(jrbEmpty); 106 107  // Create Panel jpAllChoices to place jpTitle and jpBorderStyle  108 JPanel jpAllChoices =   new   JPanel(); 109 jpAllChoices.setLayout(   new   BorderLayout()); 110 jpAllChoices.add(jpTitle, BorderLayout.CENTER); 111 jpAllChoices.add(jpBorderStyle, BorderLayout.EAST); 112 113  // Place panels in the frame  114 setLayout(   new   BorderLayout()); 115 add(jLabel1, BorderLayout.CENTER); 116 add(jpAllChoices, BorderLayout.SOUTH); 117 

[Page 958]
 118  // Register listeners  119  ActionListener listener =   new   EventListener();  120  jchkTitled.addActionListener(listener);  121 jrbAboveBottom.addActionListener(listener); 122 jrbBottom.addActionListener(listener); 123 jrbBelowBottom.addActionListener(listener); 124 jrbAboveTop.addActionListener(listener); 125 jrbTop.addActionListener(listener); 126 jrbBelowTop.addActionListener(listener); 127 jrbLeft.addActionListener(listener); 128 jrbCenter.addActionListener(listener); 129 jrbRight.addActionListener(listener); 130 jrbLoweredBevel.addActionListener(listener); 131 jrbRaisedBevel.addActionListener(listener); 132 jrbLine.addActionListener(listener); 133 jrbEtched.addActionListener(listener); 134 jrbMatte.addActionListener(listener); 135 jrbEmpty.addActionListener(listener); 136 } 137 138    private class   EventListener   implements   ActionListener {  139  /** Handle ActionEvents on check box and radio buttons */  140   public void   actionPerformed(ActionEvent e) { 141  // Get border style  142  Border border =   new   EmptyBorder (   2   ,   2   ,   2   ,   2   );  143 144   if   (jrbLoweredBevel.isSelected()) { 145  border =   new   BevelBorder(BevelBorder.LOWERED);  146 jLabel1.setText(   "Lowered Bevel Style"   ); 147 } 148   else if   (jrbRaisedBevel.isSelected()) { 149  border =   new   BevelBorder(BevelBorder.RAISED);  150 jLabel1.setText(   "Raised Bevel Style"   ); 151 } 152   else if   (jrbEtched.isSelected()) { 153  border =   new   EtchedBorder();  154 jLabel1.setText(   "Etched Style"   ); 155 } 156   else if   (jrbLine.isSelected()) { 157  border =   new   LineBorder(Color.black,   5   );  158 jLabel1.setText(   "Line Style"   ); 159 } 160   else if   (jrbMatte.isSelected()) { 161  border =   new   MatteBorder(   15   ,   15   ,   15   ,   15   ,  162   new   imageIcon(getClass().getResource(   "image/caIcon.gif"   ))); 163 jLabel1.setText(   "Matte Style"   ); 164 } 165   else if   (jrbEmpty.isSelected()) { 166  border =   new   EmptyBorder(   2   ,   2   ,   2   ,   2   );  167 jLabel1.setText(   "Empty Style"   ); 168 } 169 170   if   (jchkTitled.isSelected()) { 171  // Get the title position and justification  172   int   titlePosition = TitledBorder.DEFAULT_POSITION; 173   int   titleJustification = TitledBorder.DEFAULT_JUSTIFICATION; 174 175   if   (jrbAboveBottom.isSelected()) 176 titlePosition = TitledBorder.ABOVE_BOTTOM; 177   else if   (jrbBottom.isSelected()) 

[Page 959]
 178 titlePosition = TitledBorder.BOTTOM; 179   else if   (jrbBelowBottom.isSelected()) 180 titlePosition = TitledBorder.BELOW_BOTTOM; 181   else if   (jrbAboveTop.isSelected()) 182 titlePosition = TitledBorder.ABOVE_TOP; 183   else if   (jrbTop.isSelected()) 184 titlePosition = TitledBorder.TOP; 185   else if   (jrbBelowTop.isSelected()) 186 titlePosition = TitledBorder.BELOW_TOP; 187 188   if   (jrbLeft.isSelected()) 189 titleJustification = TitledBorder.LEFT; 190   else if   (jrbCenter.isSelected()) 191 titleJustification = TitledBorder.CENTER; 192   else if   (jrbRight.isSelected()) 193 titleJustification = TitledBorder.RIGHT; 194 195 jLabel1Border =   new   TitledBorder(   "A Title"   ); 196 jLabel1Border.setBorder(border); 197 jLabel1Border.setTitlePosition(titlePosition); 198 jLabel1Border.setTitleJustification(titleJustification); 199 jLabel1.setBorder(jLabel1Border); 200 } 201   else   { 202 jLabel1.setBorder(border); 203 } 204 } 205 } 206 } 

This example uses many panels to group UI components to achieve the desired look. Figure 28.27 illustrates the relationship of the panels. The Border Title panel groups all the options for setting title properties. The position options are grouped in the Position panel. The justification options are grouped in the Justification panel. The Border Style panel groups the radio buttons for choosing Lowered Bevel, Raised Bevel, Etched, Line, Matte, and Empty borders.

The label displays the selected border with or without a title, depending on the selection of the title check box. The label also displays a text indicating which type of border is being used, depending on the selection of the radio button in the Border Style panel.

The TitledBorder can be mixed with other borders. To do so, simply create an instance of TitledBorder , and use the setBorder method to embed a new border in TitledBorder .

The MatteBorder can be used to display icons on the border, as shown in Figure 28.28.

Figure 28.28. MatteBorder can display icons on the border.


 


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