Home > Java > javaTutorial > How to Maintain Custom JTable Cell Rendering After Editing?

How to Maintain Custom JTable Cell Rendering After Editing?

DDD
Release: 2024-12-06 07:17:11
Original
538 people have browsed it

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;
    }
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template