-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckBoxExample.java
More file actions
136 lines (116 loc) · 3.84 KB
/
Copy pathCheckBoxExample.java
File metadata and controls
136 lines (116 loc) · 3.84 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
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
public class CheckBoxExample extends JFrame
{
private final int WINDOW_WIDTH = 640;
private final int WINDOW_HEIGHT = 240;
private JPanel panel;
private JPanel panel2;
private JButton button;
private JLabel instruction;
private JCheckBox foreground;
private JCheckBox background;
private JComboBox cBox;
private JTextField field;
// private JScrollPane scroll;
public CheckBoxExample()
{
super("Check Box Example");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanel();
panel2 = new JPanel();
button = new JButton("I'm a Button!");
panel2.add(button);
panel2.setBorder(BorderFactory.createMatteBorder(5, 10, 5, 10, new Color(0,100,0)));
setLayout(new FlowLayout());
add(panel);
add(panel2);
setVisible(true);
}
private void buildPanel()
{
panel = new JPanel();
//panel.setBorder(BorderFactory.createTitledBorder("A Title"));
//instruction = new JLabel("These checkboxes control the color");
foreground = new JCheckBox("Blue Foreground");
background = new JCheckBox("Green Background");
String[] names = {"Alice", "Bob", "Charlie", "Denise", "Evan", "Francis", "George"};
cBox = new JComboBox(names);
field = new JTextField(20);
field.setEditable(false);
cBox.setEditable(true);
foreground.addItemListener(new ColorListener());
background.addItemListener(new ColorListener());
cBox.addActionListener(new BoxListener());
// panel.add(instruction);
panel.add(foreground);
panel.add(background);
panel.add(cBox);
// panel.add(scroll);
panel.add(field);
}
private class BoxListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
String selection = (String) cBox.getSelectedItem();
field.setText(selection);
}
}
/*private class NameListener implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
Object[] names = list.getSelectedValues();
String display = "";
for (int i = 0; i < names.length; i++)
{
display += names[i] + " ";
}
field.setText(display);
}
}*/
private class ColorListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
if (e.getSource() == foreground)
{
if (foreground.isSelected())
{
instruction.setForeground(Color.BLUE);
foreground.setForeground(Color.BLUE);
background.setForeground(Color.BLUE);
}
else
{
instruction.setForeground(Color.BLACK);
foreground.setForeground(Color.BLACK);
background.setForeground(Color.BLACK);
}
}
else if (e.getSource() == background)
{
if (background.isSelected())
{
panel.setBackground(Color.GREEN);
foreground.setBackground(Color.GREEN);
background.setBackground(Color.GREEN);
}
else
{
panel.setBackground(Color.LIGHT_GRAY);
foreground.setBackground(Color.LIGHT_GRAY);
background.setBackground(Color.LIGHT_GRAY);
}
}
}
}
public static void main(String[] args)
{
new CheckBoxExample();
}
}