This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Transfer focus to inside JComboBox from JTable  RSS feed

 
rajesh chalavadi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

my swing code looks like..

JTable table = new JTable(tm)
TableColumn sportColumn = table.getColumnModel().getColumn(2);
sportColumn.setCellEditor(new CustomCellEditor(args));

======

In CustomCellEdtior.java (extends AbstractCellEditor implements TableCellEditor)

public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column)
{
// Depens on row, column value, combobox will be created.
JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Chasing toddlers");
comboBox.addItem("Speed reading");
comboBox.addItem("Teaching high school");
comboBox.addItem("None");
return comboBox;
}

Now am trying to transfer the focus from jtable to insides jcomboBox when ever focus comes to 2nd column using a focus listener on jtable.

table.addFocusListener(new FocusListener()
{
public void focusGained(FocusEvent e)
{
System.out.println("got the focus on jtable");
JTable table = (JTable)e.getComponent();
int row = table.getSelectionModel().getAnchorSelectionIndex();
int col = table.getColumnModel().
getSelectionModel().getAnchorSelectionIndex();
System.out.println("row & column " + row + "-" + col);
if ( col == 2 )
{
Component comp =
table.prepareEditor(table.getCellEditor(row, col), row, col);
comp.transferFocus();
}
}

But, i guess, am doing some thing wrong in the above code. Focus is not coming to JcomboBox. Could any one help me in this issue?

I hope am clear in above usecase.
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rajesh chalavadi:
Component comp =
table.prepareEditor(table.getCellEditor(row, col), row, col);
comp.transferFocus();


That's not really what you want to do, I don't think. Instead try something like

boolean ok = table.editCellAt(row, col);
// optional: show the combo box's popup
Component comp = table.getEditorComponent();
if (ok && comp instanceof JComboBox) ((JComboBox)comp).setPopupVisible(true);


While I'm here, I'm not wild about the way you've implemented your cell editor. Even if the combo box's items need to be dynamic, I'm not sure it's a good idea to instantiate a new instance of JComboBox on each call to getTableCellEditorComponent(). I would suggest creating a single JComboBox in the editor's constructor. Don't you sort of need to do that to implement getCellEditorValue() anyway? (Depending on the circumstances, you may be able to use DefaultCellEditor instead. It has a constructor that takes a JComboBox.)

[edit: now checking editCellAt()'s return value]
[ February 13, 2008: Message edited by: Brian Cole ]
 
rajesh chalavadi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Brian for the response.

I haven't get a chance to try your suggestion. Will try it today.
 
rajesh chalavadi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian, your solution seems working fine.

But the problem is - it is hard to track the focus between JTable cells. Actually I added focusListener to JTable. The FoucsGained() method on this listener is not getting called whenever I change the cell focus using the tab key!!

One solution could be, adding a key listener to JTable in FocusGained() method and tracking TAB keys.

I am wondering, Is there any other way to track the focus on each JTable cells?
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rajesh chalavadi:
But the problem is - it is hard to track the focus between JTable cells. Actually I added focusListener to JTable. The FoucsGained() method on this listener is not getting called whenever I change the cell focus using the tab key!!


You shouldn't expect focusGained() to be called in this situation, as the JTable remains the focused component.

One solution could be, adding a key listener to JTable in FocusGained() method and tracking TAB keys.

I am wondering, Is there any other way to track the focus on each JTable cells?


You could add a listener to the JTable's selection model. Cell selection isn't quite the same thing as cell focus, but depending on the situation it may be close enough.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!