开发者

Detecting Selection Changes in a JTable

开发者 https://www.devze.com 2023-02-20 08:37 出处:网络
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 t开发者_运维问答hat you need to somehow use a TableColumnModelListener

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 t开发者_运维问答hat 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


If by "change" you mean changing the value of a cell then you can use an AbstractTableModel and implement the fireTableCellUpdated method

0

精彩评论

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