【问题标题】:JTable JComboBox SetEditable not workingJTable JComboBox SetEditable 不起作用
【发布时间】:2015-02-15 12:25:53
【问题描述】:

我正在尝试让可编辑的JComboBox 在表格中工作,但到目前为止,还没有运气。在表格中的单元格内操作时,设置cbo.setEditable(true) 似乎无效。有什么我想念的吗?请帮忙。

演示问题的示例代码:

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

    }

}

【问题讨论】:

  • 避免使用null 布局,像素完美的布局是现代用户界面设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正
  • 对我来说很好,你能描述一下你的实际问题和你的期望......
  • 对我也很好。
  • 感谢您的回复。我想做的是(比如说)添加“doggy”作为衡量标准,但是当我离开牢房时,它会恢复为“Kg”。我希望它保持“小狗”的状态。换句话说,我不想仅限于查找中的那些项目。相反,组合框列表是最常用的措施,但并不限制用户使用这些选项。 Ray2U

标签: java swing jtable jcombobox tablecelleditor


【解决方案1】:

这个输出对我来说很好:

【讨论】:

    【解决方案2】:

    回答我自己的问题。

    JComboBox 与 JTable 交互的方式似乎存在错误。解决方法是在更改新文本后按“输入”。将表格 JComboBox 单元格用 Tab 标记不起作用,这与标准文本单元格的行为不一致。

    有一个可以追溯到 1999-2001 年的错误报告,据说该错误已修复,但又再次出现。参考以下链接:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多