• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get column header click event on a JTable

 
SLAZ IN
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am a beginner to JAVA. I have created a Jtable and added it to a JScrollpane. It is having 6 column headers.

I want to sort the contents of the table when we click on the column header.For that i want to know the event which is occurred on click.How can I know that on which column header the click is happened? What is the event that occurs on click on the column header?

The code written for creating the table is given below.

JTable mainTable = new JTable();
JScrollPane mainTableScrlPane = new JScrollPane();

DefaultTableModel mainTableModel = (new DefaultTableModel(new
Object [][] {}, new String [] {"Name","ID","Date","Time","Status","Physician"})
{
boolean[] canEdit = new boolean[]{false,false,false,false};
public boolean isCellEditable(int rowIndex,int colIndex)
{
return canEdit[colIndex];
}});

mainTable.setModel(mainTableModel);
mainTableScrlPane.getViewport().add(mainTable, null);

Please help me...
 
tito bayuadji
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

IMHO you can get table header component using
JTable.getTableHeader

then add a mouse click to that component.

good luck
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic