Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 722
  • Last Modified:

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.
0
ShamD
Asked:
ShamD
  • 6
  • 5
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now