views:

447

answers:

3

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);
    }

}
+1  A: 

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().

Konrad Garus
A: 

You should read the swing layout tutorial : http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html

The layout manager i prefer is the gridbaglayout that allow maximum flexibility ( http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html ).

In particular, you should not use the BorderLayout.CENTER if you wish the component to fill all the available space around him.

an example with a gridbaglayout manager :

this.add(this.mainTabPane,
  new GridBagConstraints(
    0,0, // position
    1,1, // size
    1.0,1.0, // fill ratio
    GridBagConstraints.CENTER, GridBagConstraints.BOTH, // position inside the cell
    new Insets(2,2,2,2),0,0)); // margins
Thierry
BorderLayout.CENTER is used exactly for filling space.
Konrad Garus
A: 

As others have said, you need to be using layout managers to handle your GUI's layout requirements.

However I recommend that your primary layout manager be a table-based layout like MatrixLayout, MigLayout, TableLayout, or any of a number of other free ones.

Using a table-based layout will simplify your GUI coding by about an order of magnitude, because it will vastly reduce the amount of layout nesting required. I find that 90% of my GUI is done with one layout manager, and the remaining 10% draws on the simpler layout managers provided with Java.

I have never had cause to use GridBagLayout and I absolute do not recommend it - it's unnecessarily complicated and make ongoing maintenance harder.

Software Monkey