I'm quite new to Java Swing. And I'm stuck on trying to add a ListSelectionListener on a JComboBox instance. It seems only the ListSelectionModel interface has addListSelectionListener method. I kind of cannot figure it out...
Why I want to do add it is that I want program do something even the item in the combo box is not changes after selecting.
POTENTIAL ANSWER
I was simply thinking of attaching an actionListener on combobox not working. and i think it's bug of openjdk. I've reported it here
Thanks in advance.
Take a look at JComboBox#addItemListener:
JComboBox combo = createCombo();
combo.addItemListener(new ItemListener()
{
#Override
public void itemStateChanged(ItemEvent e)
{
if (e.getStateChange() == ItemEvent.SELECTED)
{
Object selectedItem = e.getItem();
// Do something with the selected item...
}
}
});
This event is fired for both mouse and keyboard interaction.
For JComboBox, you'll have to use ActionListener.
JComboBox jComboBox = new JComboBox();
jComboBox.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("combobox event");
}
});
AFAIK, actionPerformed is raised whenever the user makes a selection for the JComboBox even if it's the same item that was already selected.
It depends on your requirement. The ActionEvent is only fired when the keyboard is used, not when the selection changes as the mouse is moved over the items.
If you want to do some action when the item selection changes even if the mouse is moved then yes you will probably need access to the JList. You can access the JList used by the popup with the following code:
JComboBox comboBox = new JComboBox(...);
BasicComboPopup popup = (BasicComboPopup)comboBox.getAccessibleContext().getAccessibleChild(0);
JList list = popup.getList();
list.addListSelectionListener(...);
Use a PopupMenuListener. When the popup menu closes get the selected index and do your processing.
Related
I'm currently facing the situation that if a user clicks on my ComboBox and moves the selection with his keys, the selection listener will keep being called, although for all purposes a choice was still not really made by the user.
How can I distinguish those "intermediate" selections from the proper, final, user selection in my ComboBox?
I tried looking at variables such as isPopupVisible or even playing with PopupMenuListener but they didn't seem to really help.
Thanks
Edit: Example of the offending code:
public class Main extends JFrame {
public Main() {
setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
JComboBox<String> comboBox = new JComboBox<>();
comboBox.setModel(new DefaultComboBoxModel<>(new String[] { "a", "b", "c" }));
comboBox.addItemListener(e -> {
System.out.println(e.getItem());
});
add(comboBox);
}
}
I need something that only fires when the user actually clicks and the popup disappears.
comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
Set the above property so the event is only generated on a mouse released or an Enter key.
Note:
if you use an ActionListner the event will always be generated when an item is selected.
if you use an ItemListener the event is only generated if you change the selected item from its previous selection.
Edit:
You could disable key selection by using:
JComboBox comboBox = new JComboBox( model )
{
#Override
public boolean selectWithKeyChar(char keyChar)
{
return false;
}
};
Edit 2:
Or maybe as a hack you can disable your listener when you do the key search. The code might be something like:
JComboBox comboBox = new JComboBox( model )
{
#Override
public boolean selectWithKeyChar(char keyChar)
{
// remove the listener here
// This will cause the selected index to change
Boolean result = super.selectWithKeyChar(keyChar);
// add the listener back here
return result;
}
};
I habe a Java-Problem.
I made a code which includes a Checkbox (mPortalJCheckbox) and Combobox (mVersandComboBox).
When the Checkbox is true, there should only be 2 items in mVersandComboBox.
Otherwise there should be 3 items.
My Listener is like this:
mPortalJCheckbox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (mPortalJCheckbox.isSelected() == false) {
System.out.println(mPortalJCheckbox.isSelected());
mVersandComboBox.removeItemAt(2);
mVersandComboBox.revalidate();
mVersandComboBox.repaint();
}
}
});
I think the Combobox removes the last item but it doesn't repaint the Combobox right. Where is my mistake?
Thanks :)
You don't need revalidate() or repaint(). The comboBox will repaint itself when data in the combo box model is changed.
The removeItemAt(...) method only works if the ComboBoxModel is mutable. Make sure you are using the DefaultComboBoxModel:
DefaultComboBoxModel model = new DefaultComboBoxModel(...);
JComboBox comboBox = new JComboBox( model );
If you need more help then post a proper SSCCE that demonstrates the problem.
I have two JComboBox and one button. I am trying to do that if I select an item from the two combo box individually and press the button called search. Then the two selected items from the two combo box will save in a new two separate string.
Please anyone help me to solve the problem.
Here is the code snippet
//here is the strings that in the combo box
String lc[] = {"Kolabagan-Dhaka", "Gabtoli-Dhaka", "Fakirapul-Dhaka", "Shaymoli-Dhaka"};
String rc[] = {"Banani-Bogra", "Rangpur","Shatrasta-Bogra"};
//here is my two jcombo box
JComboBox lcCombo = new JComboBox(lc);
JComboBox rcCombo = new JComboBox(rc);
// here is my search button
JButton searchButton = new JButton("Search");
There are two ways to go about this. The first is to have one class that implements ActionListener and in the implementation, check the source (ActionEvent.getSource()). Based on which component sourced the event, you take the appropriate action.
The other option (and my preference) is to create an ActionListener for each component that requires one. You can use anonymous classes if you don't want to explicitly define one for each case. This way each listener knows exactly what component caused the event and what action to take.
Example:
JComboBox lcCombo = new JComboBox(lc);
lcCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//do left stuff
}
});
JComboBox rcCombo = new JComboBox(rc);
rcCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//do right stuff
}
});
To expand on unholysampler, once you have the ActionListener working you can use lcCombo.getSelectedIndex() to check which item has been selected.
I have a JComboBox. I would like it to work so that if a certain item is selected ("Other"), immediately, several more items are displayed in the same combo box (something like a submenu, but inside the combo box). I'm having a devil of a time getting this to work.
Does anyone have any ideas on how to do this?
EDIT: misunderstood your question!
I assume that you are clicking on an item in the JComboBox? Than simply add this code
comboOther.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
comboOther.addItem("new item 1");
comboOther.addItem("new item 2");
comboOther.addItem("new item 3");
// more
}
});
I need some help here for a GUI with Java OOP, I am using Eclipse.
I am creating a "select airlines" GUI with Combobox, JLabel and pictures.
1st choice, F16(combobox) add $600(JLabel) F16.jpg (outside of the combobox).
But inside the panel while selected the 2nd choice F22(combobox) the JLabel should automatically change add $900(JLabel) as well as the picture to F12.jpg
any guys can help me with Combobox,the JLabel coding, pictures
Thanks a lot!
Basically you want to use an ItemListener on your JComboBox:
JComboBox box = new JComboBox();
// Adds a listener - this performs an action when the item changes.
box.addItemListener(new ItemListener(){
#Override
public void itemStateChanged(ItemEvent e) {
//Checks if this event was caused because an item was selected
if((e.getStateChange() & ItemEvent.SELECTED) == ItemEvent.SELECTED){
System.out.println(e.getItem());
// This is where you'd modify your label based on the dropdown's value - something like this:
label.setText("$900");
}
}});
panel.add(box);