我有2列,x和y。每次用户编辑列时,我希望将这些数据分别附加到该列中的ArrayList(对于x,xValues,对于y,yValues)。通过阅读有关Tables和TabelModels的Oracle教程,我假设我需要向表中添加某种侦听器。我该怎么办?到目前为止,这是我的代码:

public class MainTable extends JPanel {
private static final long serialVersionUID = -5093783987473381647L;
private ArrayList<Integer> xValues = new ArrayList<Integer>();
private ArrayList<Integer> yValues = new ArrayList<Integer>();

public MainTable() {
    super(new GridLayout(1,0));
    createFrame();

    JTable table = new JTable(new TheTabelModel());

    JScrollPane scrollPane = new JScrollPane(table);

    add(scrollPane);
}

class TheTabelModel extends AbstractTableModel {
    private static final long serialVersionUID = -1698344960140377275L;
    private String[] columnNames = {"x", "y"};
    private Object[][] data = {
            {0, 0},
            {0, 0},
            {0, 0},
            {0, 0},
            {0, 0},
            {0, 0},
            {0, 0},
            {0, 0},
    };

    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.length;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    public Class<?> getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    public boolean isCellEditable(int row, int col) {
        if (col < 2) {
            return false;
        } else {
            return true;
        }
    }
}

private void createFrame() {
    JFrame frame = new JFrame("SimpleTableDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    frame.setSize(new Dimension(510, 200));
    frame.setLocationRelativeTo(null);
    frame.add(this);
}

最佳答案

您是否可以实现触发器来调用Java函数,从而又可以在Java代码中更新ArrayList?

检查以下内容:https://docs.oracle.com/cd/F49540_01/DOC/java.815/a64686/04_call2.htm

步骤:

CREATE OR REPLACE PROCEDURE log_sal (
  emp_id NUMBER, old_sal NUMBER, new_sal NUMBER)
AS LANGUAGE JAVA
NAME 'DBTrigger.logSal(int, float, float)';


触发器,它调用过程:

CREATE OR REPLACE TRIGGER sal_trig
AFTER UPDATE OF sal ON emp
FOR EACH ROW
WHEN (new.sal > 1.2 * old.sal)
CALL log_sal(:new.empno, :old.sal, :new.sal);


您可以将触发器设置为在更新时运行,这样,每当用户进行更新时,它就会调用该函数,从而依次更新ArrayList。

09-30 14:37
查看更多