Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Selective ui cell selection swing jtable  RSS feed

 
anuma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am having a table of 4 columns. While tabbing through, i want the columns 3,4 are to be selected and not the 1,2. How to do that?
Right now, i put the code in isCellEditable() method

but 1,2 column gets focus then transfer happens. Any solution for avoiding the 1,2 column tab movement.
thanx in advance.
 
Vinod Venugopal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think if you try to find out which column is selected using table.getSelectedColumn() & then if you are able to find out if its 1st or 2nd column then setfocus to the next column.
 
Laudney Ren
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this seemingly simple question requires a complex solution. I'll try to express clearly and succinctly.
The answer in short is that you should use different TableCellRenderer for each of your 4 columns.
javax.swing.JTable has a method:
void setDefaultRenderer(Class columnClass, TableCellRenderer cellRenderer)
As for the columnClass, you choose the argument according to the class type of data you intend to put in the cells of each of the 4 columns, such as Color.class.
As for cellRenderer, you should use javax.swing.table.DefaultTableCellRenderer, which extends from JLabel, which in turn extends from JComponent.
Well, JComponent has two methods we'll use here:
public boolean hasFocus()
public void setNextFocusableComponent(Component aComopnent)
When you set default cellrenderer for column 1 and 2, override the hasFocus() method by always returning false.
When you set default cellrenderer for column 3 and 4, override the hasFocus() method by always returning true and override the setNextFocusableComponent(Component aComopnent) method by returning the next cell you want to focus next.
I haven't tried this solution yet. If you encounter any problem, please post it immediately.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anuma,

Please re-register with a name that follows the JavaRanch Username Convention. User names are requires to be in the format "First name" + space + "Last Name".

Thanks,
-Nate
 
Anuradha Saravanamuthu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
It doesn't work out. The hasfocus and nextfocusable component overriding is having absolutely the same effect.
Any other clue to restrict the navigation in table for the selective columns alone? Is there any range set option.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!