Java单元格编辑后如何维护JTable单元格渲染

Java单元格编辑后如何维护JTable单元格渲染,第1张

Java单元格编辑后如何维护JTable单元格渲染

当你的编辑器结束时,表的

editingStopped()
方法通过收集新值
getCellEditorValue()
并将其用于
setValueAt()
模型中。反过来,该模型应
fireTableCellUpdated()
调用指定的渲染器。扩展默认值应该足以处理Number格式化。在其他情况下,将渲染器的实例用作编辑器组件可能会很方便;这个例子展示了一个典型的实现。

附录:这是一个使用默认编辑器和渲染器实现的基本示例

附录:感谢@mKorbel的有用评论,我更新了示例,以选择要编辑的单元格文本,如@camickr的文章Table Select All Editor中所述。

package overflow;import java.awt.Component;import java.awt.Dimension;import java.awt.EventQueue;import java.awt.event.MouseEvent;import java.text.NumberFormat;import java.util.EventObject;import javax.swing.DefaultCellEditor;import javax.swing.Jframe;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.table.DefaultTableCellRenderer;import javax.swing.table.DefaultTableModel;import javax.swing.text.JTextComponent;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;     }     if (e instanceof MouseEvent) {         EventQueue.invokeLater(new Runnable() {  @Override  public void run() {      ((JTextComponent) editor).selectAll();  }         });     } else {         ((JTextComponent) editor).selectAll();     }     return result; }        };        table.setPreferredScrollableViewportSize(new Dimension(123, 123));        table.setDefaultRenderer(Double.class, new CurrencyRenderer(nf));        table.setDefaultEditor(Double.class, new CurrencyEditor(nf));        this.add(new JScrollPane(table));    }    private static class CurrencyRenderer extends DefaultTableCellRenderer {        private NumberFormat formatter;        public CurrencyRenderer(NumberFormat formatter) { this.formatter = formatter; this.setHorizontalAlignment(JLabel.RIGHT);        }        @Override        public void setValue(Object value) { setText((value == null) ? "" : formatter.format(value));        }    }    private static class CurrencyEditor extends DefaultCellEditor {        private NumberFormat formatter;        private JTextField textField;        public CurrencyEditor(NumberFormat formatter) { super(new JTextField()); this.formatter = formatter; this.textField = (JTextField) this.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;        }    }    private void display() {        Jframe f = new Jframe("RenderEditNumber");        f.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);        f.add(this);        f.pack();        f.setLocationRelativeTo(null);        f.setVisible(true);    }    public static void main(String[] args) {        EventQueue.invokeLater(new Runnable() { @Override public void run() {     new RenderEditNumber().display(); }        });    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5462389.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存