Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #4270

JSpinners as JTable cells

From "Rexx Magnus" <rexx.magnus@THRWHITE.remove-dii-this>
Subject JSpinners as JTable cells
Message-ID <op.uh8q60x1p9vcmo@macmini.local> (permalink)
Newsgroups comp.lang.java.gui
Date 2011-04-27 15:49 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui
I've been writing an application for the past several weeks that has a  =

JTable that uses JSpinners in one column.
I discovered that I was having problems with the TableModel not updating=
  =

whenever a spinner was edited using only the buttons.
Editing with the text field was not a problem - but if you changed a val=
ue  =

using the buttons, you had to click in a different cell of the table  =

before the model would update.
Evidently, this was a focus problem. JSpinner buttons do not ordinarily =
 =

seem to get focus within a table - the text part can, but the buttons  =

don't.

After following numerous posts on forums etc. I stumbled across one  =

suggestion to use a custom UI for the spinner - however, this relied on =
 =

the plaf.basic look and feel, which means that if you change the UI in  =

order to add focuslisteners as you build it, the buttons may become a  =

different look and feel whilst you edit the values.
This wasn't very nice on the Mac, I can tell you!

I didn't really have much of a clue as to what I was doing - but I  =

definately didn't want to rebuild the UI from scratch, so I dug down int=
o  =

the JSpinner's component list and applied focuslisteners to each. This  =

appears to fix the problem completely - something which no other solutio=
n  =

on the net had seemed to do without requiring a total program rewrite.


  public class SpinnerEditor extends AbstractCellEditor
             implements TableCellEditor {

         final JSpinner spinner;
         private JTable currentTable;
         private int selectedRow;
         private int selectedColumn;
         // Initializes the spinner.
         public SpinnerEditor(int min, int max) {
             spinner =3D new JSpinner(new SpinnerNumberModel(min, min, m=
ax,  =

1));
             spinner.setFocusable(true);//This alone does not fix the is=
sue

             //List all of the components and make them focusable
             //then add an empty focuslistener to each
             for(Component tmpComponent:spinner.getComponents()){
                 tmpComponent.setFocusable(true);
                 tmpComponent.addFocusListener(new FocusAdapter(){
                 @Override
                 public void focusLost(FocusEvent fe){
                 }});
             }
         }

         public Component getTableCellEditorComponent(JTable table, Obje=
ct  =

value,
                 boolean isSelected, int row, int column) {
             spinner.setValue(value);
             currentTable =3D table;
             selectedRow =3D row;
             selectedColumn =3D column;
             return spinner;
         }

         public Object getCellEditorValue() {
             return spinner.getValue();
         }
     }

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextNext in thread | Find similar


Thread

JSpinners as JTable cells "Rexx Magnus" <rexx.magnus@THRWHITE.remove-dii-this> - 2011-04-27 15:49 +0000
  Re: JSpinners as JTable c "John B. Matthews" <john.b..matthews@THRWHITE.remove-dii-this> - 2011-04-27 15:49 +0000
    Re: JSpinners as JTable c "Rexx Magnus" <rexx.magnus@THRWHITE.remove-dii-this> - 2011-04-27 15:49 +0000

csiph-web