我有两列,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);
}
答案 0 :(得分:0)
您是否可以实现一个触发器来调用Java函数,而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。