|
| 1 | +import java.awt.*; |
| 2 | +import java.awt.event.*; |
| 3 | +import java.io.*; |
| 4 | +import java.net.*; |
| 5 | +import javax.swing.*; |
| 6 | +import javax.swing.border.*; |
| 7 | +import javax.swing.event.*; |
| 8 | + |
| 9 | +public class ComponentBorderDemo extends JPanel |
| 10 | + implements ActionListener |
| 11 | +{ |
| 12 | + private JRadioButton top; |
| 13 | + private JRadioButton left; |
| 14 | + private JRadioButton bottom; |
| 15 | + private JRadioButton right; |
| 16 | + |
| 17 | + private JRadioButton leading; |
| 18 | + private JRadioButton center; |
| 19 | + private JRadioButton trailing; |
| 20 | + |
| 21 | + private JSpinner gap; |
| 22 | + |
| 23 | + private JCheckBox adjustInsets; |
| 24 | + |
| 25 | + ComponentBorderDemo() |
| 26 | + { |
| 27 | + setLayout( new BoxLayout(this,BoxLayout.X_AXIS) ); |
| 28 | + setBorder( new EmptyBorder(10, 10, 10, 10) ); |
| 29 | + |
| 30 | + JComponent east = createEastPanel(); |
| 31 | + JComponent center = createCenterPanel(); |
| 32 | + |
| 33 | + add(center); |
| 34 | + add( Box.createHorizontalStrut(10) ); |
| 35 | + add(east); |
| 36 | + } |
| 37 | + |
| 38 | + private Box createCenterPanel() |
| 39 | + { |
| 40 | + Box box = Box.createVerticalBox(); |
| 41 | + |
| 42 | + JPanel first = new JPanel(); |
| 43 | + first.setBorder( new TitledBorder("Normal Approach") ); |
| 44 | + JTextField textField1= new JTextField("text", 15); |
| 45 | + first.add( textField1 ); |
| 46 | + JButton button1= new JButton("?"); |
| 47 | + button1.addActionListener( new NameActionListener(textField1) ); |
| 48 | + first.add( button1 ); |
| 49 | + box.add( first ); |
| 50 | + |
| 51 | + JPanel second = new JPanel(); |
| 52 | + second.setBorder( new TitledBorder("Alternative Approach") ); |
| 53 | + JTextField textField2 = new JTextField("text", 15); |
| 54 | + JButton button2= new JButton("?"); |
| 55 | + button2.addActionListener( new NameActionListener(textField2) ); |
| 56 | + JPanel panel = new JPanel(); |
| 57 | + panel.setLayout( new FlowLayout(FlowLayout.CENTER, 0, 0) ); |
| 58 | + panel.setBackground( textField2.getBackground() ); |
| 59 | + panel.setBorder( textField2.getBorder() ); |
| 60 | + textField2.setBorder(null); |
| 61 | + panel.add(textField2); |
| 62 | + panel.add(button2); |
| 63 | + second.add(panel); |
| 64 | + box.add(second); |
| 65 | + |
| 66 | + JPanel third = new JPanel(); |
| 67 | + third.setBorder( new TitledBorder("Component Border Approach") ); |
| 68 | + JTextField textField3 = new JTextField("text", 15); |
| 69 | + JButton button3 = new JButton("?"); |
| 70 | + button3.setMargin(new Insets(1, 1, 1, 1) ); |
| 71 | + ComponentBorder cb = new ComponentBorder(button3); |
| 72 | + button3.addActionListener( new NameActionListener(textField3) ); |
| 73 | + |
| 74 | + if (top.isSelected()) |
| 75 | + cb.setEdge(ComponentBorder.Edge.TOP); |
| 76 | + if (left.isSelected()) |
| 77 | + cb.setEdge(ComponentBorder.Edge.LEFT); |
| 78 | + if (bottom.isSelected()) |
| 79 | + cb.setEdge(ComponentBorder.Edge.BOTTOM); |
| 80 | + if (right.isSelected()) |
| 81 | + cb.setEdge(ComponentBorder.Edge.RIGHT); |
| 82 | + |
| 83 | + if (leading.isSelected()) |
| 84 | + cb.setAlignment(ComponentBorder.LEADING); |
| 85 | + if (center.isSelected()) |
| 86 | + cb.setAlignment(ComponentBorder.CENTER); |
| 87 | + if (trailing.isSelected()) |
| 88 | + cb.setAlignment(ComponentBorder.TRAILING); |
| 89 | + |
| 90 | + cb.setGap( (Integer)gap.getValue() ); |
| 91 | + |
| 92 | + cb.setAdjustInsets( adjustInsets.isSelected() ); |
| 93 | + |
| 94 | + cb.install(textField3); |
| 95 | + third.add( textField3 ); |
| 96 | + box.add( third ); |
| 97 | + |
| 98 | + return box; |
| 99 | + } |
| 100 | + |
| 101 | + private JPanel createEastPanel() |
| 102 | + { |
| 103 | + JPanel panel = new JPanel( new BorderLayout() ); |
| 104 | + panel.setBorder( new TitledBorder("Component Border Properties") ); |
| 105 | + |
| 106 | + Box box = Box.createVerticalBox(); |
| 107 | + panel.add(box); |
| 108 | + |
| 109 | + // Create Edge radio buttons |
| 110 | + |
| 111 | + top = new JRadioButton("Top"); |
| 112 | + left = new JRadioButton("Left"); |
| 113 | + bottom = new JRadioButton("Bottom"); |
| 114 | + right = new JRadioButton("Right"); |
| 115 | + right.setSelected( true ); |
| 116 | + |
| 117 | + ButtonGroup bg1 = new ButtonGroup(); |
| 118 | + bg1.add( top ); |
| 119 | + bg1.add( left ); |
| 120 | + bg1.add( bottom ); |
| 121 | + bg1.add( right ); |
| 122 | + |
| 123 | + JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT)); |
| 124 | + p1.setBorder( new TitledBorder("Edge") ); |
| 125 | + p1.add( top ); |
| 126 | + p1.add( left ); |
| 127 | + p1.add( bottom ); |
| 128 | + p1.add( right ); |
| 129 | + box.add( p1 ); |
| 130 | + |
| 131 | + // Create Alignment radio buttons |
| 132 | + |
| 133 | + leading = new JRadioButton("Leading"); |
| 134 | + center = new JRadioButton("Center"); |
| 135 | + trailing = new JRadioButton("Trailing"); |
| 136 | + center.setSelected(true); |
| 137 | + |
| 138 | + ButtonGroup bg2 = new ButtonGroup(); |
| 139 | + bg2.add( leading ); |
| 140 | + bg2.add( center ); |
| 141 | + bg2.add( trailing ); |
| 142 | + |
| 143 | + JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT)); |
| 144 | + p2.setBorder( new TitledBorder("Edge") ); |
| 145 | + p2.add( leading ); |
| 146 | + p2.add( center ); |
| 147 | + p2.add( trailing ); |
| 148 | + box.add( p2 ); |
| 149 | + |
| 150 | + // Create Gap |
| 151 | + |
| 152 | + JPanel p3 = new JPanel( new FlowLayout(FlowLayout.LEFT) ); |
| 153 | + gap = new JSpinner(new SpinnerNumberModel(5, 0, 50, 5)); |
| 154 | + p3.add( gap ); |
| 155 | + p3.add( new JLabel("Gap") ); |
| 156 | + box.add( p3 ); |
| 157 | + |
| 158 | + // Create adjust insets |
| 159 | + |
| 160 | + JPanel p4 = new JPanel( new FlowLayout(FlowLayout.LEFT) ); |
| 161 | + adjustInsets = new JCheckBox("Adjust Insets"); |
| 162 | + adjustInsets.setAlignmentX(0.5f); |
| 163 | + adjustInsets.setSelected(true); |
| 164 | + p4.add(adjustInsets); |
| 165 | + box.add(p4); |
| 166 | + |
| 167 | + JButton button = new JButton("Recreate Component Border"); |
| 168 | + button.addActionListener( this ); |
| 169 | + panel.add(button, BorderLayout.SOUTH); |
| 170 | + |
| 171 | + return panel; |
| 172 | + } |
| 173 | + |
| 174 | + public void actionPerformed(ActionEvent e) |
| 175 | + { |
| 176 | + remove(0); |
| 177 | + add(createCenterPanel(), 0); |
| 178 | + revalidate(); |
| 179 | + repaint(); |
| 180 | + } |
| 181 | + |
| 182 | + static class NameActionListener implements ActionListener |
| 183 | + { |
| 184 | + JTextField textField; |
| 185 | + |
| 186 | + NameActionListener(JTextField textField) |
| 187 | + { |
| 188 | + this.textField = textField; |
| 189 | + } |
| 190 | + |
| 191 | + public void actionPerformed(ActionEvent e) |
| 192 | + { |
| 193 | + JButton button = (JButton)e.getSource(); |
| 194 | + |
| 195 | + String text = JOptionPane.showInputDialog( |
| 196 | + button, |
| 197 | + "Enter Name", |
| 198 | + textField.getText() |
| 199 | + ); |
| 200 | + |
| 201 | + textField.setText( text ); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + public static void main(String[] args) |
| 206 | + { |
| 207 | + SwingUtilities.invokeLater(new Runnable() { |
| 208 | + public void run() { |
| 209 | + createAndShowGUI(); |
| 210 | + } |
| 211 | + }); |
| 212 | + } |
| 213 | + |
| 214 | + public static void createAndShowGUI() |
| 215 | + { |
| 216 | + JFrame frame = new JFrame("Component Border"); |
| 217 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 218 | + frame.add( new ComponentBorderDemo() ); |
| 219 | + frame.setSize(600, 360); |
| 220 | + frame.setLocationRelativeTo(null); |
| 221 | + frame.setVisible(true); |
| 222 | + } |
| 223 | +} |
0 commit comments