Update JComboBox immediately upon JComboBox selection - java

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
}
});

Related

Populating JComboBox in PopupMenuListener causes arrow button to disappear

I need to populate a JComboBox when the user clicks on it, because it is populated from a database which can change, and the user specifically requested this functionality.
That part works fine, but after the list populates the down arrow button disappears.
This is how I'm doing it:
myComboBox.addPopupMenuListener(new PopupMenuListener(){
#Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e){
JComboBox comboBox = (JComboBox)e.getSource();
comboBox.removeAll();
for(String value : DbClass.retrieveValues()){
comboBox.addItem(value);
}
}
}
I also tried getting the model of the JComboBox and calling removeAllElements() instead of using removeAll() on the JComboBox itself. The result was the same, with the down arrow button disappearing.
The following already-answered question does not address my problem, because the proposed solution does not work in this case:
JCombobox arrow disappears

Combobox in Java repaint

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.

Binding combobox and JLabel array, pictures together

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);

Adding to a databse, and editable ComboBoxModel on 'enter' press...with a keylistener?

I'd like to add to a database, and my editable comboBoxModel when I enter a new name into the comboBox. I have the method for adding to the database down fine, I'm just trying to get it to somehow listen to an entry being added to the ComboBox.
What's the Best way to do this?
I've read the Java tutorial on Editable ComboBoxes, and noted where it said:
An editable combo box fires an action event when the user chooses an item from the menu and when the user types Enter. Note that the menu remains unchanged when the user enters a value into the combo box. If you want, you can easily write an action listener that adds a new item to the combo box's menu each time the user types in a unique value.
So I thought to myself, ok lets try this, and looked up some examples. Here is my attempt, essentially copy pasted out of the example I found, with my variable names:
playerNameComboBox.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("comboBoxEdited")) {
System.out.println("Adding new player!");
IController.Util.getInstance().addNewPlayer();
playerNameComboBox.insertItemAt(playerNameComboBox.getSelectedItem(), 0);
}
}
});
When I type in a new name, and press enter, it does nothing. No new database entry and no addtional option on the ComboBox. I haven't attached an action command to the ComboBox as I thought the example above assumed it would have that as default, and so did I.
But how do I get it to shout out that action command when I press enter, with the focus on the comboBox? I would have thought that comboBoxes would have had some sort of default behaviour to shout that out? Do I need to use an if(playerNameComboBox.hasFocus()) statement? Should I implement some kind of keylistener when my comboBox hasFocus()?
I'm very new at Java, so I'm unsure as to how this sort of thing should be done; any help is very much appreciated.
As requested, here is my short example in which names may be added to a JComboBox.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Test extends JFrame {
private JComboBox box;
public static void main(String[] args) {
new Test();
}
public Test()
{
super();
setSize(200, 100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
box = new JComboBox();
box.setEditable(true);
getContentPane().add(box);
box.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("comboBoxEdited")) {
System.out.println("Adding new player!");
box.insertItemAt(box.getSelectedItem(), 0);
}
}
});
setVisible(true);
}
}

add ListSelectionListener on JComboBox

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.

Categories