JSpinners as JTable cells - a solution

R

Rexx Magnus

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 value
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 into
the JSpinner's component list and applied focuslisteners to each. This
appears to fix the problem completely - something which no other solution
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 = new JSpinner(new SpinnerNumberModel(min, min, max,
1));
spinner.setFocusable(true);//This alone does not fix the issue

//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, Object
value,
boolean isSelected, int row, int column) {
spinner.setValue(value);
currentTable = table;
selectedRow = row;
selectedColumn = column;
return spinner;
}

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top