Getting a JTable to redraw/display its contents


Hi,

I am creating an application using the MVC pattern.
The Client draws itself to begin with, and I create a new empty JTable.

The user of the App can then select a search button which goes off to the controller and fires method calls on the model to retrieve data.

When the data array is sent back to the method drawTable() in the view, its fine.
All the neccessary data I need is there.
So I create a new JTable and assign the data to it. But it wont display.
Seems to display the old table.

Here is my drawTable method.

      private void drawTable(Object[][] data) {
            table = new JTable(data, columnHeaders);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            table.getColumnModel().getColumn(0).setPreferredWidth(122);
            table.getColumnModel().getColumn(1).setPreferredWidth(180);
            table.getColumnModel().getColumn(2).setPreferredWidth(120);
            table.getColumnModel().getColumn(3).setPreferredWidth(240);
            table.getColumnModel().getColumn(4).setPreferredWidth(120);
            table.getColumnModel().getColumn(5).setPreferredWidth(100);
            table.getColumnModel().getColumn(6).setPreferredWidth(120);
            //table.setSize(800, 800);
            
            scrollPane = new JScrollPane(table);
            scrollPane.setMinimumSize(new Dimension(1000, 350));
            scrollPane.setPreferredSize(new Dimension(1000, 350));
            //table.revalidate();
            //table.updateUI();
      }

What am i doing wrong? I think i might be displaying the old table.
Would it be possible to set the data[] array into the table without creating a new JTable() object?

Thanks for your help.
ShamDAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
you need to add your table to your component hierarchy, though shouldn't be necessary try:


      private void drawTable(Object[][] data) {
          table = new JTable(data, columnHeaders);
          table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
          table.getColumnModel().getColumn(0).setPreferredWidth(122);
          table.getColumnModel().getColumn(1).setPreferredWidth(180);
          table.getColumnModel().getColumn(2).setPreferredWidth(120);
          table.getColumnModel().getColumn(3).setPreferredWidth(240);
          table.getColumnModel().getColumn(4).setPreferredWidth(120);
          table.getColumnModel().getColumn(5).setPreferredWidth(100);
          table.getColumnModel().getColumn(6).setPreferredWidth(120);
          //table.setSize(800, 800);
         
          scrollPane = new JScrollPane(table);
          scrollPane.setMinimumSize(new Dimension(1000, 350));
          scrollPane.setPreferredSize(new Dimension(1000, 350));
          //table.revalidate();
          //table.updateUI();
      }
      private void drawTable(Object[][] data) {
          table = new JTable(data, columnHeaders);
          table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
          table.getColumnModel().getColumn(0).setPreferredWidth(122);
          table.getColumnModel().getColumn(1).setPreferredWidth(180);
          table.getColumnModel().getColumn(2).setPreferredWidth(120);
          table.getColumnModel().getColumn(3).setPreferredWidth(240);
          table.getColumnModel().getColumn(4).setPreferredWidth(120);
          table.getColumnModel().getColumn(5).setPreferredWidth(100);
          table.getColumnModel().getColumn(6).setPreferredWidth(120);
          //table.setSize(800, 800);
         
          scrollPane.getViewport().setView(table);
          scrollPane.repaint();
      }

Or another way would be to create new model and use the existing tables setModel() method.

      private void drawTable(Object[][] data) {
          table.setModel(new DefaultTableModel(data, columnHeaders));
      }
0
ShamDAuthor Commented:
Hi,

Thanks for your comments, They dont seem to work though.
Think the problem is not just with the JTable. Its with the GUI in general not displaying any objects when it comes back from the controller.

I tried repaint but it doesnt work.
I even tried a System.out.println("hello"); in the above method. That doesnt even appear.
0
objectsCommented:
where do u add your table?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

ShamDAuthor Commented:
I add the table in an initialiseGUI() method...that runs the first time the screen is built.
But doesnt run when it returns from the controller.
So the old table is in the container.
Is this the problem?
0
ShamDAuthor Commented:
Is it possible that it is displaying behind the original table. ?
Is there a way of removing the first table I create and add to the container?
So this new table gets displayed when control comes back to the GUI and the drawTable method is called.?
0
objectsCommented:
you shouldn't need to remove the original table, can you post the code that adds it.
0
ShamDAuthor Commented:
Yes,

I agree with you. I got the functionality to work, But I feel I am doing this in an incorrect way - I am removing the original scrollPane (which has the table attatched) from the container, and adding a new one in the same position. It works..but it is not the correct way to do it.
So I am not going to do it this way.

I think it is wrong to have to remove the acual table each time.
Here is the code i used, before I got it to work by removing the scrollPane.

This is a couple of method I use.
Please try to remember that when the flow of control comes back into this Client class from the controller...it goes into drawTable()..and functions correctly.
It wont call initialieGUI again. Hope you can help.
Thanks

public class Client extends JFrame implements ActionListener{

        protected GridBagLayout gridBagLayout;
      protected Container container;
      protected JTable table;
      protected JScrollPane scrollPane;

   
        // Constructor
        public Client(){
            
            super("Test Table");            
            initialiseGUI();
            
      }

        protected void initialiseGUI() {
            
            container = getContentPane();
            gridBagLayout = new GridBagLayout();
            container.setLayout(gridBagLayout);
            
            Object data[][] = new Object[1][7];
            drawTable(data);
                        
            container.add(scrollPane, new GridBagConstraints(0, 1, 1, 1, 1.0, 3.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(20, 30, 20, 30), 0, 0));
            
            setSize(950,900);
            setVisible(true);
      }


      private void drawTable(Object[][] data) {

            table = new JTable(new ContractorTableModel(data));
            
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            table.getColumnModel().getColumn(0).setPreferredWidth(122);
            table.getColumnModel().getColumn(1).setPreferredWidth(180);
            table.getColumnModel().getColumn(2).setPreferredWidth(120);
            table.getColumnModel().getColumn(3).setPreferredWidth(240);
            table.getColumnModel().getColumn(4).setPreferredWidth(120);
            table.getColumnModel().getColumn(5).setPreferredWidth(100);
            table.getColumnModel().getColumn(6).setPreferredWidth(120);
            
            scrollPane = new JScrollPane(table);
            scrollPane.setMinimumSize(new Dimension(1000, 350));
            scrollPane.setPreferredSize(new Dimension(1000, 350));
            
      }
}
0
objectsCommented:
try something like:

      protected void initialiseGUI() {
         
          container = getContentPane();
          gridBagLayout = new GridBagLayout();
          container.setLayout(gridBagLayout);

          Object data[][] = new Object[1][7];
          table = new JTable(new ContractorTableModel(data));
          table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
          table.getColumnModel().getColumn(0).setPreferredWidth(122);
          table.getColumnModel().getColumn(1).setPreferredWidth(180);
          table.getColumnModel().getColumn(2).setPreferredWidth(120);
          table.getColumnModel().getColumn(3).setPreferredWidth(240);
          table.getColumnModel().getColumn(4).setPreferredWidth(120);
          table.getColumnModel().getColumn(5).setPreferredWidth(100);
          table.getColumnModel().getColumn(6).setPreferredWidth(120);

          scrollPane = new JScrollPane(table);
          scrollPane.setMinimumSize(new Dimension(1000, 350));
          scrollPane.setPreferredSize(new Dimension(1000, 350));
                   
          container.add(scrollPane, new GridBagConstraints(0, 1, 1, 1, 1.0, 3.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(20, 30, 20, 30), 0, 0));
         
          setSize(950,900);
          setVisible(true);
     }


     private void drawTable(Object[][] data) {

          table.setModel(new ContractorTableModel(data));
     }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ShamDAuthor Commented:
Excellent man.
Works like a dream.
You are a ledgend.
0
ShamDAuthor Commented:
I increased the points to 200 for you.
Very appreciative of your help.
Thanks alot
0
objectsCommented:
no worries :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.