Resize JScrollPane and JTabbedPane

I have a problem resizing some of my components in a Java application. When I resize the main JFrame, they retain their original size instead of filling the surrounding component.

For example, in my program I have a JTabbedPane inside the tab of another JTabbedPane (which is basically a JFrame). I do the tabs added to the inner JTabbedPane, fill all the surrounding space through various .setPreferredSize (). And then when I resize the JFrame by dragging the corner of the window, the tabs of the external JTabbedPane change simply, but the JTabbedPane inside it remains the same.

The same thing happens when I test it with JScrollPane: the outer size of the JTabbedPane with JScrollPane remains the same size.

Below is the code for creating tabbed panels if you see something obvious that I missed. These are tabPane / jsp objects in createEndGamePane () than resizing when resizing a JFrame. 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);
    }

}
+3
source share
3 answers

If you want components to dynamically change, 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 it's all. No setPreferredSize().

+1
source

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

gridbaglayout, (http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html).

, BorderLayout.CENTER, , .

gridbaglayout:

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
0

, .

, , MatrixLayout, MigLayout, TableLayout .

GUI , . , 90% , 10% , Java.

GridBagLayout, - .

0

Source: https://habr.com/ru/post/1735468/


All Articles