调用setSelectedItem()后JComboBox弹出单元格高度错误

时间:2015-04-24 07:16:21

标签: java popup jcombobox

我想要一个下拉组合框,其中所选元素在未选中时为单行,但当我从下拉列表中选择时,每个单元格都更高并显示一些额外信息

Drop down cells are multiple lines

这是通过以下代码实现的:

import java.awt.Component;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;

public class ComboJFrame extends javax.swing.JFrame {

    private boolean popup;

    private javax.swing.JComboBox jComboBox1;

    public ComboJFrame() {
        initComponents();
    }

    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.FlowLayout());

        DefaultComboBoxModel model = new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3",
                                                                                                                                                                         "Long Long Item 4" });
        jComboBox1.setModel(model);
        jComboBox1.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                System.out.println("rend = " + value);
                // if popup is showing we need to make elements tall
                if (popup) this.setText("<html>" + value.toString().replaceAll(" ", "<br/>\n"));
                return this;
            }
        ;
        });
                jComboBox1.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
                popup = false;
                System.out.println("popup = " + popup);
            }

            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                popup = false;
                System.out.println("popup = " + popup);
            }

            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
                popup = true;
                jComboBox1.invalidate();
                System.out.println("popup = " + popup);
            }
        });
        getContentPane().add(jComboBox1);

        pack();

        model.setSelectedItem("Item 3");  // THIS GIVES THE PROBLEM
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ComboJFrame().setVisible(true);
            }
        });
    }
}

问题是如果我以编程方式选择项目,例如

        model.setSelectedItem("Item 3");

然后JCombobox错误地显示单元格高度并显示

enter image description here

以编程方式选择项目时,仅发生。如果我通过手动点击鼠标选择一个项目,然后再次打开弹出窗口则会消失。

我在使用Java 7和Java 8的Ubuntu Linux 14.04上看到了这一点。

我想解决这个问题,并了解Windows和Mac是否存在同样的问题。

奇怪的是,如果我以一种从不调用setSelectedItem的方式向模型添加项目,例如:

model.removeAllElements();
model.addElement("Item 3");
model.insertElementAt("Item 2",0);
model.insertElementAt("Item 1",0);
model.addElement("Long Item 4");

那么问题就不会出现了

另一种解决方法似乎是选择项目,然后重新设置模型:

model.setSelectedItem("Item 3");
jComboBox1.setModel(new DefaultComboBoxModel());
jComboBox1.setModel(model);

以上是测试代码,实际应用是Apertium机器翻译,组合框如下:

Apertium-viewer combobox

0 个答案:

没有答案