刷新 JTable 的 setCellEditor

发布于 2024-07-12 06:38:08 字数 848 浏览 3 评论 0原文

我正在寻找上一个问题的答案,并且有一个巧妙的想法来克服限制JTable。 我需要编辑器逐行不同,而 JTable 只能为每列处理一个编辑器。

所以我的想法是使用 MouseListener 来检查 JTable 上的行和列,并每次设置新的编辑器。

但是,再次调用 setCellEditor() 不会产生任何效果。 编辑器仍然是第一个确定的编辑器。 那么如何让“setCellEditor”对同一列再次起作用呢?

这是 MouseListener 中的代码。

public void mouseClicked(MouseEvent e) {
    int cols = resultTable.columnAtPoint(new Point(e.getX(), e.getY()));
    int rows = resultTable.rowAtPoint(new Point(e.getX(), e.getY()));
    StorageObject item = (StorageObject) resultTable.getModel().getValueAt(rows, cols);
    TableColumn col = resultTable.getColumnModel().getColumn(cols);
    col.setCellEditor(new MyComboBoxEditor(item.list));
}

I was looking for an answer for a previous question and had an ingenious idea to overcome a limit on JTable. I need the editor to be different on a row by row basis, whereas JTable can only handle a single editor for each column.

So my idea is to use a MouseListener to check the row and column on the JTable and set new editor each time.

But, calling setCellEditor() a second time do not have any effect. The editor remains to be the first one that was set. So how can I make "setCellEditor" work a second time for the same column?

Here's the code in MouseListener.

public void mouseClicked(MouseEvent e) {
    int cols = resultTable.columnAtPoint(new Point(e.getX(), e.getY()));
    int rows = resultTable.rowAtPoint(new Point(e.getX(), e.getY()));
    StorageObject item = (StorageObject) resultTable.getModel().getValueAt(rows, cols);
    TableColumn col = resultTable.getColumnModel().getColumn(cols);
    col.setCellEditor(new MyComboBoxEditor(item.list));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

自演自醉 2024-07-19 06:38:08

我不确定为什么你的代码不起作用(自从我完成 Swing 以来已经有一段时间了),但是你为什么不直接重写

public TableCellEditor getCellEditor(int row, int column)

On your JTable 呢? 维护要用于每一行的组合框的映射,并在重写方法中返回正确的组合框。

I'm not sure why your code isn't working (it's been a while since I've done Swing), but why don't you just override

public TableCellEditor getCellEditor(int row, int column)

On your JTable? Maintain a map of the combo boxes you want to use for each row and in your overriden method return the correct one.

黒涩兲箜 2024-07-19 06:38:08

我的理论是,当调用所有注册到 Table/TableCell 的鼠标侦听器时,默认情况下安装到 API 类的侦听器将在鼠标侦听器之前首先被调用。 这意味着导致编辑器被获取的事件将在您将其设置为其他编辑器之前发生。 有点像竞争条件,只是它实际上是在 API 源代码中的某处定义的...这是我天真的理论,我已经可以看到其中的一些漏洞,所以我的解决方案是:

Override JTable.getCellEditor(int row, int col)。 这允许您返回任何单元格所需的任何编辑器。

My theory is that when all the mouse listeners registered to the Table/TableCell are invoked, the ones installed to the API classes by default will be invoked first, before your mouse listener. This means the event causing the editor to be fetched will occur before you set it to a different one. Kind of like a race condition, only it's actually defined somewhere in the API source code... That's my naive theory and I can already see some holes in it, so on to my solution:

Override JTable.getCellEditor(int row, int col). This allows you to return whatever editor you want for any cell.

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