简体   繁体   English

如何在 JTable 中插入一行数据?

[英]How do I insert a row of data into JTable?

I am trying to insert data at the top row of JTable.我正在尝试在 JTable 的第一行插入数据。 A fixed size table.固定大小的桌子。 I did actually got it to work, but if I were to do on a 10 row table.我确实让它工作了,但如果我要在一个 10 行的桌子上做。 The code would be very awkward.代码会很尴尬。

I'm very new to this, no lambda for now please.我对此很陌生,请暂时不要 lambda。 (if possible) Thank You. (如果可能)谢谢。 Bye, I need sleep.再见,我需要睡觉。

/**
 * 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();
    }
}

Okay, I followed - camickr .好的,我跟着 - camickr Solved quite nicely thank you.很好地解决了谢谢。 Very informative.信息量很大。 This code is the what was intended.此代码是预期的。 (almost) (几乎)

/**
 * 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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM