【问题标题】:JComboBox in JTable Cell not showing upJTable Cell中的JComboBox未显示
【发布时间】:2010-10-31 19:48:13
【问题描述】:

我正在尝试使用组合框编辑 JTable 中某个列的单元格的内容。因此,我只需使用 JTables 上的官方 Java 教程 (http://download.oracle.com/javase/tutorial/uiswing/components/table.html) 中的代码。 相关代码最终如下所示:

tableModel = new NetworkTableModel(columnNames,0,0);
networkTable = new JTable(tableModel);
....
TableColumn sendingColumn = networkTable.getColumnModel().getColumn(3);  
JComboBox sendingBox = new JComboBox();
sendingBox.addItem("Sending");
sendingBox.addItem("Receiving");
sendingColumn.setCellEditor(new DefaultCellEditor(sendingBox));

问题是当我单击该列中的单元格时,组合框不会出现。我完全能够编辑单元格,但只能通过输入它们。为什么 ComboBox 不显示?

【问题讨论】:

    标签: jtable jcombobox


    【解决方案1】:

    问题解决了。

    虽然我没有经验,但当基础数据发生变化时,我总是为我的表设置一个完整的新表模型(更准确地说是表示表中一行的对象数量)。这就是为什么除了我创建的表模型的第一个实例之外的所有组合框都没有显示,因为我只在这个实例上调用了 setCellEditor 方法。将我的程序更改为不总是创建新的表模型,现在一切正常......学到了一些东西。

    【讨论】:

      猜你喜欢
      • 2016-08-17
      • 1970-01-01
      • 2017-01-02
      • 1970-01-01
      • 1970-01-01
      • 2020-06-08
      • 1970-01-01
      • 1970-01-01
      • 2013-02-08
      相关资源
      最近更新 更多