Java 如何将一行数据插入JTable?
我试图在JTable的顶行插入数据。固定大小的桌子。我确实让它工作了,但如果我在一张10排的桌子上工作的话。代码将非常笨拙 我对这个很陌生,请暂时不要lambda。(如有可能) 非常感谢。再见,我需要睡觉Java 如何将一行数据插入JTable?,java,swing,jtable,Java,Swing,Jtable,我试图在JTable的顶行插入数据。固定大小的桌子。我确实让它工作了,但如果我在一张10排的桌子上工作的话。代码将非常笨拙 我对这个很陌生,请暂时不要lambda。(如有可能) 非常感谢。再见,我需要睡觉 /** * A basic 3x3 JTable within JFrame inserting data from top row */ public class TestCode extends JTable { private JTable table; // I
/**
* A basic 3x3 JTable within JFrame inserting data from top row
*/
public class TestCode extends JTable {
private JTable table;
// Insert at row zero and push other row by one row
public void insertRowZero() {
String one, two, three;
one = "numone";
two = "numtwo";
three = "numthree";
// get row 1 and paste into row 2
table.setValueAt(table.getValueAt(1, 0).toString(), 2, 0);
table.setValueAt(table.getValueAt(1, 1).toString(), 2, 1);
table.setValueAt(table.getValueAt(1, 2).toString(), 2, 2);
// get row 0 and paste into row 1
table.setValueAt(table.getValueAt(0, 0).toString(), 1, 0);
table.setValueAt(table.getValueAt(0, 1).toString(), 1, 1);
table.setValueAt(table.getValueAt(0, 2).toString(), 1, 2);
// not actually insert, but does the job
table.setValueAt(one, 0, 0);
table.setValueAt(two, 0, 1);
table.setValueAt(three, 0, 2);
table.repaint();
}
// Create a fixed size table, 3 row and 3 column
public void createTable() {
String[] columnName = {"x", "y", "z"}; // column row doesn't show ?
Object[][] data = {
{"r0-c0", "r0-c1", "r0-c2"},
{"r1-c0", "r1-c1", "r1-c2"},
{"r2-c0", "r2-c1", "r2-c2"}
};
JFrame frame = new JFrame();
frame.setSize(300, 100);
table = new JTable(data, columnName);
frame.add(table);
frame.setVisible(true);
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(TestCode.class.getName()).log(Level.SEVERE, null, ex);
}
}
public TestCode() {
// construct
createTable();
insertRowZero();
}
public static void main(String[] args) {
// just a start point
new TestCode();
}
}
好吧,我跟着-。
很好地解决了,谢谢。信息量很大。
此代码是预期的。(差不多)
1) 使用DefaultTableModel
保存数据。2) 使用DefaultTableModel
的insertRow(…)
方法插入一行数据。3) 不要扩展JTable。只有在向类添加功能时,才能扩展类。您没有添加功能。4) 不需要使用table.repaint()。当组件的属性更改或组件的模型更改时,所有Swing组件将自动重新绘制自己。。
/**
* A basic 3x3 JTable within JFrame inserting data from top row
*/
public class TestCode {
private DefaultTableModel tm;
// Insert at row zero and push other row by one row
// and remove row three
public void insertRowZero() {
tm.insertRow(0, new Object[]{"numone", "numtwo", "numthree"});
tm.removeRow(3);
}
// Create a fixed size table, 3 row and 3 column
public void createTable() {
tm = new DefaultTableModel();
JTable table = new JTable(tm);
tm.addColumn("x");
tm.addColumn("y");
tm.addColumn("z");
tm.insertRow(0, new Object[]{"r0-c0", "r0-c1", "r0-c2"});
tm.insertRow(1, new Object[]{"r1-c0", "r1-c1", "r1-c2"});
tm.insertRow(2, new Object[]{"r2-c0", "r2-c1", "r2-c2"});
JFrame frame = new JFrame();
frame.setSize(300, 100);
frame.add(table);
frame.add(new JScrollPane(table)); // needed to show column header
frame.setVisible(true); // don't really want scroll pane
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(TestCode.class.getName()).log(Level.SEVERE, null, ex);
}
}
public TestCode() {
// construct
createTable();
insertRowZero();
}
public static void main(String[] args) {
// just a start point
new TestCode();
}
}