3

我需要一些帮助来解决我的问题。我有一个表,例如一个双列和一个字符串列。如果双列中的值为负数,则字符串应为“负数”。反之,如果值为正,则字符串应为“正”。现在的问题是,如果我在 jTable 中编辑双精度值,字符串也应该被更新。

更新我的问题,实际代码如下所示: 但它不起作用,因为在我编辑第一列值后,第二列中的字符串不会更新。它仅在我第一次启动程序时才有效。

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);
            }
        });
    }
}

谢谢你的帮助。

山姆

4

2 回答 2

6

我已经修改了您的 sscce 以显示此处建议的替代方法。请注意获得Double常数的替代方法。我还重新考虑了String约束。

附录:在有用的评论中,@kleopatra 观察到直接查询模型总是会产生正确的结果,但是 aTableModelListener只会看到对第 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);
            }
        }
    }
}
于 2012-11-29T14:41:51.333 回答
3

如果我没记错的话,您确实可以通过TableModelEvent.getSource().

只是为了提供一个基本的例子(主要是因为一个句子的答案似乎不太像一个答案):

TableModel model = (TableModel)te.getSource();
Double number = model.getValueAt(te.firstRow, 0);
model.setValueAt(((number < 0) ? "negativ":"positiv"), te.firstRow, 1);
于 2012-11-28T19:14:33.353 回答