I have a custom tree editor that contains two jtextfields. When I edit the first text field and press tab, the cell looses focus. The desired behavior is to have the focus shift to the second text field in the cell. I tried adding a key listener and set the focus on the second cell, but that doesn't work.
Code is attached below:
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.AbstractCellEditor;
import javax.swing.table.TableCellEditor;
/**
* <p>Title: Trigger Reset Editor</p>
* <p>Description: Custom editor for specifying the trigger and the
reset.</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: Peakstone Corporation</p>
* @author Kate Lapan Fox
* @version 2.0
* @todo tab moves from trigger to reset to stopping editing
* @todo validate the trigger and reset values (prevent wrong input)
*/
publicclass TriggerResetEditor extends AbstractCellEditor implements
TableCellEditor {
private Editor editor = new Editor();
/**
* Create the trigger reset editor
*/
public TriggerResetEditor() {
}
/**
* Get the value from the cell editor
*
* @return value
*/
public Object getCellEditorValue() {
return editor.getValue();
}
/**
* Sets an initial <code>value</code> for the editor.
* @param isSelected true if the cell is to be rendered with
* highlighting
* @param row the row of the cell being edited
* @param column the column of the cell being edited
* @return the component for editing
*/
public Component getTableCellEditorComponent(JTable table, Object
value,
boolean isSelected, int row, int column) {
editor.setValue((String)value);
return editor;
}
/**
* <p>Title: Editor</p>
* <p>Description: Displays two text fields separated by "/".</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: Peakstone Corporation</p>
* @author Kate Lapan Fox
* @version 2.0
*/
class Editor extends JPanel {
JTextField trigger=new JTextField(3), reset=new JTextField(3);
privatestaticfinal String SEPARATOR = "/";
privatestaticfinal String DISPLAYSEPARATOR = " / ";
privatefinal TabListener tabListener = new TabListener();
/**
* Create a new editor
*/
public Editor() {
setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
setBackground(Color.WHITE);
trigger.setToolTipText("Trigger");
trigger.addKeyListener(tabListener);
reset.setToolTipText("Reset");
add(trigger);
add(new JLabel(DISPLAYSEPARATOR));
add(reset);
}
/**
* Get the editor value
*
* @return value
*/
public String getValue() {
return trigger.getText() + SEPARATOR + reset.getText();
}
/**
* Set the editor value
*
* @param value
*/
publicvoid setValue(String value) {
String triggerValue = "";
String resetValue = "";
if (value != null && value.length() != 0) {
triggerValue = value.substring(0, value.indexOf(SEPARATOR));
resetValue = value.substring(value.indexOf(SEPARATOR)+1);
}
trigger.setText(triggerValue);
reset.setText(resetValue);
}
class TabListener extends KeyAdapter {
/**
* Invoked when a key has been pressed.
*
* @param e
*/
publicvoid keyPressed(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_TAB) {
System.out.println("tabpressed");
trigger.grabFocus();
}
}
}
}
}