JDK-6210276 : A JSpinner editor created for JTable with terminateEditOnFocusLost draws wrong.
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-12-17
  • Updated: 2010-04-02
  • Resolved: 2004-12-20
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
jdk1.5

ADDITIONAL OS VERSION INFORMATION :
WindowsXP, although it probably affects others as well.

A DESCRIPTION OF THE PROBLEM :
JSpinner doesn't function correctly when used as an editor for a JTable that terminates edits when it loses focus.

For many tables, you want editing to stop when the user moves focus from the table onto another component on the same panel. To do this, you have to call JTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE).

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the given source code.

1. Click on the top cell - the JSpinner cell editor shows up.
2. Click on the text box.
3. Click back on the same top cell - notice that where the up/down buttons should be drawn, instead a box is drawn with the same color as the "highlight" color for the table. If you move the mouse over that area, the up/down spinner buttons will show up again.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The up/down spinner buttons should be repainted properly.
ACTUAL -
A box the color of the tables highligh color is drawn instead of the up/down buttons.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class TestSpinnerEditorProblem {
    public static void main(String[] args) {
        JTable table = new JTable(2, 3);

        // *** This is the line that turns on the problem. ***
        table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

        // Set up the table
        table.getColumnModel().getColumn(0).setCellEditor(new
                NumberSpinnerTableCellEditor());
        table.getModel().setValueAt(new Integer(0), 0, 0);
        table.getModel().setValueAt(new Integer(0), 1, 0);
        JScrollPane scrollPane = new JScrollPane(table);

        // Setup up the text field area
        JTextField textField = new JTextField(30);
        JLabel textFieldLabel = new JLabel("Text Field: ");
        JPanel textFieldPanel = new JPanel(new FlowLayout());
        textFieldPanel.add(textFieldLabel);
        textFieldPanel.add(textField);

        // Put the areas on one panel
        JPanel contentPane = new JPanel(new BorderLayout());
        contentPane.add(textFieldPanel, BorderLayout.NORTH);
        contentPane.add(scrollPane, BorderLayout.CENTER);

        // Show the frame
        JFrame frame = new JFrame("Spinner Editor Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(contentPane);
        frame.pack();
        frame.show();
    }

    // The "Spinner" editor for the table.
    private static class NumberSpinnerTableCellEditor extends
            AbstractCellEditor implements TableCellEditor {

        private JSpinner editor;

        public NumberSpinnerTableCellEditor() {
            editor = new JSpinner();
            editor.setBorder(null);
        }

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

        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int column) {

            // Uncomment this line and it will display correctly.
            //((AbstractTableModel) table.getModel()).fireTableCellUpdated(row, column);

            editor.setValue(value);

            return editor;
        }
    }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Anything that causes the table cell to be redrawn each time the editor is used.

The best way I found was to customize the spinner editor to add this line:
((AbstractTableModel) table.getModel()).fireTableCellUpdated(row, column);

to the getTableCellEditorComponent() method of your custom cell editor.

which forces the cell to be redrawn each time the editor is requested. The "fix" is a line commented out in the sample code.
###@###.### 2004-12-17 23:02:36 GMT

Comments
EVALUATION This is a duplicate of 4778542 which I've just found the fix for. Will apply the fix shortly. ###@###.### 2004-12-20 20:17:24 GMT
20-12-2004