-1

I have seen quite a few threads on refreshing a JTable using:

String[] columnNames = {"column1","column2,......,"columnN"};
String[][] tableData = new String[1][columnNames.length];
JTable table = new JTable(tableData,columnNames);

JScrollPane scrollpane = new JScrollPane(table);
contentPane.add(scrollpane);

... make a change to the data stored in tableData then call...

AbstractTableModel model = (AbstractTableModel) table.getModel();
model.fireTableDataChanged();

But this doesn't seem to change anything for me. I need to initially show display a blank table with one row and then update it when the user loads a data file however the code below doesn't seem to work for me. I assume its because im redefining tableData but this has to be done as i do not know how many rows of data are to be displayed until the user loads a file. How can I update the table?


Edit: To make it clearer the code i am running is:

//Initialize Table With 1 Blank Row
String[] columnNames = {"column1","column2,......,"columnN"};
String[][] tableData = new String[1][columnNames.length];
JTable table = new JTable(tableData,columnNames);

JScrollPane scrollpane = new JScrollPane(table);
contentPane.add(scrollpane);

//When the file is loaded resize tableData so it can fit all the file data
tableData = new String[length of data file][columnNames.length];

//Update some of the tableData points
tableData[0][0]="data 1";
tableData[1][0]="data 2";
tableData[0][1]="data 3";
tableData[1][1]="data 4";
....etc

//Tell the JTable to update
AbstractTableModel model = (AbstractTableModel) table.getModel();
model.fireTableDataChanged();
4

2 回答 2

3

我认为问题在于,当您创建 JTable 时,您没有为其分配任何模型。

见下面的例子:

这里太多了,但你会明白的。

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TestGUI {

    private JFrame frame;
    private JTable table;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestGUI window = new TestGUI();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public TestGUI() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(60, 81, 311, 96);

        frame.getContentPane().add(scrollPane);

        String[] columnNames = { "column1", "column2", "columnN" };
        String[][] tableData = new String[1][columnNames.length];
        table = new JTable(tableData, columnNames);
        DefaultTableModel model = new DefaultTableModel(2, columnNames.length);
        model.addRow(new String[] { "A", "B", "C" });
        table.setModel(model);

        scrollPane.setViewportView(table);

        JButton btnNewButton = new JButton("update");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                DefaultTableModel model = (DefaultTableModel) table.getModel();
                model.addRow(new String[] { "A", "B", "D" });

            }
        });
        btnNewButton.setBounds(49, 199, 89, 23);
        frame.getContentPane().add(btnNewButton);
    }
}
于 2013-10-22T15:52:48.357 回答
2

您的示例将永远无法工作,因为DefaultTableModel(默认情况下在此处使用)会将您的所有数组数据复制到内部向量对象中。因此,您对原始数组的更改将永远不会传播到DefaultTableModel.

直接使用DefaultTableModel以避免您的问题。

于 2013-10-22T16:02:49.517 回答