简体   繁体   English

略微奇怪的JComboBox行为

[英]Slightly Odd JComboBox Behavior

Below is the code for a simplified piece of a larger GUI I'm working on. 下面是我正在研究的更大GUI的简化部分的代码。 I'm seeing some slightly strange behavior by the JComboBox . 我看到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. 正如预期的那样, JComboBox显示的值将会改变。 Now, without touching anything else, click on the “down arrow” thingy in the JComboBox . 现在,在不触及任何其他内容的情况下,单击JComboBox的“向下箭头”。 Its drop-down scroll pane will appear, but the selected value (“Jan”) will not be in the visible portion. 将出现其下拉滚动窗格,但所选值(“Jan”)将不在可见部分中。 It will look like this: 它看起来像这样:

在此输入图像描述

Now, without touching anything else, click in a blank section of the GUI to make the scroll pane disappear. 现在,在不触及任何其他内容的情况下,单击GUI的空白部分以使滚动窗格消失。 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: 它看起来像这样:

在此输入图像描述

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

I can also duplicate the problem using JDK7. 我也可以使用JDK7复制问题。 I don't see anything wrong with your code as all components are created on the EDT. 我没有看到您的代码有任何问题,因为所有组件都是在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();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM