当按下 Enter 键时,如何确定选择了 JTable 的哪一部分?

发布于 2024-10-24 05:11:22 字数 79 浏览 2 评论 0原文

我有一个JTable。我想知道当用户按 Enter 时选择了哪行和哪列。我怎样才能得到这些信息?

I have a JTable. I want to know which row and column are selected when the user presses Enter. How can I get this information?

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

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

发布评论

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

评论(3

一抹淡然 2024-10-31 05:11:22

实现 TableModelListener。 tableChanged() 中的 TableModelEvent方法将告诉您更改的来源是哪行和哪列。

Implmenent a TableModelListener. The TableModelEvent from the tableChanged() method will tell you what row and column was the source of the change.

无敌元气妹 2024-10-31 05:11:22

所有 Swing 组件都使用操作来处理击键。 Enter 键的默认操作是将单元格选择向下移动一行。如果您想更改此行为,则需要将默认操作替换为自定义操作。

查看按键绑定,了解如何替换操作的简单说明。

All Swing components use Actions to handle key strokes. The default Action for the Enter key is to move the cell selection down one row. If you want to change this behaviour then you need to replace the default Action with a custom Action.

Check out Key Bindings for a simple explanation on how to replace an Action.

滥情稳全场 2024-10-31 05:11:22

将其添加到您的表中。对于 rowClickedcolClicked 有两个 int 全局变量。应该可以了


        table.addMouseListener(new MouseAdapter(){
            public void mouseReleased(MouseEvent e)
            {
                rowClicked = rowAtPoint(e.getPoint());
                colClicked = columnAtPoint(e.getPoint());
            }

            public void mouseClicked(MouseEvent e)
            {
                rowClicked = rowAtPoint(e.getPoint());
                colClicked = columnAtPoint(e.getPoint());
            }
        });

如果您谈论使用键盘来注册事件,您必须找到选定的单元格,然后向其添加一个 KeyListener 。您可以使用以下代码来查找所选单元格。请注意,这实际上取决于单元选择模式。


public void getSelectedCells()
    {
        if (getColumnSelectionAllowed() && ! getRowSelectionAllowed())
        {
            // Column selection is enabled
            // Get the indices of the selected columns
            int[] vColIndices = getSelectedColumns();
        }
        else if (!getColumnSelectionAllowed() && getRowSelectionAllowed())
        {
            // Row selection is enabled
            // Get the indices of the selected rows
            int[] rowIndices = getSelectedRows();
        }
        else if (getCellSelectionEnabled())
        {
            // Individual cell selection is enabled

            // In SINGLE_SELECTION mode, the selected cell can be retrieved using
            setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            int rowIndex = getSelectedRow();
            int colIndex = getSelectedColumn();

            // In the other modes, the set of selected cells can be retrieved using
            setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

            // Get the min and max ranges of selected cells
            int rowIndexStart = getSelectedRow();
            int rowIndexEnd = getSelectionModel().getMaxSelectionIndex();
            int colIndexStart = getSelectedColumn();
            int colIndexEnd = getColumnModel().getSelectionModel().getMaxSelectionIndex();

            // Check each cell in the range
            for (int r = rowIndexStart; r < = rowIndexEnd; r++)
            {
                for (int c = colIndexStart; c < = colIndexEnd; c++)
                {
                    if (isCellSelected(r, c))
                    {
                        // cell is selected
                    }
                }
            }
        }
    }

Add this to your table. Have two int globals for rowClicked and colClicked. Should be good to go


        table.addMouseListener(new MouseAdapter(){
            public void mouseReleased(MouseEvent e)
            {
                rowClicked = rowAtPoint(e.getPoint());
                colClicked = columnAtPoint(e.getPoint());
            }

            public void mouseClicked(MouseEvent e)
            {
                rowClicked = rowAtPoint(e.getPoint());
                colClicked = columnAtPoint(e.getPoint());
            }
        });

If you talking about using the keyboard to register the event, you must find the selected cell, then add a KeyListener to it. You can use the following code to find the selected cell. Note that it really depends on the cell selection mode.


public void getSelectedCells()
    {
        if (getColumnSelectionAllowed() && ! getRowSelectionAllowed())
        {
            // Column selection is enabled
            // Get the indices of the selected columns
            int[] vColIndices = getSelectedColumns();
        }
        else if (!getColumnSelectionAllowed() && getRowSelectionAllowed())
        {
            // Row selection is enabled
            // Get the indices of the selected rows
            int[] rowIndices = getSelectedRows();
        }
        else if (getCellSelectionEnabled())
        {
            // Individual cell selection is enabled

            // In SINGLE_SELECTION mode, the selected cell can be retrieved using
            setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            int rowIndex = getSelectedRow();
            int colIndex = getSelectedColumn();

            // In the other modes, the set of selected cells can be retrieved using
            setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

            // Get the min and max ranges of selected cells
            int rowIndexStart = getSelectedRow();
            int rowIndexEnd = getSelectionModel().getMaxSelectionIndex();
            int colIndexStart = getSelectedColumn();
            int colIndexEnd = getColumnModel().getSelectionModel().getMaxSelectionIndex();

            // Check each cell in the range
            for (int r = rowIndexStart; r < = rowIndexEnd; r++)
            {
                for (int c = colIndexStart; c < = colIndexEnd; c++)
                {
                    if (isCellSelected(r, c))
                    {
                        // cell is selected
                    }
                }
            }
        }
    }

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