OnJava8-Examples/gui/ComboBoxes.java

37 lines
1.0 KiB
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: gui/ComboBoxes.java
// Using drop-down lists.
import javax.swing.*;
import java.awt.*;
import static net.mindview.util.SwingConsole.*;
public class ComboBoxes extends JFrame {
private String[] description = {
"Ebullient", "Obtuse", "Recalcitrant", "Brilliant",
"Somnescent", "Timorous", "Florid", "Putrescent"
};
private JTextField t = new JTextField(15);
2015-05-06 12:09:38 -07:00
private JComboBox<String> c = new JComboBox<>();
2015-04-20 15:36:01 -07:00
private JButton b = new JButton("Add items");
private int count = 0;
public ComboBoxes() {
for(int i = 0; i < 4; i++)
c.addItem(description[count++]);
t.setEditable(false);
2015-05-06 12:09:38 -07:00
b.addActionListener(e -> {
2015-05-05 14:05:39 -07:00
if(count < description.length)
c.addItem(description[count++]);
2015-04-20 15:36:01 -07:00
});
2015-05-06 12:09:38 -07:00
c.addActionListener(e -> {
2015-05-05 14:05:39 -07:00
t.setText("index: "+ c.getSelectedIndex() + " " +
((JComboBox)e.getSource()).getSelectedItem());
2015-04-20 15:36:01 -07:00
});
setLayout(new FlowLayout());
add(t);
add(c);
add(b);
}
public static void main(String[] args) {
run(new ComboBoxes(), 200, 175);
}
} ///:~