 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
John Wheeler Guest
|
Posted: Wed Nov 26, 2003 12:56 am Post subject: Custom ComboBox Editor in JTable focus issue |
|
|
I created a custom JComboBox.KeySelectionManager that allows
progressive key selection. For instance, when installed in a combo box
populated with U.S. states, a user can type 'ar' and the combo box
will select 'Arizona' instead of 'Rhode Island.'
I install combo boxes with these KeySelectionManagers as JTable
CellEditors. When the JTable passes focus to the component, I type one
key, editing is stopped, and focus returns to the table (BTW, I have
setSurrendersFocusOnKeystroke(true)). Consequently, the
KeySelectionManager's behavior is not realized. In order to intercept
calls to editingStopped, I created a subclass of JTable with an
overridden editingStopped method that installs KeyAdapter's on the
combo boxes and when 'tab' is pressed, i call super.editingStopped
(code below). Now the combo boxes retain focus, but mouse events do
not stop editing like they used to. So, if I begin editing in a combo
box and click another cell, the editing session doesn't stop.
public void editingStopped(final ChangeEvent ce) {
final DefaultCellEditor dce = (DefaultCellEditor)
ce.getSource();
if (!(dce.getComponent() instanceof JComboBox)) {
super.editingStopped(ce);
return;
}
dce.getComponent().addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
if (ke.getKeyChar() == KeyEvent.VK_TAB)
TxTable.super.editingStopped(ce);
}
});
}
Can anyone help?
|
|
| Back to top |
|
 |
Kleopatra Guest
|
Posted: Wed Nov 26, 2003 9:49 am Post subject: Re: Custom ComboBox Editor in JTable focus issue |
|
|
John Wheeler wrote:
| Quote: |
I install combo boxes with these KeySelectionManagers as JTable
CellEditors. When the JTable passes focus to the component, I type one
key, editing is stopped, and focus returns to the table (BTW, I have
setSurrendersFocusOnKeystroke(true)). Consequently, the
KeySelectionManager's behavior is not realized. In order to intercept
calls to editingStopped, I created a subclass of JTable with an
overridden editingStopped method that installs KeyAdapter's on the
|
don't know the solution to your problem - but overriding JTable's
editingStopped is definitely the wrong place: that's the callback method
used by the cellEditor to notify CellEditorListeners that the editing
has stopped.
What you need is a custom cellEditor that has a adjusted strategy to
decide which actions on the custom combo should be interpreted as
editing stopped - by default it simply listens to actionEvents which
might be fired by your combo while still selecting. Additionally you
might have to fiddle with the combo and/or the combo's uidelegate as
well, because it does some nasty things (highly version dependent) if it
detects the combo to be used as editorcomponent in a JTable.
I hate to be so vague - but making JComboBox and JTable work together
smoothly is tricky...
Greetings
Jeanette
|
|
| Back to top |
|
 |
John Wheeler Guest
|
Posted: Wed Nov 26, 2003 5:58 pm Post subject: Re: Custom ComboBox Editor in JTable focus issue |
|
|
thanks for your insight on the protocol. I'm looking into it now.
|
|
| Back to top |
|
 |
John Wheeler Guest
|
Posted: Wed Nov 26, 2003 7:25 pm Post subject: Re: Custom ComboBox Editor in JTable focus issue |
|
|
I created a table cell editor that extends JComboBox and implements
TableCellEditor. It's constructor installs my custom key selection
manager (I followed the JScrollBar sample from Chapter 15 of Eckstein,
Loy, and Wood) . I got what I wanted, but I don't know why. my
fireEditingStopped implementation is similar to
AbstractTableCellEditor's implementation. I guess I don't understand
the protocol these components use, but I am satisfied with how things
are going. Are you aware of any ramifications I should know about?
public class AshtonComboBoxTableCellEditor extends JComboBox
implements TableCellEditor {
protected transient Vector listeners;
protected transient Object origValue;
public AshtonComboBoxTableCellEditor(Vector parts) {
setKeySelectionManager(new AshtonComboBoxKeySM());
listeners = new Vector();
for (int i=0, n=parts.size(); i<n; i++)
addItem(parts.get(i));
}
public Component getTableCellEditorComponent(JTable table, Object
value,
boolean isSelected,
int row, int column)
{
if (value == null)
return this;
setSelectedItem(value);
table.setRowSelectionInterval(row, row);
table.setColumnSelectionInterval(column, column);
origValue = getSelectedItem();
return this;
}
public Object getCellEditorValue() {
return getSelectedItem();
}
public boolean isCellEditable(EventObject anEvent) {
return true;
}
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
public boolean stopCellEditing() {
fireEditingStopped();
return true;
}
public void cancelCellEditing() {
fireEditingCancelled();
}
public void addCellEditorListener(CellEditorListener l) {
listeners.addElement(l);
}
public void removeCellEditorListener(CellEditorListener l) {
listeners.removeElement(l);
}
protected void fireEditingCancelled() {
setSelectedItem(origValue);
ChangeEvent ce = new ChangeEvent(this);
for (int i=listeners.size()-1; i>=0; i--)
((CellEditorListener)listeners.elementAt(i)).editingCanceled(ce);
}
protected void fireEditingStopped() {
ChangeEvent ce = new ChangeEvent(this);
for (int i=listeners.size()-1; i>=0; i--)
((CellEditorListener)listeners.elementAt(i)).editingStopped(ce);
}
|
|
| Back to top |
|
 |
Kleopatra Guest
|
Posted: Thu Nov 27, 2003 3:06 pm Post subject: Re: Custom ComboBox Editor in JTable focus issue |
|
|
John Wheeler wrote:
| Quote: |
I created a table cell editor that extends JComboBox and implements
TableCellEditor. It's constructor installs my custom key selection
manager (I followed the JScrollBar sample from Chapter 15 of Eckstein,
Loy, and Wood) . I got what I wanted, but I don't know why. my
fireEditingStopped implementation is similar to
AbstractTableCellEditor's implementation. I guess I don't understand
the protocol these components use, but I am satisfied with how things
are going. Are you aware of any ramifications I should know about?
|
the main difference is that your custom cellEditor is not terminating
edits without explicite requests from some client code - the default is
listening to actionEvents fired by the combo - which may be okay in your
case. Don't know for sure.
Something unrelated: your code can alter the state of the table during
editing
| Quote: | public Component getTableCellEditorComponent(JTable table, Object
value,
boolean isSelected,
int row, int column)
{
if (value == null)
return this;
setSelectedItem(value);
table.setRowSelectionInterval(row, row);
table.setColumnSelectionInterval(column, column);
|
^^^^^^^^
NEVER do such a thing ... you can get very nasty effects by doing so.
Greetings
Jeanette
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|