Ganjira
Ganjira

Reputation: 976

Java - Updating JTable with fireTableDataChanged();

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();
}

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


EDIT: 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);

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

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

}

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? ;)

Upvotes: 1

Views: 27146

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

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).

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?

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.

Edit 1
I would do something like so:

  • 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.

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:

  • 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.

Edit 3
You state:

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.

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.

Upvotes: 3

Related Questions