Link to home
Start Free TrialLog in
Avatar of ShamD
ShamD

asked on

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.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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));
      }
Avatar of ShamD
ShamD

ASKER

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.
where do u add your table?
Avatar of ShamD

ASKER

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?
Avatar of ShamD

ASKER

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.?
you shouldn't need to remove the original table, can you post the code that adds it.
Avatar of ShamD

ASKER

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));
            
      }
}
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ShamD

ASKER

Excellent man.
Works like a dream.
You are a ledgend.
Avatar of ShamD

ASKER

I increased the points to 200 for you.
Very appreciative of your help.
Thanks alot
no worries :)