0

I'm having a problem with displaying a JTable in a JPanel. I'm having the user press a button and within this button the table should generate the table and put in an already existing panel, now when I tried to open it in a new JFrame the data shows perfectly but I want to show it in a JPanel.

Code:

   files = c.RequestFile("list"); //gets the data of files
        Object[][] filesArray = new Object[files.size()][1];
        int size = 0;
        for (FileInfo fileInfo : files) {
            filesArray[size][0] = fileInfo.getName();
            size++;
        }

        fileTablePanel.setLayout(new BorderLayout());
        fileTablePanel.add(new JScrollPane(createTable(filesArray))); 
Peter Walser
  • 15,208
  • 4
  • 51
  • 78
Luke Griffiths
  • 119
  • 1
  • 1
  • 12
  • For many components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). But the typical approach with a `JTable` is to add it at start up, and *populate it* (add data) when needed. – Andrew Thompson Mar 07 '14 at 08:55

3 Answers3

4

You want to look into using a TableModel. What you are currently doing is trying to replace one JTable with another JTable. That is totally unnecessary. You should just manipulate the data of the TableModel.

You can see more at How to use Tables. You can focus on the section Creating a Table Model

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

After adding the new content to the panel, you need to repaint the panel to the screen:

fileTablePanel.revalidate();
fileTablePanel.repaint();

It works when you popup a new JFrame since it's the first time the JFrame is painted to the screen.

Joshua Kissoon
  • 3,269
  • 6
  • 32
  • 58
  • *"that is what i needed"* That is the right answer to the wrong question. See the post of peeskillet for the correct approach. – Andrew Thompson Mar 07 '14 at 08:57
  • No, this is the right answer @AndrewThompson. The problem is when he updates his panel, the table is not showing, but it is showing when he creates a new JFrame instead, this is because he isn't repainting his panel. Even if he uses a TableModel, he'll still need to repaint after changing the table – Joshua Kissoon Mar 07 '14 at 08:58
  • *"Even if he uses a TableModel, he'll still need to repaint after changing the table"* You completely missed the point. There should only be one table and one model. Change the data in the model, and the view changes. Look ma, no hands (and no p*ssing about with adding and removing components)! – Andrew Thompson Mar 07 '14 at 09:00
  • I'm not saying that using a TableModel is wrong, it is the better way to go, but it doesn't solve the problem. Repainting the panel solves it. – Joshua Kissoon Mar 07 '14 at 09:02
  • *"but it doesn't solve the problem."* Hence 'wrong question'.. You really should pay more attention. -1 as a warning to future visitors that this entire ***question*** is off the mark. – Andrew Thompson Mar 07 '14 at 09:03
0

You need to repaint the table like this

fileTablePanel.repaint();
Seeker
  • 509
  • 4
  • 19