如何在添加新行后进行JTable刷新 [英] how to make my JTable refresh after adding a new row

查看:221
本文介绍了如何在添加新行后进行JTable刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JTable 添加新行后。写入的信息转到txt文件,但我的 JTable 没有显示最后一个原始文件。但是,如果我关闭程序并再次运行它,那么表中的信息就会出现。那么,有没有办法刷新 JTable 中的数据而不关闭应用程序并再次运行它?

After I add a new row to a JTable. The information wrote goes to the txt file, but my JTable doesn't shows the last raw. But if I close the program, and run it again, the information it's in the table. So, is there a way to refresh the data in the JTable without closing the application and running it again?

String[] columns = {"nume", "compozitie", "indicatii", "contraindicatii", "administrare", "pret", "compensabil", "stoc"};

Object[][] data = null;
try {
    File file = new File("medicamente.txt");
    FileReader fileReader = new FileReader(file);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    data = new Object[100][];
    String line;
    int numLines = 0;
    while ((line = bufferedReader.readLine()) != null) {
        data[numLines] = line.split(",");
        numLines++;
    }

    fileReader.close();

} catch (IOException e) {
    e.printStackTrace();
}

TableModel model = new DefaultTableModel(data, columns) {
    @Override
    public Class getColumnClass(int column) {
        Class returnValue;
        if ((column >= 0) && (column < getColumnCount())) {
            returnValue = getValueAt(0, column).getClass();
        } else {
            returnValue = Object.class;
        }
        return returnValue;
    }
};

JTable table = new JTable(model) {
    public boolean isCellEditable(int row, int column) {
        return false;
    }
};

final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
table.setRowSorter(sorter);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(1000, 500));
mainPanel.add(scrollPane);
scrollPane.setBounds(0, 240, 995, 510);

final JTextField filterText = new JTextField(null);
mainPanel.add(filterText);
filterText.setBounds(0, 750, 850, 25);

JButton button = new JButton("Filter");
mainPanel.add(button);
button.setBounds(850, 750, 150, 25);
button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        String text = filterText.getText();
        if (text.length() == 0) {
            sorter.setRowFilter(null);
            //   model.fireTableDataChanged();                       

        } else {
            sorter.setRowFilter(RowFilter.regexFilter(text));
        }
    }
});


推荐答案

看起来你认为当文件时更新, JTable 也应如此。它不像那样工作。您需要做的是在 TableModel 中添加一行。在你的情况下的一个缺点是这

It looks like what you think is that when the file updates, so should the JTable. It doesn't work like that. What you need to do is add a row to the TableModel. A disadvantage in your case is this

TableModel model = new DefaultTableModel(   data, columns)

您使用的是 TableModel 界面,您可以使用非常有限的方法。而是这样做

You're using the TableModel interface, which has very limited methods you can use. Instead do this

DefaultTableModel model = new DefaultTableModel(   data, columns)

然后你可以使用 DefaultTableModel

  • public void addRow(Object[] rowData) - Adds a row to the end of the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated.

public void addRow(Vector rowData) - 在模型末尾添加一行。除非指定了rowData,否则新行将包含空值。将生成添加行的通知。

public void addRow(Vector rowData) - Adds a row to the end of the model. The new row will contain null values unless rowData is specified. Notification of the row being added will be generated.

因此,当您想要添加行时,您可以收集您的行数据到一个数组, addRow(..)然后表会自动为你更新。

So when you want to add a row, you can gather your data into an array, addRow(..) then the table will get automatically update for you.

Object[] row = { data1, data2, data2, data4, data5 };
DefaultTableModel model = (DefaultTableModel)table.getModel();
model.addRow(row);

此外,它看起来像你的 JTable 是本地范围的。您可能希望为其提供全局的类成员范围,以便您可以从任何需要的位置访问它。

Also it looks to me like your JTable is locally scoped. You may want to give it a global, class member scope, so you can access it from where ever you need to.

这篇关于如何在添加新行后进行JTable刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆