0

ボタンを押したときに行を自動的に追加するにはどうすればよいですか。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);
4

1 に答える 1

1

率直に言って、完全な答えを完全に推測するのに十分なコードはありません。

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);
        }        
    }    
}
于 2013-04-06T08:42:18.793 に答える