Java by API/javax.swing/JComboBox
Содержание
- 1 implements JComboBox.KeySelectionManager
- 2 JComboBox: addActionListener(ActionListener lis)
- 3 JComboBox: addItemListener(ItemListener lis)
- 4 JComboBox: addItem(Object o)
- 5 JComboBox: addPopupMenuListener(PopupMenuListener l)
- 6 JComboBox: getActionMap()
- 7 JComboBox: getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
- 8 JComboBox: getItemAt(int index)
- 9 JComboBox: getItemCount()
- 10 JComboBox: getKeySelectionManager()
- 11 JComboBox: getSelectedIndex()
- 12 JComboBox: getSelectedItem()
- 13 JComboBox: insertItemAt(Object anObject, int index)
- 14 JComboBox: isPopupVisible()
- 15 JComboBox: removeAllItems()
- 16 JComboBox: removeItemAt(int anIndex)
- 17 JComboBox: setEditable(boolean b)
- 18 JComboBox: setEditor(ComboBoxEditor anEditor)
- 19 JComboBox: setKeySelectionManager(KeySelectionManager aManager)
- 20 JComboBox: setMaximumRowCount(int count)
- 21 JComboBox: setRenderer(ListCellRenderer aRenderer)
- 22 JComboBox: setSelectedIndex(int anIndex)
- 23 JComboBox: setSelectedItem(Object anObject)
- 24 JComboBox: setUI(ComboBoxUI ui)
- 25 new JComboBox()
- 26 new JComboBox(ComboBoxModel aModel)
- 27 new JComboBox(Vector items)
implements JComboBox.KeySelectionManager
<source lang="java">
import javax.swing.ruboBoxModel; import javax.swing.JComboBox; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "A", "A", "B", "B", "C", "C" }; JComboBox cb = new JComboBox(items); cb.setKeySelectionManager(new MyKeySelectionManager()); }
} class MyKeySelectionManager implements JComboBox.KeySelectionManager {
long lastKeyTime = 0; String pattern = ""; public int selectionForKey(char aKey, ComboBoxModel model) { return 1; }
}
</source>
JComboBox: addActionListener(ActionListener lis)
<source lang="java">
import java.awt.BorderLayout; import java.awt.ItemSelectable; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JComboBox; import javax.swing.JFrame; public class MainClass {
public static void main(final String args[]) { final String labels[] = { "A", "B", "C", "D", "E" }; JFrame frame = new JFrame("Selecting JComboBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComboBox comboBox = new JComboBox(labels); frame.add(comboBox, BorderLayout.SOUTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Command: " + actionEvent.getActionCommand()); ItemSelectable is = (ItemSelectable)actionEvent.getSource(); System.out.println(", Selected: " + selectedString(is)); } }; comboBox.addActionListener(actionListener); frame.setSize(400, 200); frame.setVisible(true); } static private String selectedString(ItemSelectable is) { Object selected[] = is.getSelectedObjects(); return ((selected.length == 0) ? "null" : (String)selected[0]); }
}
</source>
JComboBox: addItemListener(ItemListener lis)
<source lang="java">
import java.awt.Color; import java.awt.ruponent; import java.awt.Graphics; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.Icon; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; public class MainClass extends JPanel implements ItemListener {
public MainClass() { JComboBox jc = new JComboBox(); jc.addItem("France"); jc.addItem("Germany"); jc.addItem("Italy"); jc.addItem("Japan"); jc.addItemListener(this); add(jc); } public void itemStateChanged(ItemEvent ie) { String s = (String)ie.getItem(); System.out.println(s); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.getContentPane().add(new MainClass()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 200); frame.setVisible(true); }
}
</source>
JComboBox: addItem(Object o)
<source lang="java">
import java.awt.Color; import java.awt.ruponent; import java.awt.Graphics; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.Icon; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; public class MainClass extends JPanel implements ItemListener {
public MainClass() { JComboBox jc = new JComboBox(); jc.addItem("France"); jc.addItem("Germany"); jc.addItem("Italy"); jc.addItem("Japan"); jc.addItemListener(this); add(jc); } public void itemStateChanged(ItemEvent ie) { String s = (String)ie.getItem(); System.out.println(s); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.getContentPane().add(new MainClass()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 200); frame.setVisible(true); }
}
</source>
JComboBox: addPopupMenuListener(PopupMenuListener l)
<source lang="java">
import java.awt.BorderLayout; import java.awt.Container; import javax.swing.ruboBoxModel; import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.event.PopupMenuEvent; import javax.swing.event.PopupMenuListener; public class Main {
public static void main(String args[]) { JFrame frame = new JFrame("Popup Menu Listener"); Container contentPane = frame.getContentPane(); final String flavors[] = { "Item 1", "Item 2", "Item 3"}; PopupMenuListener listener = new PopupMenuListener() { boolean initialized = false; public void popupMenuCanceled(PopupMenuEvent e) { } public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { } public void popupMenuWillBecomeVisible(PopupMenuEvent e) { if (!initialized) { JComboBox combo = (JComboBox) e.getSource(); ComboBoxModel model = new DefaultComboBoxModel(flavors); combo.setModel(model); initialized = true; } } }; JComboBox jc = new JComboBox(); jc.addPopupMenuListener(listener); jc.setMaximumRowCount(4); jc.setEditable(true); contentPane.add(jc, BorderLayout.NORTH); frame.pack(); frame.setVisible(true); }
}
</source>
JComboBox: getActionMap()
<source lang="java">
import java.awt.Dimension; import java.awt.event.ActionEvent; import java.util.Vector; import javax.swing.AbstractAction; import javax.swing.ruboBoxEditor; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.plaf.ruboBoxUI; public class MainClass extends JFrame {
JComboBox jcb; MainClass(String title) { super(title); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel jp = new JPanel(); Vector v = new Vector(); v.add("A"); v.add("B"); v.add("C"); jcb = new JComboBox(v); jcb.setEditable(true); jcb.getActionMap().put("selectNext", new DownAction()); jp.setPreferredSize(new Dimension(200, 35)); jp.add(jcb); getContentPane().add(jp); pack(); setVisible(true); } public static void main(String[] args) { new MainClass("Binding Demo3"); }
} class DownAction extends AbstractAction {
public void actionPerformed(ActionEvent e) { JComboBox jcb = (JComboBox) e.getSource(); System.out.println("down action"); ComboBoxUI ui = jcb.getUI(); if (ui.isPopupVisible(jcb)) { int i = jcb.getSelectedIndex(); if (i < jcb.getModel().getSize() - 1) { jcb.setSelectedIndex(i + 1); jcb.repaint(); } } else { int nItems = jcb.getItemCount(); ComboBoxEditor cbe = jcb.getEditor(); String st; // Search text st = ((String) cbe.getItem()).toUpperCase(); for (int i = 0; i < nItems; i++) { String item = ((String) jcb.getItemAt(i)).toUpperCase(); if (item.startsWith(st)) { jcb.setSelectedIndex(i); break; } } ui.setPopupVisible(jcb, true); } }
}
</source>
JComboBox: getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
<source lang="java">
import java.awt.Dimension; import java.awt.Event; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.util.Vector; import javax.swing.AbstractAction; import javax.swing.ruboBoxEditor; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.KeyStroke; public class MainClass extends JFrame {
JComboBox jcb; MainClass(String title) { super(title); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel jp = new JPanel(); Vector v = new Vector(); v.add("A"); v.add("B"); v.add("C"); jcb = new JComboBox(v); jcb.setEditable(true); KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, Event.CTRL_MASK); jcb.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(ks, "clearEditor"); jcb.getActionMap().put("clearEditor", new ClearEditorAction()); jp.setPreferredSize(new Dimension(200, 35)); jp.add(jcb); getContentPane().add(jp); pack(); setVisible(true); } public static void main(String[] args) { new MainClass("Binding Demo3"); }
} class ClearEditorAction extends AbstractAction {
public void actionPerformed(ActionEvent e) { JComboBox jcb = (JComboBox) e.getSource(); ComboBoxEditor cbe = jcb.getEditor(); cbe.setItem(""); }
}
</source>
JComboBox: getItemAt(int index)
<source lang="java">
import javax.swing.JComboBox; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "item1", "item2", "item3" }; JComboBox cb = new JComboBox(items); // Get number of items int num = cb.getItemCount(); // Get items for (int i = 0; i < num; i++) { Object item = cb.getItemAt(i); } }
}
</source>
JComboBox: getItemCount()
<source lang="java">
import java.awt.Dimension; import java.awt.event.ActionEvent; import java.util.Vector; import javax.swing.AbstractAction; import javax.swing.ruboBoxEditor; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.plaf.ruboBoxUI; public class MainClass extends JFrame {
JComboBox jcb; MainClass(String title) { super(title); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel jp = new JPanel(); Vector v = new Vector(); v.add("A"); v.add("B"); v.add("C"); jcb = new JComboBox(v); jcb.setEditable(true); jcb.getActionMap().put("selectNext", new DownAction()); jp.setPreferredSize(new Dimension(200, 35)); jp.add(jcb); getContentPane().add(jp); pack(); setVisible(true); } public static void main(String[] args) { new MainClass("Binding Demo3"); }
} class DownAction extends AbstractAction {
public void actionPerformed(ActionEvent e) { JComboBox jcb = (JComboBox) e.getSource(); System.out.println("down action"); ComboBoxUI ui = jcb.getUI(); if (ui.isPopupVisible(jcb)) { int i = jcb.getSelectedIndex(); if (i < jcb.getModel().getSize() - 1) { jcb.setSelectedIndex(i + 1); jcb.repaint(); } } else { int nItems = jcb.getItemCount(); ComboBoxEditor cbe = jcb.getEditor(); String st; // Search text st = ((String) cbe.getItem()).toUpperCase(); for (int i = 0; i < nItems; i++) { String item = ((String) jcb.getItemAt(i)).toUpperCase(); if (item.startsWith(st)) { jcb.setSelectedIndex(i); break; } } ui.setPopupVisible(jcb, true); } }
}
</source>
JComboBox: getKeySelectionManager()
<source lang="java">
import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JComboBox; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "A", "A", "B", "B", "C" }; JComboBox cb = new JComboBox(items); cb.addKeyListener(new MyKeyListener()); }
} class MyKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent evt) { JComboBox cb = (JComboBox) evt.getSource(); int curIx = cb.getSelectedIndex(); char ch = evt.getKeyChar(); JComboBox.KeySelectionManager ksm = cb.getKeySelectionManager(); if (ksm != null) { int ix = ksm.selectionForKey(ch, cb.getModel()); boolean noMatch = ix < 0; boolean uniqueItem = ix == curIx; if (noMatch || !uniqueItem) { cb.showPopup(); } } }
}
</source>
JComboBox: getSelectedIndex()
<source lang="java">
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JComboBox; import javax.swing.JFrame; public class MainClass {
public static void main(final String args[]) { final String labels[] = { "A", "B", "C", "D", "E" }; JFrame frame = new JFrame("Editable JComboBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JComboBox comboBox = new JComboBox(labels); comboBox.setMaximumRowCount(5); comboBox.setEditable(true); frame.add(comboBox, BorderLayout.NORTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Selected: " + comboBox.getSelectedItem()); System.out.println(", Position: " + comboBox.getSelectedIndex()); } }; comboBox.addActionListener(actionListener); frame.setSize(300, 200); frame.setVisible(true); }
}
</source>
JComboBox: getSelectedItem()
<source lang="java">
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JComboBox; import javax.swing.JFrame; public class MainClass {
public static void main(final String args[]) { final String labels[] = { "A", "B", "C", "D", "E" }; JFrame frame = new JFrame("Editable JComboBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JComboBox comboBox = new JComboBox(labels); comboBox.setMaximumRowCount(5); comboBox.setEditable(true); frame.add(comboBox, BorderLayout.NORTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Selected: " + comboBox.getSelectedItem()); System.out.println(", Position: " + comboBox.getSelectedIndex()); } }; comboBox.addActionListener(actionListener); frame.setSize(300, 200); frame.setVisible(true); }
}
</source>
JComboBox: insertItemAt(Object anObject, int index)
<source lang="java">
import javax.swing.JComboBox; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "item1", "item2" }; JComboBox cb = new JComboBox(items); // Add an item to the start of the list cb.insertItemAt("item0", 0); }
}
</source>
JComboBox: isPopupVisible()
<source lang="java">
import javax.swing.JComboBox; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "item1", "item2" }; JComboBox cb = new JComboBox(items); // Determine if the menu is visible boolean isVisible = cb.isPopupVisible(); }
}
</source>
JComboBox: removeAllItems()
<source lang="java">
import javax.swing.JComboBox; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "item1", "item2" }; JComboBox cb = new JComboBox(items); cb.removeAllItems(); }
}
</source>
JComboBox: removeItemAt(int anIndex)
<source lang="java">
import javax.swing.JComboBox; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "item1", "item2" }; JComboBox cb = new JComboBox(items); cb.removeItemAt(0); }
}
</source>
JComboBox: setEditable(boolean b)
<source lang="java">
import java.awt.BorderLayout; import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; public class MainClass {
public static void main(final String args[]) { String labels[] = { "A", "B", "C", "D", "E" }; final DefaultComboBoxModel model = new DefaultComboBoxModel(labels); JFrame frame = new JFrame("Shared Data"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); JComboBox comboBox1 = new JComboBox(model); comboBox1.setMaximumRowCount(5); comboBox1.setEditable(true); JComboBox comboBox2 = new JComboBox(model); comboBox2.setMaximumRowCount(5); comboBox2.setEditable(true); panel.add(comboBox1); panel.add(comboBox2); frame.add(panel, BorderLayout.NORTH);
frame.setSize(300, 200); frame.setVisible(true); }
}
</source>
JComboBox: setEditor(ComboBoxEditor anEditor)
<source lang="java">
import java.awt.BorderLayout; import java.awt.ruponent; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.HashMap; import java.util.Map; import javax.swing.ruboBoxEditor; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.BevelBorder; public class MainClass extends JPanel {
private IconItem items[] = { new IconItem("A", "A.gif"), new IconItem("B", "B.gif"), new IconItem("C", "C.gif") }; Map itemMap = new HashMap(); public MainClass() { // Build a mapping from book titles to their entries for (int i = 0; i < items.length; i++) { itemMap.put(items[i].getTitle(), items[i]); } setLayout(new BorderLayout()); JComboBox bookCombo = new JComboBox(items); bookCombo.setEditable(true); bookCombo.setEditor(new MyComboBoxEditor(itemMap, items[0])); bookCombo.setMaximumRowCount(4); bookCombo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("You chose " + ((JComboBox) e.getSource()).getSelectedItem() + "!"); } }); bookCombo.setActionCommand("Hello"); add(bookCombo, BorderLayout.CENTER); } public static void main(String s[]) { JFrame frame = new JFrame("Combo Box Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(new MainClass()); frame.pack(); frame.setVisible(true); }
} class IconItem {
private final String title; private final String imagePath; private ImageIcon image; public IconItem(String title, String imagePath) { this.title = title; this.imagePath = imagePath; } public String getTitle() { return title; } public ImageIcon getImage() { if (image == null) { image = new ImageIcon(imagePath); } return image; } // Override standard toString method to give a useful result public String toString() { return title; }
} class MyComboBoxEditor implements ComboBoxEditor {
Map map; ImagePanel panel; ImageIcon questionIcon; public MyComboBoxEditor(Map m, IconItem defaultChoice) { map = m; panel = new ImagePanel(defaultChoice); questionIcon = new ImageIcon("question.gif"); } public void setItem(Object anObject) { if (anObject != null) { panel.setText(anObject.toString()); IconItem entry = (IconItem) map.get(anObject.toString()); if (entry != null) panel.setIcon(entry.getImage()); else panel.setIcon(questionIcon); } } public Component getEditorComponent() { return panel; } public Object getItem() { return panel.getText(); } public void selectAll() { panel.selectAll(); } public void addActionListener(ActionListener l) { panel.addActionListener(l); } public void removeActionListener(ActionListener l) { panel.removeActionListener(l); } class ImagePanel extends JPanel { JLabel imageIconLabel; JTextField textField; public ImagePanel(IconItem initialEntry) { setLayout(new BorderLayout()); imageIconLabel = new JLabel(initialEntry.getImage()); imageIconLabel.setBorder(new BevelBorder(BevelBorder.RAISED)); textField = new JTextField(initialEntry.getTitle()); textField.setColumns(45); textField.setBorder(new BevelBorder(BevelBorder.LOWERED)); add(imageIconLabel, BorderLayout.WEST); add(textField, BorderLayout.EAST); } public void setText(String s) { textField.setText(s); } public String getText() { return (textField.getText()); } public void setIcon(Icon i) { imageIconLabel.setIcon(i); repaint(); } public void selectAll() { textField.selectAll(); } public void addActionListener(ActionListener l) { textField.addActionListener(l); } public void removeActionListener(ActionListener l) { textField.removeActionListener(l); } }
}
</source>
JComboBox: setKeySelectionManager(KeySelectionManager aManager)
<source lang="java">
import javax.swing.ruboBoxModel; import javax.swing.JComboBox; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "A", "A", "B", "B", "C", "C" }; JComboBox cb = new JComboBox(items); cb.setKeySelectionManager(new MyKeySelectionManager()); }
} class MyKeySelectionManager implements JComboBox.KeySelectionManager {
long lastKeyTime = 0; String pattern = ""; public int selectionForKey(char aKey, ComboBoxModel model) { return 1; }
}
</source>
JComboBox: setMaximumRowCount(int count)
<source lang="java">
import java.awt.BorderLayout; import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; public class MainClass {
public static void main(final String args[]) { String labels[] = { "A", "B", "C", "D", "E" }; final DefaultComboBoxModel model = new DefaultComboBoxModel(labels); JFrame frame = new JFrame("Shared Data"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); JComboBox comboBox1 = new JComboBox(model); comboBox1.setMaximumRowCount(5); comboBox1.setEditable(true); JComboBox comboBox2 = new JComboBox(model); comboBox2.setMaximumRowCount(5); comboBox2.setEditable(true); panel.add(comboBox1); panel.add(comboBox2); frame.add(panel, BorderLayout.NORTH);
frame.setSize(300, 200); frame.setVisible(true); }
}
</source>
JComboBox: setRenderer(ListCellRenderer aRenderer)
<source lang="java">
import java.awt.BorderLayout; import java.awt.Color; import java.awt.ruponent; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.DefaultListCellRenderer; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.ListCellRenderer; public class MainClass {
public static void main(String args[]) throws Exception { Color colors[] = { Color.GREEN, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW }; JFrame frame = new JFrame("Color JComboBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JComboBox comboBox = new JComboBox(colors); comboBox.setMaximumRowCount(5); comboBox.setEditable(true); comboBox.setRenderer(new ColorCellRenderer()); frame.add(comboBox, BorderLayout.NORTH); final JLabel label = new JLabel(); label.setOpaque(true); label.setBackground((Color) comboBox.getSelectedItem()); frame.add(label, BorderLayout.CENTER); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Color selectedColor = (Color) comboBox.getSelectedItem(); label.setBackground(selectedColor); } }; comboBox.addActionListener(actionListener); frame.setSize(300, 200); frame.setVisible(true); }
} class ColorCellRenderer implements ListCellRenderer {
protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer(); private final static Dimension preferredSize = new Dimension(0, 20); public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); if (value instanceof Color) { renderer.setBackground((Color) value); } renderer.setPreferredSize(preferredSize); return renderer; }
}
</source>
JComboBox: setSelectedIndex(int anIndex)
<source lang="java">
import java.awt.Dimension; import java.awt.event.ActionEvent; import java.util.Vector; import javax.swing.AbstractAction; import javax.swing.ruboBoxEditor; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.plaf.ruboBoxUI; public class MainClass extends JFrame {
JComboBox jcb; MainClass(String title) { super(title); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel jp = new JPanel(); Vector v = new Vector(); v.add("A"); v.add("B"); v.add("C"); jcb = new JComboBox(v); jcb.setEditable(true); jcb.getActionMap().put("selectNext", new DownAction()); jp.setPreferredSize(new Dimension(200, 35)); jp.add(jcb); getContentPane().add(jp); pack(); setVisible(true); } public static void main(String[] args) { new MainClass("Binding Demo3"); }
} class DownAction extends AbstractAction {
public void actionPerformed(ActionEvent e) { JComboBox jcb = (JComboBox) e.getSource(); System.out.println("down action"); ComboBoxUI ui = jcb.getUI(); if (ui.isPopupVisible(jcb)) { int i = jcb.getSelectedIndex(); if (i < jcb.getModel().getSize() - 1) { jcb.setSelectedIndex(i + 1); jcb.repaint(); } } else { int nItems = jcb.getItemCount(); ComboBoxEditor cbe = jcb.getEditor(); String st; // Search text st = ((String) cbe.getItem()).toUpperCase(); for (int i = 0; i < nItems; i++) { String item = ((String) jcb.getItemAt(i)).toUpperCase(); if (item.startsWith(st)) { jcb.setSelectedIndex(i); break; } } ui.setPopupVisible(jcb, true); } }
}
</source>
JComboBox: setSelectedItem(Object anObject)
<source lang="java">
import javax.swing.JComboBox; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "item1", "item2" }; JComboBox cb = new JComboBox(items); Object obj = cb.getSelectedItem(); cb.setSelectedItem("item2"); obj = cb.getSelectedItem(); cb.setSelectedItem("item3"); obj = cb.getSelectedItem(); }
}
</source>
JComboBox: setUI(ComboBoxUI ui)
<source lang="java">
import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.plaf.ruboBoxUI; import javax.swing.plaf.ruponentUI; import javax.swing.plaf.basic.BasicArrowButton; import javax.swing.plaf.basic.BasicComboBoxUI; public class MainClass {
public static void main(final String args[]) { final String labels[] = { "A", "B", "C", "D", "E" }; JFrame frame = new JFrame("Popup JComboBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComboBox comboBox = new JComboBox(labels); comboBox.setUI((ComboBoxUI) MyComboBoxUI.createUI(comboBox)); frame.add(comboBox, BorderLayout.NORTH); frame.setSize(300, 200); frame.setVisible(true); } static class MyComboBoxUI extends BasicComboBoxUI { public static ComponentUI createUI(JComponent c) { return new MyComboBoxUI(); } protected JButton createArrowButton() { JButton button = new BasicArrowButton(BasicArrowButton.EAST); return button; } }
}
</source>
new JComboBox()
<source lang="java">
import java.awt.Color; import java.awt.ruponent; import java.awt.Graphics; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.Icon; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; public class MainClass extends JPanel implements ItemListener {
public MainClass() { JComboBox jc = new JComboBox(); jc.addItem("France"); jc.addItem("Germany"); jc.addItem("Italy"); jc.addItem("Japan"); jc.addItemListener(this); add(jc); } public void itemStateChanged(ItemEvent ie) { String s = (String)ie.getItem(); System.out.println(s); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.getContentPane().add(new MainClass()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 200); frame.setVisible(true); }
}
</source>
new JComboBox(ComboBoxModel aModel)
<source lang="java">
import java.awt.BorderLayout; import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; public class MainClass {
public static void main(final String args[]) { String labels[] = { "A", "B", "C", "D", "E" }; final DefaultComboBoxModel model = new DefaultComboBoxModel(labels); JFrame frame = new JFrame("Shared Data"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); JComboBox comboBox1 = new JComboBox(model); comboBox1.setMaximumRowCount(5); comboBox1.setEditable(true); JComboBox comboBox2 = new JComboBox(model); comboBox2.setMaximumRowCount(5); comboBox2.setEditable(true); panel.add(comboBox1); panel.add(comboBox2); frame.add(panel, BorderLayout.NORTH);
frame.setSize(300, 200); frame.setVisible(true); }
}
</source>
new JComboBox(Vector items)
<source lang="java">
import java.util.Vector; import javax.swing.JComboBox; import javax.swing.JFrame; public class MainClass extends JFrame {
MainClass(String title) { super(title); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Vector v = new Vector(); v.add("A"); v.add("B"); v.add("C"); JComboBox jcb = new JComboBox(v); getContentPane().add(jcb); setSize(200, 50); setVisible(true); } public static void main(String[] args) { new MainClass("Combo box Demo1"); }
}
</source>