• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

JTable cell formatting

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way in a JTable to "merge" cells. In other words, for each label on the left side of the table I need to cells. Say I was making some sort of scheduling program.
For any specific time I would need whether or not its available, and the last name of the person they are to meet with, etc.
I suppose I could just say its booked and have them click to open another window for more info, but in that case how do I add a listener to that specific cell?
Thanx
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at a common approach to doing row oriented tables. You can have separate tables that are side-by-side, but line up, appearing to be one table. I have not done it myself, but have seen it done and it looks good, although it may be kinda slow.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
You want to add the listeners to each cell, yeah you can do this by using ListSelectionListener. You have to add this listener to both rows and columns of the table so that each cell can react to the listener. I am giving you the example for this.
Example:
code....
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionModel rowSelection=table.getSelectionModel();
rowSelection.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent le) {
try {
if(le.getValueIsAdjusting()) return;
ListSelectionModel lsm= (ListSelectionModel)le.getSource();
if (lsm.isSelectionEmpty()) {
//Nothing to Do
}
else {
table.setCellSelectionEnabled(true);
table.setRowSelectionAllowed(false);
selectedRow=lsm.getMinSelectionIndex();
}catch(NumberFormatException ex) {}
}
});
The above code is for only the row, same way you have to use for column also.
I have used this in Calendar(Swing application), when you select a date in a calendar (Dates resides in the cells of the table) it will give you the weekday for a particular date. I had added the Listener on each cell in this way....
Regards.
 
reply
    Bookmark Topic Watch Topic
  • New Topic