Java-使用fireTableDataChanged()更新JTable; [英] Java - Updating JTable with fireTableDataChanged();

查看:48
本文介绍了Java-使用fireTableDataChanged()更新JTable;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有一个JTable.我想在单击JButton后更新它. 我是这样写的:

I have a JTable in my program. I want to update it after clicking JButton. I wrote this:

DefaultTableModel myTable = new DefaultTableModel(celDatas,celNames);
        JTable source = new JTable(myTable){public boolean isCellEditable(int rowIndex, int colIndex) {
  return false;}};
        JScrollPane pane = new JScrollPane(source);

(...)
    start.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {
                    query = "Select sal FROM EMP";
    myTable =(DefaultTableModel)source.getModel();
                myTable.fireTableDataChanged();
}

问题在于id不会更新我在JTable上的数据. 如何解决这个问题?

The problem is that id doesn't update my data on JTable. How to resolve this problem?

JTable通过JScrollPane显示在我的家伙中. 我现在做这个:

JTable is displayed in my guy through JScrollPane. I make now this:

   source = new JTable(myTable){public boolean isCellEditable(int rowIndex, int colIndex) {return false;}};
   pane = new JScrollPane(source);

我还创建了一个新的void,从数据库中获取数据+在此定义myTable:

I made also a new void, where is getting datas from database + there I define myTable:

void queryConnection() {
   (...)
        myTable = new DefaultTableModel(celDatas,celNames);

}

我添加了一个JButton,它会更新我的JTable(当我们更改查询时.

I added a JButton, which update my JTable (when we change the query.

start.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
          query = "Select sal FROM EMP";
          queryConnection();
    }
}

================================================ ================================

================================================================================

public class Application2 implements Runnable {
private JTable source;
   private JScrollPane pane;
   private DefaultTableModel myTable;

   private JPanel panel;

   private String[][] celDatas = null;
   private String[] celNames = null;

   public void run() {


(...)


start.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {
            if(...) {

                query = "Select sal FROM EMP";
                queryConnection();
            }
}

if(query == null) {
            query = "Select * from EMP";
            queryConnection();
        }

        source = new JTable(myTable){public boolean isCellEditable(int rowIndex, int colIndex) {return false;}};
        pane = new JScrollPane(source);
        pane.setSize(f.getSize().width-60,300);
        pane.setLocation(30,20);

panel.add(pane);
f.add(panel);
        f.pack();
}
void queryConnection() {


//here is connection and downloading datas

myTable = new DefaultTableModel(celDatas,celNames);

}

我希望现在更方便吗? ;)

I hope that it is right now more convenient? ;)

推荐答案

如果要创建新的TableModel,则不需要调用任何fireXXX()方法,但是如果要创建新的TableModel,则主要需要调用这些方法根据AbstractTableModel编写TableModel(不是).

You don't need to call any fireXXX() methods if you're creating a new TableModel, but rather you mainly need to call these if you're writing your TableModel based on AbstractTableModel (you're not).

我认为您的问题出在其他地方.您似乎正在创建一个新的JTable并将其添加到新的JScrollPane中.您是否正在将这些组件添加到GUI(未显示)?如果是这样,您要添加的容器是否能够顺利接受新组件?您是否重新验证容器并重新粉刷容器?您的新创建的JTable是否显示在GUI中?

Your problem lies elsewhere I think. You seem to be creating a new JTable and adding it to a new JScrollPane. Are you adding these components to your GUI (you don't show)? If so, is the container that you're adding to able to accept new components smoothly? Do you revalidate and repaint the container? Is your newly created JTable being displayed in the GUI?

或者,您的GUI中是否已经显示了JTable?如果是这样,也许您要做的只是设置其模型,而不是创建一个新的JTable.我最喜欢此解决方案.

Or, is there a JTable already displayed in your GUI? If so, perhaps all you want to do is set its model rather create a new JTable. I favor this solution as the easiest.

编辑1
我会做这样的事情:

Edit 1
I would do something like so:

  • 在后台线程中访问数据库并从中获取信息.
  • 创建一个新的DefaultTableModel对象,用从数据库中提取的数据填充该对象.
  • 如果已经存在一个新的JTable,我将不创建它.相反,我只需要在当前显示的JTable上调用setTableModel(...),然后传入我新创建的DefaultTableModel对象即可.
  • 然后我会坐下来享受我的成功所带来的丰厚金钱和其他成果.
  • Access Database in a background thread and get information from it.
  • Create a new DefaultTableModel object filling it with data extracted from the database.
  • I would not create a new JTable if one already exists. Instead I'd simply call setTableModel(...) on the currently displayed JTable and pass in my newly created DefaultTableModel object.
  • Then I'd sit back and enjoy the big bucks and other fruits of my success.

编辑2
Ganjira,为了使我们能够最好地为您提供帮助,我们需要更好地了解问题,因此我要求您完全澄清您的问题,或者要求 sscce ,我都没看过.我在了解您的问题时遇到的问题包括:

Edit 2
Ganjira, for us to be able to best help you, we need to understand the problem better, which is why I've requested either full clarification of your problem or an sscce, of which I've seen neither. My problems with understanding your issues include:

  • 再次,您的GUI中是否已经显示了JTable,现在您正尝试根据从数据库中提取的数据来更改JTable中显示的数据?
  • 如果是这样,为什么不简单地更改现有JTable的模型而不是创建一个全新的JTable?
  • 如果应用程序中还没有JTable,现在想显示一个JTable,则说明您的数据没有显示,但没有显示代码来帮助我们理解原因.
  • 如果您可以回答前两个问题,则我们可能不需要sscce,但如果没有,请理解您当前的帖子与 sscce ,因为我们无法运行它,我们无法对其进行编译,它也无法为我们重现您的问题,...我想知道您是否还阅读了该链接,它解释了为什么所有这些条件都是重要的(简明扼要,以免淹没我们大量无关的代码).
  • 否,不需要您发布SSCCE,但是如果我们不能根据您提出的问题和代码段来帮助您,那么它确实为我们提供了更好的机会来理解问题并查找一个适合您的解决方案.
  • Again, do you have a JTable already displayed in your GUI, and now you're trying to change the data that is displayed in the JTable based on data extracted from a database?
  • If so, why not simply change the model of the existing JTable rather than create a whole new JTable?
  • If you don't already have a JTable in the application and now you want to display one, you state that your data is not being displayed but don't show code to help us understand why.
  • If you can answer the first two questions, we may not need an sscce, but if not, please understand that your current post is no where close to being an sscce, in that we cannot run it, we cannot compile it, it doesn't reproduce your problem for us,... I have to wonder if you've even read the link yet as it explains why all of these conditions are important (that and brevity so as not to drown us in a large amount of unrelated code).
  • No, it is not required that you post an SSCCE, but if we can't help you based on the text in your question and your code snippets, it does offer a better chance of allowing us to understand the problem and find a solution for you.

编辑3
您声明:

Edit 3
You state:

JTable已经在GUI中,因为它显示了程序的开始".单击JButton之后,我想更改JTable中的值.它不会更改现有JTable的模型-不会更改任何内容.

JTable's been already in GUI, because it shows the 'beginning of the program'. After clicking JButton I want to change values in JTable. It doesn't change the model of the existing JTable - it doesn't change nothing.

这使我感到困惑,因为一直以来我一直建议最好的解决方案-不要创建新的JTable,而是创建一个新的DefaultTableModel并更改现有JTable的模型.您还没有提到为什么不尝试使用此功能,或者如果尝试过使用它,那么它为什么不起作用.

And this confuses me as the best solution is one I've been suggesting all along -- Don't make a new JTable, but instead create a new DefaultTableModel and change the model for the existing JTable. You've yet to mention why you're not trying this, or if you have tried it, how it's not working for you.

这篇关于Java-使用fireTableDataChanged()更新JTable;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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