problems in laying out panels

Hi all,

I'm experiencing a strange problem in laying out panels in my JFrame. This is the situation: JFrame has the borderlayout, the north is assinged to a JToolBar, the center is assigned to another JSplitPane, and the south is assigned to another JPanel. Nothing is placed at the east and west sides. The problem is that

1) the center is not using all the size of the window and remains littler than the whole window. Moreover, it seems to be cut depending on the screen resolution: sometimes the bottom part of the JSplitPane is not shown on the window, and sometimes the horizontal right part is cut on screens with a different resolution of mine. I didn't set the size (preferred, min, max) of the any panel.

The following is the part of code that builds up the JFrame layout:

publicvoid initGUI(){

// sets the layout

this.setLayout(new BorderLayout() );

// builds up the menu bar

JMenu databaseMenu =new JMenu("Database");

this.connectToDatabaseMenuItem = ComponentBuilder.createJMenuItem("Connetti al server database",CommandStrings.CONNECT,this);

JMenuItem exit = ComponentBuilder.createJMenuItem("Esci",CommandStrings.EXIT,this);

databaseMenu.add(this.connectToDatabaseMenuItem);

databaseMenu.addSeparator();

databaseMenu.add(exit);

JMenu actionMenu =new JMenu("Azioni");

JMenuBar bar =new JMenuBar();

bar.add(databaseMenu);

bar.add(actionMenu);

JMenu toolsMenu =new JMenu("Strumenti");

JMenuItem runGC = ComponentBuilder.createJMenuItem("Pulizia memoria", CommandStrings.RUN_GC,this);

toolsMenu.add(runGC);

JMenu helpMenu =new JMenu("Help");

JMenuItem credits = ComponentBuilder.createJMenuItem("Credits", CommandStrings.CREDITS,this);

helpMenu.add(credits);

bar.add(helpMenu);

bar.add(toolsMenu);

this.setJMenuBar(bar);

// builds up the toolbar

JToolBar toolBar =new JToolBar();

toolBar.setLayout(new BoxLayout(toolBar,BoxLayout.LINE_AXIS));

this.connectToDatabaseButton = ComponentBuilder.createJButton("Connetti",CommandStrings.CONNECT,this);

toolBar.add(this.connectToDatabaseButton);

toolBar.add(Box.createHorizontalGlue());// fill the space so that the logo appears on the right side of

// toolbar. The glue is invisible

toolBar.addSeparator();

toolBar.add(new ImagePanel(ComponentBuilder.getPoweredLogoPath()));

this.add(toolBar,BorderLayout.NORTH);

// add the status/memory bar at the bottom

this.statusBar = MainFrameStatusBar.getInstance();

this.statusBar.setText("Ready");

this.add( this.statusBar, BorderLayout.SOUTH);

//add the main panel at the center

this.mainPanel =new MainPanel(this,actionMenu);

this.add(mainPanel, BorderLayout.CENTER);

this.add(mainPanel);

// all done

this.centerIntoScreen(0.9);

}

Any idea about how to force the center panel to adapt itself to the rest of the window size?

Thanks,

Luca

[4167 byte] By [cat4hirea] at [2007-11-26 22:33:37]
# 1

You are adding a MainPanel to the center. That could have anything in it with any layout, we'd just be guessing.

If you call "mainPanel.setBorder(BorderFactory.createLineBorder(Color.RED, 1));" then the component will be outlined in red - I suspect you will find that this fits the center as expected and the content of that panel is your problem.

Maybe, maybe not.

itchyscratchya at 2007-7-10 11:40:46 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for your help, you were right since the mainpanel was laying out itself in all the center, and the jsplitpane was not.

This is the situation before, in mainPanel:

public class MainPanel extends JPanel{

public MainPanel(){

super();

this.splitter = new JSplitPane();

this.splitter.setLeftComponent(this.leftPanel);

this.splitter.setRightComponent(this.rightPanel);

this.add(this.splitter);

this.splitter.setResizeWeight(0.1);

}

}

I've tried to set the layout of the mainpanel (that simply contains the split pane) at flowlayout, but nothing, while using a borderlasyout it works:

public class MainPanel extends JPanel{

public MainPanel(){

super();

this.setLayout(new BorderLayout());

this.splitter = new JSplitPane();

this.splitter.setLeftComponent(this.leftPanel);

this.splitter.setRightComponent(this.rightPanel);

this.add(this.splitter, BorderLayout.CENTER);

this.splitter.setResizeWeight(0.1);

}

}

This is a good solution for me, and it works. However I don't understand why it is behaving thus, can anyone explain me?

Thanks,

Luca

cat4hirea at 2007-7-10 11:40:46 > top of Java-index,Desktop,Core GUI APIs...
# 3
I don't understand why it is behaving thusIf you read the documentation for FlowLayout and BorderLayout it should become clear that they are both working exactly as described.
itchyscratchya at 2007-7-10 11:40:47 > top of Java-index,Desktop,Core GUI APIs...