jcombobox

How do I get the index of the item selected from a JComboBox?

拈花ヽ惹草 提交于 2019-12-01 01:09:04
This is how I created my JComboBox - String[] options = {"First", "Second" , "Third"}; JComboBox optionsCombo = new JComboBox(options); When one of these items is selected, how do i get the index of the item which was selected ? I don't want the item the item that was selected. Use : optionsCombo.getSelectedIndex(); inside actionListener Like this : ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Selected: " + optionsCombo.getSelectedItem()); System.out.println(", Position: " + optionsCombo.getSelectedIndex()); }

How to update JComboBox instances in Swing?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 00:40:24
I've 3 comboboxes, upon selecting first combobox, the rest should be updated but my code doesn't seems to be working. Please help in this guys. Here is my code(since my code very long so I'll write error part only). // example code public class GuiComponents { JComboBox<String> comboBox1, comboBox2, comboBox3; public GuiComponents() { ......... ......... String[] element1 = {"item1", "item2", "item3"}; String[] element2 = {"item1", "item2", item3}; String[] element3 = {"item1", "item2", "item3"}; comboBox1.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent

Using all (jComboBox, JTextField, jFileChooser) as table editor overrides the refrences

一个人想着一个人 提交于 2019-11-30 23:09:45
In the code below, for various rows of same table, I am trying to set an Editable comboBox as editor for first row ( so that the user can select from the available choices or type its own), a filechooser for second row and the default textFiled for the rest of rows. The Problem: and steps to reproduce it: 1- Run the code, 2- click on second row and choose a folder (the row turns yellow) 3- now click on first row to select the type of movie (just click , no need to type anything or to choose) 4- now make another click back on second row(Folder selection) you will see the contents of this row

Using an animated GIF in a JComboBox

只谈情不闲聊 提交于 2019-11-30 20:50:27
问题 I'm trying to use animated (GIF) icons in a JComboBox. As the DefaultListCellRenderer is based on JLabel, ImageIcons are directly supported when putting them into the the ComboBoxModel. However this does not work with animated GIFs. In the dropdown they are not show at all unless they are selected (the GIFs do work when used in a regular JLabel though) The code to populate the combobox is straight forward: ImageIcon[] data = new ImageIcon[4]; data[0] = new ImageIcon("icon_one.gif"); data[1] =

How do I get the index of the item selected from a JComboBox?

荒凉一梦 提交于 2019-11-30 19:55:43
问题 This is how I created my JComboBox - String[] options = {"First", "Second" , "Third"}; JComboBox optionsCombo = new JComboBox(options); When one of these items is selected, how do i get the index of the item which was selected ? I don't want the item the item that was selected. 回答1: Use : optionsCombo.getSelectedIndex(); inside actionListener Like this : ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Selected:

JComboBox as a Custom TableCellEditor

拈花ヽ惹草 提交于 2019-11-30 16:23:21
I have a table. Changes on that table update database. One column is edited by a JComboBox in that table. Clicks to any cell in that column fires a tableChanged event. However it needs to be fired after selecting an item of a JComboBox. How can i make tableChanged occur after selection? public class JIDCellEditor extends AbstractCellEditor implements TableCellEditor { JComboBox jComboBox; @Override public Object getCellEditorValue() { return jComboBox.getSelectedItem(); } @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)

Colored jcombobox with colored items and focus

淺唱寂寞╮ 提交于 2019-11-30 16:14:03
问题 i am trying to make a colored dropdown list with colored items (please see code below). The color is getting applied after the combobox loses focus. is this correct behaviour? how can i get the foreground and/or the background color to change when the combobox has the focus? thanks import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.border.*; public class DropDown { enum Colors { red(Color.red), orange(Color.orange), green(Color.green),

JComboBox as a Custom TableCellEditor

戏子无情 提交于 2019-11-30 16:06:14
问题 I have a table. Changes on that table update database. One column is edited by a JComboBox in that table. Clicks to any cell in that column fires a tableChanged event. However it needs to be fired after selecting an item of a JComboBox. How can i make tableChanged occur after selection? public class JIDCellEditor extends AbstractCellEditor implements TableCellEditor { JComboBox jComboBox; @Override public Object getCellEditorValue() { return jComboBox.getSelectedItem(); } @Override public

How do I know if an item of an auto-complete decorated JComboBox is mouse clicked?

不问归期 提交于 2019-11-30 14:31:05
I'm using the SwingX AutoCompleteDecorator for a JComboBox . The autocomplete feature works beautifully... But I have trouble to identify the moment of the final user selection ; to persist my data seldom. Let me try to explain: The combobox fires an "comboBoxChanged"- ActionEvent for every selection. I have to ignore these events while the user is typing characters and the combobox is auto-matching and selecting items. If the user hits the return-key an "comboBoxEdited"- ActionEvent is generated and I can save the selected value. Great ;-) If the mouse is used to open the JComboBox -PopUp and

displaying images in JComboBox

会有一股神秘感。 提交于 2019-11-30 14:04:39
问题 i need to display a image in JComboBox 回答1: Just add an Icon to the model instead of a String: import java.awt.*; import javax.swing.*; public class ComboBoxIcon extends JFrame { JComboBox comboBox; public ComboBoxIcon() { Object[] items = { new ImageIcon("about16.gif"), new ImageIcon("add16.gif"), new ImageIcon("copy16.gif") }; comboBox = new JComboBox( items ); getContentPane().add( comboBox, BorderLayout.NORTH ); } public static void main(String[] args) { JFrame frame = new ComboBoxIcon();