ginou
ginou

Reputation: 63

JTable not visible in JPanel

As I am a beginner with java swing (I'm using netbeans), I am having trouble integrating tables in my gui. I have a tab with a panel with research options in a db, and I want to add beneath this panel the produced table with the research results. With swing, i have put a JPanel 'researchPanel' within my tab and what I do is the following:

        table = my_research_function(options);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        JScrollPane pane = new JScrollPane(table);
        pane.setViewportView(table);
        //resultsPanel.setLayout(new BorderLayout());  
        resultsPanel.add(pane);

        // I'm just using this to ensure i get a table 
        // with correct results - works fine
        JOptionPane.showMessageDialog(null, new JScrollPane(table));

As I saw in the produced code by swing, resultsPanel already has a layout. Just in case, i did a resultsPanel.setLayout(new BorderLayout()) and resultsPanel.add(pane, BorderLayout.CENTER) and I saw some shades in my panel but still no data.

Furthermore, in the Design tab of netbeans, I've set the panel to Auto-Resize both Vertical and Horizontal as the results table can be quite big. What am I missing?

Upvotes: 1

Views: 2296

Answers (1)

alex2410
alex2410

Reputation: 10994

Swing component can have only one parent component.

You can't see your JTable in resultsPanel because of the following reasons

At first you create JScrollPane pane = new JScrollPane(table);

which add to resultsPanel,

but then you override parent component of your table here JOptionPane.showMessageDialog(null, new JScrollPane(table));.

So remove last line and your JTable will be visible.

Upvotes: 2

Related Questions