Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable cell formatting

 
ryan headley
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • 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
 
jeremy hull
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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.
 
Sanjay Bahrani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic