Render Edit Number: Maintain JTable Format After Cell Editing
When extending a JTable with custom cell rendering, it's essential to maintain that rendering even after cell editing. This includes preserving formatting within the cell renderer.
Problem:
After implementing a JTextField editor for a JTable column formatted with a custom cell renderer, the cell loses its custom rendering upon edit completion.
Solution:
The loss of formatting occurs because the model does not update the renderer upon cell value change. To fix this, ensure that the cell renderer is called after any table model changes.
Implementation:
Example Code:
The example code provided demonstrates this approach by creating a JTable with a currency-formatted column that maintains formatting after cell editing:
import javax.swing.*; import java.awt.*; import java.awt.event.MouseEvent; import java.text.NumberFormat; import java.util.EventObject; public class RenderEditNumber extends JPanel { private NumberFormat nf = NumberFormat.getCurrencyInstance(); public RenderEditNumber() { DefaultTableModel model = new DefaultTableModel( new String[]{"Amount"}, 0) { @Override public Class<?> getColumnClass(int columnIndex) { return Double.class; } }; for (int i = 0; i < 16; i++) { model.addRow(new Object[]{Double.valueOf(i)}); } JTable table = new JTable(model) { @Override // Always selectAll() public boolean editCellAt(int row, int column, EventObject e) { boolean result = super.editCellAt(row, column, e); final Component editor = getEditorComponent(); if (editor == null || !(editor instanceof JTextComponent)) { return result; } EventQueue.invokeLater(new Runnable() { @Override public void run() { ((JTextComponent) editor).selectAll(); } }); return result; } }; table.setDefaultRenderer(Double.class, new CurrencyRenderer(nf)); table.setDefaultEditor(Double.class, new CurrencyEditor(nf)); add(new JScrollPane(table)); } public static void main(String[] args) { EventQueue.invokeLater(() -> new RenderEditNumber().display()); } } class CurrencyRenderer extends DefaultTableCellRenderer { private NumberFormat formatter; public CurrencyRenderer(NumberFormat formatter) { this.formatter = formatter; setHorizontalAlignment(JLabel.RIGHT); } @Override public void setValue(Object value) { setText((value == null) ? "" : formatter.format(value)); } } class CurrencyEditor extends DefaultCellEditor { private NumberFormat formatter; private JTextField textField; public CurrencyEditor(NumberFormat formatter) { super(new JTextField()); this.formatter = formatter; textField = (JTextField) getComponent(); textField.setHorizontalAlignment(JTextField.RIGHT); textField.setBorder(null); } @Override public Object getCellEditorValue() { try { return new Double(textField.getText()); } catch (NumberFormatException e) { return Double.valueOf(0); } } @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { textField.setText((value == null) ? "" : formatter.format((Double) value)); return textField; } }
By utilizing this approach, JTable cells maintain their customized rendering even after being edited, allowing for consistent data presentation and improved user experience.
The above is the detailed content of How to Maintain Custom JTable Cell Rendering After Editing?. For more information, please follow other related articles on the PHP Chinese website!