Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem resizing JScrollPane and JTabbedPane

I have a problem with resizing some of my components in my java application. When I resize my main JFrame they keep their previous size instead of filling out the surrounding component.

For example I have in my program a JTabbedPane within a tab of another JTabbedPane (which is located within the main JFrame). I make tabs added to the inner JTabbedPane fill the whole surrounding space via various .setPreferredSize(). And then when I resize the JFrame by dragging the corner of the window, the tabs out the outer JTabbedPane resizes just fine but the JTabbedPane located within it stays the same old size.

The same thing with happens when I test it with a JScrollPane: the outer JTabbedPane resizes by the JScrollPane stays the same size.

Here below is the code for creating the tabbed panels in case you can see something obvious I missed. It's the tabPane/jsp objects in createEndGamePane() than wont resize when the JFrame is resized. mainTabPane resizes as it should.

class MyFrame extends JFrame {
   private JTabbedPane mainTabPane;
   private JPanel endGameInfo;
   //....

   private void createTabbedCenterPane(){
       this.mainTabPane = new JTabbedPane();

       this.endGameInfo = new JPanel();

       this.createEndGamePane();

       this.mainTabPane.addTab("Turneringschema", null, this.scheduleHolder, "Spelschemat för turneringen");
       this.mainTabPane.addTab("Grupper & Pooler", null, this.poolInfo, "Information om grupper, lag och pooler");
       this.mainTabPane.addTab("Slutspelsträd", null, this.endGameInfo, "Information om slutspelen");

       this.add(this.mainTabPane, BorderLayout.CENTER);
    }

    private void createEndGamePane(){
        JTabbedPane tabPane = new JTabbedPane();
        tabPane.setPreferredSize(this.endGameInfo.getSize());

        for (int i = 0; i < this.tournament.data.getGroups().size(); i++) {
            EndGame e = this.tournament.data.getEndGames().get(i);;

            //Create the gameTree
            EndGameTreeCanvas gameTree = new EndGameTreeCanvas(e, this.endGameInfo.getSize());

            //Create the ScrollPane
            JScrollPane jsp = new JScrollPane(gameTree);
            jsp.setPreferredSize(tabPane.getSize());
            jsp.setBorder(BorderFactory.createEmptyBorder());


            //Add to the endGameIndo panel
            tabPane.addTab(this.tournament.data.getGroups().get(i).getName(), jsp);
        }

        this.endGameInfo.add(tabPane);
    }

}
like image 616
Snail Avatar asked May 13 '26 07:05

Snail


1 Answers

If you want the components to resize dynamically, you should avoid telling Swing what their size is. Instead, how about this:

JTabbedPane tabs = new JTabbedPane();
JScrollPane inner1 = new JScrollPane(myOtherComponent);
tabs.add("Scroll", inner1);
JTabbedPane inner2 = new JTabbedPane();
tabs.add("Tabs", inner2);

And that's all. No setPreferredSize().

like image 113
Konrad Garus Avatar answered May 15 '26 21:05

Konrad Garus