Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting Selection Changes in a JTable

Tags:

java

swing

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.

like image 348
Jonathan Avatar asked Mar 27 '11 23:03

Jonathan


2 Answers

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

like image 106
jzd Avatar answered Oct 06 '22 00:10

jzd


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

like image 39
Boro Avatar answered Oct 06 '22 01:10

Boro