• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

how to get column header click event on a JTable

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You save more money with a clothesline than dozens of light bulb purchases. Tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic