JTable 单元格中的 JComboBox 未显示
我正在尝试使用 ComboBox 编辑 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));
问题是当我单击该列中的单元格时,组合框不会出现。我完全能够编辑单元格,但只能通过键入内容来编辑。为什么组合框不显示?
i'm trying to edit the content of the cells of a certain column in my JTable with ComboBoxes. Therefore i simply use the code from the official Java Tutorial on JTables (http://download.oracle.com/javase/tutorial/uiswing/components/table.html).
The relevant code ends up looking like this:
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));
The Problem is when i click on a cell in that column the ComboBox doesn't appear. I am perfectly able to edit the cells but only by typing into them. Why is the ComboBox not showing up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题解决了。
尽管我缺乏经验,但当基础数据发生变化时,我总是为我的表设置一个完整的新表模型(更精确的是表示表中一行的对象数量)。这就是为什么除我创建的表模型的第一个实例之外的所有实例都没有显示组合框的原因,因为我只在此实例上调用了 setCellEditor 方法。将我的程序更改为不总是创建新的表模型,现在一切正常......学到了一些东西。
Problem Solved.
Unexperienced as I am, i did always set a complete new table model for my table when the underlying data changed (more precise the number of objects representing a row in the table). thats why the combo boxes didn't show up for all but the first instance of the table model i created, because i only called the setCellEditor method on this instance. Changed my program to not always creating a new table model and everything works fine now....learned something.