Java,将单元格内容更改为同一行中另一个单元格的function
我需要一些帮助解决我的问题。 我有一个表,例如一个双列和一个string列。 如果双列中的值是负数,则string应该是“negativ”。 而另一种方式,如果价值是积极的,string应该是“positiv”。 现在的问题是,如果我编辑jTable中的double值,该string也应该更新。
更新我的问题,实际的代码看起来像这样:但它不起作用,因为在我编辑第一列值后,第二列中的string将不会更新。 只有在我第一次启动程序时才有效。
import java.util.Vector; import javax.swing.*; import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; import javax.swing.table.*; public class ExampleRemoveAddRows extends JFrame { private Object[] columnNames = {"Double", "positiv / negativ"}; private Object[][] data = { {new Double(10.0), "positiv"}, {new Double(-10.0), "negativ"}, {new Double(20.0), "positiv"}, {new Double(-30.0), "negativ"} }; private JTable table; private DefaultTableModel model; public ExampleRemoveAddRows() { model = new DefaultTableModel(data, columnNames) { @Override public Class getColumnClass(int column) { return getValueAt(0, column).getClass(); } @Override public Object getValueAt(int row, int column) { if (column == 1) { double number = Double.parseDouble(this.getValueAt(row, 0).toString()); System.out.println(number); System.out.println("good"); System.out.println((number < 0) ? "negativ" : "positiv"); return "C: "+ this.getValueAt(row, 0);//((number < 0) ? "negativ" : "positiv"); } else { return super.getValueAt(row, column); } } }; table = new JTable(model); table.setPreferredScrollableViewportSize(table.getPreferredSize()); JScrollPane scrollPane = new JScrollPane(table); add(scrollPane); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ExampleRemoveAddRows frame = new ExampleRemoveAddRows(); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } }
谢谢你的帮助。
山姆
我修改了sscce来显示在这里build议的替代方法。 请注意获取Double
常量的替代方法。 我也重新考虑了String
限制。
附录:在有用的评论中,@kleopatra观察到,直接查询模型将始终产生正确的结果,但是TableModelListener
只会看到第0列而不是第1列的变化。简单的方法是使列1 不可编辑,因为它的值完全取决于列0。
@Override public boolean isCellEditable(int row, int col) { return col == 0; }
下面的第一个例子使用DefaultTableModel
:
import javax.swing.*; import javax.swing.table.*; /** @see https://stackoverflow.com/a/13628183/230513 */ public class ExampleRemoveAddRows extends JFrame { public static final String NEGATIVE = "negativ"; public static final String POSITIVE = "positiv"; private Object[] columnNames = {"Double", POSITIVE + " / " + NEGATIVE}; private Object[][] data = { {10d, null}, {-10.0, null}, {Double.valueOf(30), null}, {Double.valueOf("-30"), null} }; private JTable table; private DefaultTableModel model; public ExampleRemoveAddRows() { model = new DefaultTableModel(data, columnNames) { @Override public Class getColumnClass(int column) { return getValueAt(0, column).getClass(); } @Override public boolean isCellEditable(int row, int col) { return col == 0; } @Override public Object getValueAt(int row, int col) { if (col == 1) { double number = (Double) this.getValueAt(row, 0); return (number < 0) ? NEGATIVE : POSITIVE; } else { return super.getValueAt(row, col); } } @Override public void setValueAt(Object aValue, int row, int col) { super.setValueAt(aValue, row, col); fireTableCellUpdated(row, 1); // may have changed } }; table = new JTable(model); table.setPreferredScrollableViewportSize(table.getPreferredSize()); JScrollPane scrollPane = new JScrollPane(table); add(scrollPane); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ExampleRemoveAddRows frame = new ExampleRemoveAddRows(); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } }
这个变体extends AbstractTableModel
:
import java.awt.EventQueue; import java.util.ArrayList; import java.util.List; import javax.swing.*; import javax.swing.table.*; /** * @see https://stackoverflow.com/a/13628183/230513 */ public class ExampleRemoveAddRows extends JFrame { public static final String NEGATIVE = "negativ"; public static final String POSITIVE = "positiv"; public ExampleRemoveAddRows() { DoubleModel model = new DoubleModel(); model.add(10.1); model.add(-10.2); model.add(Double.valueOf(30.1)); model.add(Double.valueOf("-30.2")); JTable table = new JTable(model); table.setPreferredScrollableViewportSize(table.getPreferredSize()); JScrollPane scrollPane = new JScrollPane(table); add(scrollPane); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { ExampleRemoveAddRows frame = new ExampleRemoveAddRows(); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } private class DoubleModel extends AbstractTableModel { List<Double> data = new ArrayList<Double>(); public void add(Double d) { data.add(d); } @Override public int getRowCount() { return data.size(); } @Override public int getColumnCount() { return 2; } @Override public String getColumnName(int col) { if (col == 0) { return "Double"; } else { return POSITIVE + " / " + NEGATIVE; } } @Override public Class<?> getColumnClass(int col) { if (col == 0) { return Double.class; } else { return String.class; } } @Override public boolean isCellEditable(int row, int col) { return col == 0; } @Override public Object getValueAt(int row, int col) { if (col == 0) { return data.get(row); } else { double number = (Double) this.getValueAt(row, 0); return (number < 0) ? NEGATIVE : POSITIVE; } } @Override public void setValueAt(Object aValue, int row, int col) { if (col == 0) { data.set(row, (Double) aValue); fireTableRowsUpdated(row, row); } } } }
通过TableModelEvent.getSource()
,确实可以访问TableModel(如果没有弄错的话TableModelEvent.getSource()
。
只是提供一个基本的例子(主要是因为一个答辩答案似乎不是一个答案):
TableModel model = (TableModel)te.getSource(); Double number = model.getValueAt(te.firstRow, 0); model.setValueAt(((number < 0) ? "negativ":"positiv"), te.firstRow, 1);