在我的上一个问题中,你们是如此棒的指向我正确的方向,我在这里有一些原始问题的延伸:
How to set a JTable column as String and sort as Double?
我现在通过使用我的自定义单元格渲染器将我的价格列格式化为$ ###,## 0.00,我现在也为该单元格设置了一个JTextField编辑器。编辑单元格工作正常,除了更新值时,我的自定义渲染器中设置的数字格式似乎不再格式化单元格(我在编辑提交后丢失了$)。即使在初始显示数据之后,此渲染器是否也不应渲染单元格?
我试图使用以下但没有运气:
((AbstractTableModel) table.getModel()).fireTableDataChanged();
我希望这会强制表格使用自定义渲染器重新验证和重新绘制单元格来渲染新值,但遗憾的是这不起作用...
我错过了什么......显然,但是什么?
答案 0 :(得分:10)
当您的编辑器结束时,表格的editingStopped()
方法会通过getCellEditorValue()
收集新值,并将其用于模型中的setValueAt()
。反过来,模型应该fireTableCellUpdated()
,它将调用规定的渲染器。扩展default应该足以处理Number
格式。在其他情况下,使用渲染器的实例作为编辑器组件可能会很方便;这个example显示了一个典型的实现。
附录:这是使用默认编辑器和渲染器实现的基本示例。
附录:感谢@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;
/** @see http://stackoverflow.com/a/10067560/230513 */
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();
}
});
}
}