选择下一个单元格 JTable

发布于 2024-08-04 17:17:51 字数 104 浏览 2 评论 0原文

我想制作一个 jTable,其中当用户选择不可编辑的单元格时,它应该自动将焦点更改到下一个可编辑的单元格。重要提示:用户可以通过键盘(选项卡或箭头)和鼠标单击来选择单元格。是否可以??怎么去呢?

I would like to make a jTable in which when user select an uneditable cell then it should change focus to the next editable cell automatically. Important: the user could select a cell by keyboard (tab or arrow) and by mouse clicking. Is it possible?? How to to it?

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

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

发布评论

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

评论(2

诠释孤独 2024-08-11 17:17:51

此链接详细介绍了以编程方式在 JTable 组件中进行选择;你必须将鼠标监听器/等链接起来才能解决这个问题。

This link details Programmatically Making Selections in a JTable Component; you'd have to have mouselisteners/etc chained to work off this.

南城追梦 2024-08-11 17:17:51

表格选项卡展示了如何使用键盘进行操作。

我从未尝试过,但当您单击单元格时,您应该能够使用 MouseListener 来调用相同的操作。

刚刚对 MouseListener 进行了快速测试,它似乎工作正常:

JTable table = new JTable(...);
final EditableCellFocusAction action = 
    new EditableCellFocusAction(table, KeyStroke.getKeyStroke("TAB"));

MouseListener ml = new MouseAdapter()
{
    public void mouseReleased(MouseEvent e)
    {
        JTable table = (JTable)e.getSource();
        int row = table.rowAtPoint(e.getPoint());
        int column = table.columnAtPoint(e.getPoint());

        if (! table.isCellEditable(row, column))
        {
                ActionEvent event = new ActionEvent(
                    table,
                    ActionEvent.ACTION_PERFORMED,
                    "");
                action.actionPerformed(event);
        }
    }
};
table.addMouseListener(ml);

Table Tabbing shows how you can do it with the keyboard.

I've never tried it but you should be able to use a MouseListener to invoke the same Action when you click on a cell.

Just did a quick test for the MouseListener and it seems to work fine:

JTable table = new JTable(...);
final EditableCellFocusAction action = 
    new EditableCellFocusAction(table, KeyStroke.getKeyStroke("TAB"));

MouseListener ml = new MouseAdapter()
{
    public void mouseReleased(MouseEvent e)
    {
        JTable table = (JTable)e.getSource();
        int row = table.rowAtPoint(e.getPoint());
        int column = table.columnAtPoint(e.getPoint());

        if (! table.isCellEditable(row, column))
        {
                ActionEvent event = new ActionEvent(
                    table,
                    ActionEvent.ACTION_PERFORMED,
                    "");
                action.actionPerformed(event);
        }
    }
};
table.addMouseListener(ml);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文