List of usage examples for javax.swing JComboBox setMaximumRowCount
@BeanProperty(preferred = true, description = "The maximum number of rows the popup should have") public void setMaximumRowCount(int count)
JComboBox
displays. From source file:Main.java
public static void main(final String args[]) { final DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>(); model.addElement("A"); model.addElement("C"); model.addElement("D"); model.addElement("A"); model.removeElement("A"); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComboBox<String> comboBox1 = new JComboBox<String>(model); comboBox1.setMaximumRowCount(5); comboBox1.setEditable(true);/*from ww w . ja v a 2s.c o m*/ frame.add(comboBox1, BorderLayout.NORTH); JList<String> jlist = new JList<String>(model); JScrollPane scrollPane = new JScrollPane(jlist); frame.add(scrollPane, BorderLayout.CENTER); JButton button = new JButton("Add"); frame.add(button, BorderLayout.SOUTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { model.addElement("a"); model.insertElementAt("Z", 0); } }; button.addActionListener(actionListener); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(final String args[]) { final DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>(); model.addElement("A"); model.addElement("C"); model.addElement("D"); model.addElement("A"); model.removeAllElements();//from w w w.j a v a 2 s .c o m JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComboBox<String> comboBox1 = new JComboBox<String>(model); comboBox1.setMaximumRowCount(5); comboBox1.setEditable(true); frame.add(comboBox1, BorderLayout.NORTH); JList<String> jlist = new JList<String>(model); JScrollPane scrollPane = new JScrollPane(jlist); frame.add(scrollPane, BorderLayout.CENTER); JButton button = new JButton("Add"); frame.add(button, BorderLayout.SOUTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { model.addElement("a"); model.insertElementAt("Z", 0); } }; button.addActionListener(actionListener); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(final String args[]) { final DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>(); model.addElement("A"); model.addElement("C"); model.addElement("D"); model.addElement("A"); model.setSelectedItem("C"); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComboBox<String> comboBox1 = new JComboBox<String>(model); comboBox1.setMaximumRowCount(5); comboBox1.setEditable(true);/*from ww w .ja v a 2 s. c o m*/ frame.add(comboBox1, BorderLayout.NORTH); JList<String> jlist = new JList<String>(model); JScrollPane scrollPane = new JScrollPane(jlist); frame.add(scrollPane, BorderLayout.CENTER); JButton button = new JButton("Add"); frame.add(button, BorderLayout.SOUTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { model.addElement("a"); model.insertElementAt("Z", 0); } }; button.addActionListener(actionListener); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(final String args[]) { final DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>(); model.addElement("A"); model.addElement("C"); model.addElement("D"); model.addElement("A"); model.removeElementAt(1);/*from w ww . j av a2 s.c o m*/ JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComboBox<String> comboBox1 = new JComboBox<String>(model); comboBox1.setMaximumRowCount(5); comboBox1.setEditable(true); frame.add(comboBox1, BorderLayout.NORTH); JList<String> jlist = new JList<String>(model); JScrollPane scrollPane = new JScrollPane(jlist); frame.add(scrollPane, BorderLayout.CENTER); JButton button = new JButton("Add"); frame.add(button, BorderLayout.SOUTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { model.addElement("a"); model.insertElementAt("Z", 0); } }; button.addActionListener(actionListener); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame(); Container contentPane = frame.getContentPane(); ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Command: " + e.getActionCommand()); int modifiers = e.getModifiers(); System.out.println("\tALT : " + checkMod(modifiers, ActionEvent.ALT_MASK)); System.out.println("\tCTRL : " + checkMod(modifiers, ActionEvent.CTRL_MASK)); System.out.println("\tMETA : " + checkMod(modifiers, ActionEvent.META_MASK)); System.out.println("\tSHIFT: " + checkMod(modifiers, ActionEvent.SHIFT_MASK)); Object source = e.getSource(); if (source instanceof JComboBox) { JComboBox jb = (JComboBox) source; System.out.println("Combo: " + jb.getSelectedItem()); }/*from w w w. ja va2 s . com*/ } private boolean checkMod(int modifiers, int mask) { return ((modifiers & mask) == mask); } }; String flavors[] = { "Item 1", "Item 2", "Item 3" }; JComboBox jc = new JComboBox(flavors); jc.setMaximumRowCount(4); jc.setEditable(true); jc.addActionListener(listener); contentPane.add(jc, BorderLayout.NORTH); JButton b = new JButton("Button!"); b.addActionListener(listener); contentPane.add(b, BorderLayout.CENTER); JPanel panel = new JPanel(); JLabel label = new JLabel("Label 1: "); JTextField text = new JTextField("Type your text", 15); text.addActionListener(listener); label.setDisplayedMnemonic(KeyEvent.VK_1); label.setLabelFor(text); panel.add(label); panel.add(text); contentPane.add(panel, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); }
From source file:ActionTest.java
public static void main(String args[]) { JFrame frame = new JFrame(); Container contentPane = frame.getContentPane(); ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Command: " + e.getActionCommand()); System.out.println("Modifiers: "); int modifiers = e.getModifiers(); System.out.println("\tALT : " + checkMod(modifiers, ActionEvent.ALT_MASK)); System.out.println("\tCTRL : " + checkMod(modifiers, ActionEvent.CTRL_MASK)); System.out.println("\tMETA : " + checkMod(modifiers, ActionEvent.META_MASK)); System.out.println("\tSHIFT: " + checkMod(modifiers, ActionEvent.SHIFT_MASK)); Object source = e.getSource(); if (source instanceof JComboBox) { JComboBox jb = (JComboBox) source; System.out.println("Combo: " + jb.getSelectedItem()); }/* w w w . j av a 2 s .c om*/ } private boolean checkMod(int modifiers, int mask) { return ((modifiers & mask) == mask); } }; String flavors[] = { "Item 1", "Item 2", "Item 3" }; JComboBox jc = new JComboBox(flavors); jc.setMaximumRowCount(4); jc.setEditable(true); jc.addActionListener(listener); contentPane.add(jc, BorderLayout.NORTH); JButton b = new JButton("Button!"); b.addActionListener(listener); contentPane.add(b, BorderLayout.CENTER); JPanel panel = new JPanel(); JLabel label = new JLabel("Label 1: "); JTextField text = new JTextField("Type your text", 15); text.addActionListener(listener); label.setDisplayedMnemonic(KeyEvent.VK_1); label.setLabelFor(text); panel.add(label); panel.add(text); contentPane.add(panel, BorderLayout.SOUTH); frame.pack(); frame.show(); }
From source file:PopupTest.java
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) { }//from ww w . j a va 2 s . c o m 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.show(); }
From source file:Main.java
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) { }//from ww w.ja v a 2s . c o m 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); }
From source file:ColorComboBox.java
public static void main(String args[]) { Color colors[] = { Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green, Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow }; JFrame frame = new JFrame("Color JComboBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); final JComboBox comboBox = new JComboBox(colors); comboBox.setMaximumRowCount(5); comboBox.setEditable(true);//ww w. j a va 2 s . c om comboBox.setRenderer(new ColorCellRenderer()); Color color = (Color) comboBox.getSelectedItem(); ComboBoxEditor editor = new ColorComboBoxEditor(color); comboBox.setEditor(editor); contentPane.add(comboBox, BorderLayout.NORTH); final JLabel label = new JLabel(); label.setOpaque(true); label.setBackground((Color) comboBox.getSelectedItem()); contentPane.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); }
From source file:ItemTest.java
public static void main(String args[]) { JFrame frame = new JFrame(); Container contentPane = frame.getContentPane(); ItemListener listener = new ItemListener() { public void itemStateChanged(ItemEvent e) { System.out.println("Source: " + name(e.getSource())); System.out.println("Item: " + name(e.getItem())); int state = e.getStateChange(); System.out.println("State: " + ((state == ItemEvent.SELECTED) ? "Selected" : "Deselected")); }// w w w.j a v a2 s. c o m private String name(Object o) { if (o instanceof JComponent) { JComponent comp = (JComponent) o; return comp.getName(); } else { return o.toString(); } } }; JPanel panel = new JPanel(new GridLayout(0, 1)); ButtonGroup group = new ButtonGroup(); JRadioButton option = new JRadioButton("French Fries", true); option.setName(option.getText()); option.addItemListener(listener); group.add(option); panel.add(option); option = new JRadioButton("Onion Rings", false); option.setName(option.getText()); option.addItemListener(listener); group.add(option); panel.add(option); option = new JRadioButton("Ice Cream", false); option.setName(option.getText()); option.addItemListener(listener); group.add(option); panel.add(option); contentPane.add(panel, BorderLayout.NORTH); String flavors[] = { "Item 1", "Item 2", "Item 3" }; JComboBox jc = new JComboBox(flavors); jc.setName("Combo"); jc.addItemListener(listener); jc.setMaximumRowCount(4); contentPane.add(jc, BorderLayout.SOUTH); frame.pack(); frame.show(); }