开发者

How can I figure out what part of a JTable is selected when Enter is pressed?

I have a JTable. 开发者_如何学PythonI want to know which row and column are selected when the user presses Enter. How can I get this information?


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


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.


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
                    }
                }
            }
        }
    }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜