使用新数据刷新Jtable

时间:2012-02-07 07:50:43

标签: swing jtable jcheckbox

  1. 我有加载按钮,将数据加载到jtable
  2. 我可以将新数据加载到jtable
  3. 但是当我重新加载数据并点击jcheckbox时,会出现旧数据。 我正在使用defaulttablemodel选项。 请运行编程并帮助我在哪里出错。

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    
    public class MainWindow extends JFrame {
    JTable table;
    private static final int CHECK_COL = 3;
        String[] columnNames = {
        "Country", "Capital", "Population in Millions", "Democracy"};
        Object[][] data = {
        {"USA", "Washington DC", 280, false},
        {"Canada", "Ottawa", 32, false},
        {"United Kingdom", "London", 60, false},
        {"Germany", "Berlin", 83, false},
        {"France", "Paris", 60, false},
        {"Norway", "Oslo", 4.5, false},
        {"India", "New Deli", 1046, false}
        };
        DefaultTableModel dtm ;
        // static JScrollPane scrollpane= new JScrollPane();
        public MainWindow() {
        setTitle("Marking of data");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         JButton button1 = new JButton("load");
        button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(dtm!=null){
                System .out .println("NOT EMPTY");
                int c = dtm.getRowCount();
            // for (int i=c-1; i>=0; i--)
            // {
                // dtm.removeRow(i);
                // table.revalidate();
            // }
                loadtable();
                 }
            if(dtm==null){
                System .out .println("EMPTY");
                loadtable();
            }
          }
          });
    
          JButton button = new JButton("check");
          button.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            for (int row = 0; row < table.getRowCount(); row++) {
                Boolean b = ((Boolean) table.getValueAt(row, CHECK_COL));
                if (b.booleanValue()) {
                    System.out.print("row " + row + " is " + b + ": ");
                    for (int col = 0; col < table.getColumnCount(); col++) {
                        System.out.print(table.getValueAt(row, col) + " ");
                    }
                    System.out.println();
                }
            }
          }
          });
          JPanel buttonpanel1 = new JPanel();
          buttonpanel1.add(button1);
          JPanel buttonpanel = new JPanel();
          buttonpanel.add(button);
          add(buttonpanel, BorderLayout.SOUTH);
          add(buttonpanel1, BorderLayout.NORTH);
          pack();
          setLocationByPlatform(true);
          setVisible(true);
          }
          public void loadtable(){
        dtm = new DefaultTableModel(data, columnNames) {
            @Override
            public Class getColumnClass(int col) {
                return getValueAt(0, col).getClass();
            }
            @Override
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return (colIndex == CHECK_COL);
            }
          };
          table = new JTable(dtm);
          JScrollPane scrollpane = new JScrollPane(table);
          add(scrollpane, BorderLayout.CENTER);
          pack();
          setVisible(true);
          }
    
    
         public static void main(String args[]) {
         EventQueue.invokeLater(new Runnable() {
         @Override
         public void run() {
            new MainWindow();
          }
         });
         }
          }
    
  4. 需要帮助。 PLS

1 个答案:

答案 0 :(得分:0)

默认表模型将二维数组中的数据复制到自己的向量中。

每次按下加载按钮,都会使用相同的数据创建一个新的默认表模型,并为新表模型的向量创建一个新副本。

每次按下加载按钮时,您的代码都会重置表模型,当然,旧数据会出现。