当我按下按钮时如何自动添加行。我正在使用validate()repaint()fireTableDataChanged()fireTableStructureChanged(),但是它没有解决我的问题。我的数据在数组列表中,并插入到表中。这是我的代码:

JTable table = new JTable();
DefaultTableModel model = new DefaultTableModel();
model.fireTableDataChanged();
table.repaint();
JScrollPane scrollPane = new JScrollPane(table);
try {
    orclConn.statement = orclConn.getConnection().createStatement();
    orclConn.resultset = orclConn.statement.executeQuery(
    "SELECT JOR,DANA,NRXYJOR FROM froshtnykalayhatw WHERE dt = '"+d+"'  ");

    while (orclConn.resultset.next()) {
        //jlFroshraw is array list
        jlFroshraw.add(new IydaShow(orclConn.resultset.getString(1), orclConn.resultset.getInt("dana"), orclConn.resultset.getInt("nrxyjor")));
    }
} catch (Exception df) {
    df.printStackTrace();
}


table.setModel(model);
model.setColumnIdentifiers(new String[]{"dd", "nn", "mm"});

for (IydaShow p : jlFroshraw) {
    model.addRow(new Object[]{p.jor, p.chandJor, p.nrxyfroshraw});
}

pnl.add(scrollPane, BorderLayout.CENTER);

最佳答案

坦率地说,没有足够的代码来完全推导出一个完整的答案

JTable table = new JTable();
DefaultTableModel model = new DefaultTableModel();
// This is pointless as the model isn't actually attached to anything
model.fireTableDataChanged();
// This is pointless as the table hasn't been added to anything
table.repaint();
JScrollPane scrollPane = new JScrollPane(table);
try {
    orclConn.statement = orclConn.getConnection().createStatement();
    orclConn.resultset = orclConn.statement.executeQuery(
    "SELECT JOR,DANA,NRXYJOR FROM froshtnykalayhatw WHERE dt = '"+d+"'  ");

    while (orclConn.resultset.next()) {
        //jlFroshraw is array list
        // This looks dangrous to me, as I can't see where it's been created
        // or if it's been cleared.
        // This could mean that all previous queries are still contained
        // with in it...
        jlFroshraw.add(new IydaShow(orclConn.resultset.getString(1), orclConn.resultset.getInt("dana"), orclConn.resultset.getInt("nrxyjor")));
    }
} catch (Exception df) {
    df.printStackTrace();
}

// This is actually okay
table.setModel(model);
model.setColumnIdentifiers(new String[]{"dd", "nn", "mm"});

// Read the previous statement about jlFroshraw list
for (IydaShow p : jlFroshraw) {
    model.addRow(new Object[]{p.jor, p.chandJor, p.nrxyfroshraw});
}

pnl.add(scrollPane, BorderLayout.CENTER);


更新工作示例

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class TestTableLoad {

    public static void main(String[] args) {
        new TestTableLoad();
    }

    public TestTableLoad() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class TestPane extends JPanel {

        private JTable table;

        public TestPane() {
            table = new JTable();
            setLayout(new BorderLayout());
            add(new JScrollPane(table));

            JButton btn = new JButton("Populate");
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    List<Object[]> values = new ArrayList<>(1000);
                    for (int index = 0; index < 1000; index++) {
                        Object[] row = new Object[3];
                        row[0] = index;
                        row[1] = (int)(Math.random() * 100);
                        row[2] = (int)(Math.random() * 100);
                        values.add(row);
                    }
                    DefaultTableModel model = new DefaultTableModel(new Object[]{"dd", "nn", "mm"}, 0);
                    table.setModel(model);
                    for (Object[] obj : values) {
                        model.addRow(obj);
                    }
                }
            });
            add(btn, BorderLayout.SOUTH);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    }
}

关于java - 从Oracle表自动将数组列表添加到JTable,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15848704/

10-11 19:59