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