如何向 JTable 中插入一行数据?
How do I insert a row of data into JTable?
我正在尝试在 JTable 的顶行插入数据。固定大小 table。我确实让它工作了,但如果我要在 10 行上做 table。代码会很尴尬。
我对此很陌生,请暂时不要使用 lambda。 (如果可能的话)
谢谢你。再见,我要睡觉了
/**
* 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();
}
}
好的,我关注了 - camickr。
很好地解决了谢谢。非常翔实。
此代码是预期的。 (差不多)
/**
* 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();
}
}
我正在尝试在 JTable 的顶行插入数据。固定大小 table。我确实让它工作了,但如果我要在 10 行上做 table。代码会很尴尬。
我对此很陌生,请暂时不要使用 lambda。 (如果可能的话) 谢谢你。再见,我要睡觉了
/**
* 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();
}
}
好的,我关注了 - camickr。 很好地解决了谢谢。非常翔实。 此代码是预期的。 (差不多)
/**
* 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();
}
}