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

Focus traversal problem in JTable  RSS feed

 
ashok rajendran
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a check box and a JTable in my swing app. This JTable cells will become a JComboBox when user wants to edit the value. User can edit the cell either by clicking on that or by pressing F2 and UP/DOWN keys.
I want to navigate out from the JTable by pressing ctrl+tab.(Which is prescribed in sun java doc as well). So, when i press ctrl+tab from JTable the focus should come to the check box which is first component (and next component to JTable) in my app.
It does not work for me when my focus is on the JcomboBox. It works only when the focus is on the cell which is not in edit mode, i mean plain JTable cell. (If you press esc/tab key the JComboBox will disappear and your focus will be on plain JTable cell)
Please help me out to get my requirement.

Here is my sample app for you reference.
public class Frame1 extends JFrame
{
public Frame1()
{
super();
this.setLayout( null );
this.setSize( new Dimension(400, 300) );

JTextField ch = new JTextField();
ch.setVisible(true);
ch.setBounds(10, 10, 10, 10);
this.add(ch, null);

DefaultTableModel tmodel = new DefaultTableModel(3, 1);
tmodel.setValueAt("0 0 1",0,0);
tmodel.setValueAt("1 0 1",1,0);
tmodel.setValueAt("2 0 1",2,0);

JTable custLayersTable = new JTable(tmodel);
custLayersTable.getColumnModel().getColumn(0).
setCellEditor(new ComboEditor());
custLayersTable.setBounds(new Rectangle(40, 40, 280, 145));
this.add(custLayersTable, null);
}

public static void main(String[] args)
{
Frame1 a = new Frame1();
a.setVisible(true);
}
}

// This is the table cell editor which returns combo box to JTable.
final class ComboEditor extends AbstractCellEditor
implements TableCellEditor
{
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column)
{
Vector<String> layerValSet = new Vector<String>();
for(int i=0; i<3; i++)
layerValSet.add(row+" "+column+" "+i);
mComboModel = new DefaultComboBoxModel(layerValSet);
mComboModel.setSelectedItem(value);
mEditorComp = new JComboBox(mComboModel);
return mEditorComp;
}

public Object getCellEditorValue()
{
return mEditorComp.getSelectedItem();
}

private DefaultComboBoxModel mComboModel;
private JComboBox mEditorComp;
}
 
ashok rajendran
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I missed to inform the following in the post..
When i focus is on JComboBox pressing ctrl+tab does not take the focus to anywhere. The only change i can see after pressing ctrl+tab is that the focus lost from the JComboBox, but the JComboBox remains as it was before.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!