简体   繁体   English

JTable JComboBox SetEditable不起作用

[英]JTable JComboBox SetEditable not working

I am trying to get an editable JComboBox to work within a table but, so far, no luck. 我试图使一个可编辑的JComboBox在表中工作,但到目前为止,还没有运气。 Setting cbo.setEditable(true) seems to have no effect when operating within a cell within a table. 在表中的单元格内进行操作时,设置cbo.setEditable(true)似乎无效。 Is there something I am missing? 我有什么想念的吗? Please help. 请帮忙。

Sample code to demonstrate the problem: 示例代码演示该问题:

public class ComboBoxTest {

    private JFrame frame;
    private JTable table;
    private JComboBox<?> cboFrm = null;
    private JComboBox<?> cboTbl = null;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ComboBoxTest window = new ComboBoxTest();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public ComboBoxTest() {
        initialise();
    }

    /**
     * Initialise the contents of the frame.
     */
    private void initialise() {
        frame = new JFrame();
        frame.setBounds(100, 100, 455, 233);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        String[] cboData = { "tspn", "tblspn", "gram", "Kg" };

        cboFrm = new JComboBox<String>(cboData);
        cboFrm.setBounds(10, 26, 86, 20);
        cboFrm.setEditable(true);
        frame.getContentPane().add(cboFrm);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 57, 414, 127);
        frame.getContentPane().add(scrollPane);

        String columnNames[] = { "Qty", "Measure", "Ingrediant" };
        // @formatter:off
        Object[][] tableData =
            {
                { 1, "Kg", "Sugar" },
                { 1, "pinch", "Salt" },
                { 2, "handfuls", "Peanuts" },
                { 1, "Litre", "Milk"}
            };
        // @formatter:on
        table = new JTable(tableData, columnNames);
        cboTbl = new JComboBox<String>(cboData);
        cboTbl.setEditable(true);
        table.getColumnModel().getColumn(1)
                .setCellEditor(new DefaultCellEditor(cboTbl));
        scrollPane.setViewportView(table);

    }

}

Worked fine for me with this output: 此输出对我来说很好用:

在此处输入图片说明

To answer my own question. 回答我自己的问题。

There seems to be a bug in the way JComboBox interacts with JTable. JComboBox与JTable交互的方式似乎存在一个错误。 The workaround is to press 'enter' after the new text has been altered. 解决方法是在更改新文本后按“输入”。 Tabbing off the table JComboBox cell doesn't work which is inconsistent with the behaviour of a standard text cell. 跳出表格JComboBox单元格不起作用,这与标准文本单元格的行为不一致。

There is a bug report dating back to 1999-2001 where it was supposedly fixed but it has again resurfaced. 有一个错误报告可以追溯到1999-2001年,据说该错误已得到修复,但再次出现。 Refer to the following link: 请参考以下链接:

http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4275046 http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4275046

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

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