How to Maintain Custom JTable Cell Rendering After Editing?
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:
- Extend the default renderer to handle the desired formatting (e.g., CurrencyRenderer).
- Extend the default editor (e.g., CurrencyEditor) and implement the getCellEditorValue() and getTableCellEditorComponent() methods for editing and rendering the value.
- Override the getTableCellEditorComponent() method in the table (e.g., editingStopped()) to ensure text selection in the editor.
- In the example provided, the CurrencyRenderer and CurrencyEditor classes handle currency formatting in a JTable. Upon cell edit, the value is parsed, formatted, and rendered correctly.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...
