简体   繁体   中英

Detecting Selection Changes in a JTable

I'm trying to find a way to detect changes in which column the user selected in a JTable. I did some poking around and it appears that you need to somehow use a TableColumnModelListener in order to detect the changes, but that doesn't seem to fire an event when you change the column you have selected.

You need to add a ListSelectionListener instead. That will capture selection events. Here are some Swing tutorials that go further in depth:

http://download.oracle.com/javase/tutorial/uiswing/events/listselectionlistener.html http://download.oracle.com/javase/tutorial/uiswing/components/table.html#selection

From what I read, I think you need to add a MouseListener to your table, which for example in mouseClicked will get the row and column using the following code, below:



table.addMouseListener(new MouseListener()
{
    @Override
    public void mouseClicked(MouseEvent e)
    {   
       Point pnt = evt.getPoint();
       int row = table.rowAtPoint(pnt);
       int col = table.columnAtPoint(pnt);
    }
}

It should work great for you I have used similar thing myself before. BTW it look similar to the problem I found on coderanch, link: http://www.coderanch.com/t/332737/GUI/java/detect-single-click-any-cell

Good luck, Boro

如果通过“更改”表示更改单元格的值,则可以使用AbstractTableModel并实现fireTableCellUpdated方法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM