Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slightly Odd JComboBox Behavior

Below is the code for a simplified piece of a larger GUI I’m working on. I’m seeing some slightly strange behavior by the JComboBox. To recreate the issue, run the code and without touching anything else click on the ‘Toggle’ button. As expected, the value displayed in the JComboBox will change. Now, without touching anything else, click on the “down arrow” thingy in the JComboBox. Its drop-down scroll pane will appear, but the selected value (“Jan”) will not be in the visible portion. It will look like this:

enter image description here

Now, without touching anything else, click in a blank section of the GUI to make the scroll pane disappear. Now, click the “down arrow” again. This time the selected value will be in the visible portion of the scroll pane. It will look like this:

enter image description here

Any idea why this is happening? Thanks.

Here's the code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComboBoxIssue extends JPanel {
    private JComboBox myCombo; 
    private JButton myButton;
    private boolean state = true;
    public ComboBoxIssue() {
        super(new FlowLayout());
        myCombo = new JComboBox();
        myCombo.setModel(new DefaultComboBoxModel<>(new String[] {"Jan",
                "Feb", "March", "April", "May", "June", "July", "Aug", "Sept",
                "Oct", "Nov", "Dec"}));
        myCombo.setSelectedItem("Dec");
        myCombo.setMaximumRowCount(5);
        myButton = new JButton("Toggle");
        myButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (state) {
                    state = false;
                    myCombo.setSelectedItem("Jan");        
                } else {
                    state = true;
                    myCombo.setSelectedItem("Dec");     
                }
            }     
        });
        add(myCombo);
        add(myButton);
    }
    private static void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(
                "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
            );
        } catch (Exception exc) {
            // ignore error
        }
    }
    private static void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        JComponent newContentPane = new ComboBoxIssue();
        frame.add(newContentPane);
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        setLookAndFeel();
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGui();
            }
        });
    } 
}
like image 630
Greg Valvo Avatar asked May 01 '26 15:05

Greg Valvo


1 Answers

I can also duplicate the problem using JDK7. I don't see anything wrong with your code as all components are created on the EDT. I would suggest this is a bug.

Looks like there is a problem with the scrolling until the popup has been displayed for the first time.

I added the following code and it seems to work now:

add(myCombo);
add(myButton);

SwingUtilities.invokeLater(new Runnable()
{
    public void run()
    {
        myCombo.showPopup();
        myCombo.hidePopup();
    }
});
like image 82
camickr Avatar answered May 04 '26 03:05

camickr