JTable 单元格中的 JComboBox 未显示

发布于 2024-09-30 06:38:16 字数 600 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉生梦死 2024-10-07 06:38:16

问题解决了。

尽管我缺乏经验,但当基础数据发生变化时,我总是为我的表设置一个完整的新表模型(更精确的是表示表中一行的对象数量)。这就是为什么除我创建的表模型的第一个实例之外的所有实例都没有显示组合框的原因,因为我只在此实例上调用了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文