我不确定为什么没有调用我为 jtable 设置的编辑器。我使用了另一个SO Question中的编辑器示例。
当我编辑我的 jtable 时,它只是作为字符串编辑......我希望它只接受数字值。如果我输入任何其他文本可能会出现一些例外情况......但我认为编辑表格时不会在这里调用编辑器。
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.NumberFormatter;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.Locale;
public class Grow extends JFrame {
private static final Object[][] rowData = {{"Hello", "World"}};
private static final Object[] columnNames = {"A", "B"};
private JTable table;
private DefaultTableModel model;
public Grow() {
Container c = getContentPane();
c.setLayout(new BorderLayout());
model = new DefaultTableModel(rowData, columnNames);
table = new JTable();
table.setModel(model);
c.add(new JScrollPane(table), BorderLayout.CENTER);
JButton add = new JButton("Add");
JButton delete = new JButton("Delete");
c.add(add, BorderLayout.LINE_START);
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
model.addRow(rowData[0]);
}
});
table.setCellEditor(new NumberCellEditor());
c.add(delete, BorderLayout.LINE_END);
delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(table.getSelectedRow()>-1)
model.removeRow(table.getSelectedRow());
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
class NumberCellEditor extends DefaultCellEditor {
/**
*
*/
private static final long serialVersionUID = 1L;
public NumberCellEditor(){
super(new JFormattedTextField());
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
JFormattedTextField editor = (JFormattedTextField) super.getTableCellEditorComponent(table, value, isSelected, row, column);
if (value!=null){
DecimalFormat numberFormat = new DecimalFormat("#,##0.00;(#,##0.00)");
editor.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(numberFormat)));
Number num = (Number) value;
String text = numberFormat.format(num);
editor.setHorizontalAlignment(SwingConstants.RIGHT);
editor.setText(text);
}
System.out.println(value);
return editor;
}
/*@Override
public boolean stopCellEditing() {
try {
// try to get the value
//this.getCellEditorValue();
return super.stopCellEditing();
} catch (Exception ex) {
return false;
}
}
*/
@Override
public Object getCellEditorValue() {
// get content of textField
String str = (String) super.getCellEditorValue();
if (str == null) {
return null;
}
if (str.length() == 0) {
return null;
}
// try to parse a number
try {
ParsePosition pos = new ParsePosition(0);
Number n = NumberFormat.getInstance().parse(str, pos);
if (pos.getIndex() != str.length()) {
throw new ParseException(
"parsing incomplete", pos.getIndex());
}
// return an instance of column class
return new Float(n.floatValue());
} catch (ParseException pex) {
throw new RuntimeException(pex);
}
}
}
public static void main(String[] args) {
Grow g = new Grow();
g.setLocationRelativeTo(null);
g.setVisible(true);
}
}