Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2003

Tip: Looking for answers? Try searching our database.

Custom ComboBox Editor in JTable focus issue

Thread view: 
John Wheeler - 26 Nov 2003 00:56 GMT
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?
Kleopatra - 26 Nov 2003 09:49 GMT
> I install combo boxes with these KeySelectionManagers as JTable
> CellEditors. When the JTable passes focus to the component, I type one
[quoted text clipped - 3 lines]
> 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
John Wheeler - 26 Nov 2003 17:58 GMT
thanks for your insight on the protocol. I'm looking into it now.
John Wheeler - 26 Nov 2003 19:25 GMT
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);
   }
Kleopatra - 27 Nov 2003 15:06 GMT
> I created a table cell editor that extends JComboBox and implements
> TableCellEditor. It's constructor installs my custom key selection
[quoted text clipped - 4 lines]
> 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


>     public Component getTableCellEditorComponent(JTable table, Object
> value,
[quoted text clipped - 7 lines]
>         table.setRowSelectionInterval(row, row);
>         table.setColumnSelectionInterval(column, column);

    ^^^^^^^^

NEVER do such a thing ... you can get very nasty effects by doing so.

Greetings
Jeanette


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.