JPanel inside CENTER of BorderLayout is given GRID LAYOUT
My code compiles and runs, but I have a behavior problem with my Layout managers.
This is the creation of the JFrame which uses BorderLayout.
I use Border because I like/need the Quality that allows the center region to consume all new free space given by the window size.
Code :
public void buildBattleGridJFrame()
{
JScrollPane centerScroller = new JScrollPane(center,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
setTitle("BattleGrid");
setLayout(new BorderLayout());
add(centerScroller,BorderLayout.CENTER);
setSize(100, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
The scroller is intended to give me access to content not shown because of small window size.
And within this region I was hoping to use the GridLayout. I fill my GridLayout JPanel with smaller JPanels.
I give these smaller JPanels a size with "temp.setSize(100, 100);" but they end up taking what ever shape they want based on filling the grid region, rather then the scrollpane ever getting used to deal with oversizing. I want to start by trying ways to force my mini JPanels to behave in size making them unable to resize. Is there code for that?
EDIT:
I was curious how each of these worked beyond the obvious. it seems sometimes gui's just don't listen to size specifications
setSize(new Dimension(20, 20));
setPreferredSize(new Dimension(20, 20));
setMinimumSize(new Dimension(20, 20));
setMaximumSize(new Dimension(20, 20));
Thanks,
Jonathan
Re: JPanel inside CENTER of BorderLayout is given GRID LAYOUT
Try using the setPreferredSize/setMinimumSize/setMaximum size methods, and should that not help perhaps post an SSCCE
Re: JPanel inside CENTER of BorderLayout is given GRID LAYOUT
Code :
import java.awt.*;
import javax.swing.*;
public class SSCCE extends JFrame {
public static void main(String[] args)
{
//This block creates a panel(which uses grid layout) to put in the center of a border layout
JPanel center = new JPanel();
center.setVisible(true);
center.setBackground(Color.red);
center.setLayout(new GridLayout(10,10));
fillGrid(center, 10*10);
// a scroller is needed for functionality
JScrollPane centerScroller = new JScrollPane(center,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//setting up the frame
SSCCE frame = new SSCCE();
frame.setLayout(new BorderLayout());
frame.add(centerScroller,BorderLayout.CENTER);
frame.setSize(100, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} // end main
public static void fillGrid(JPanel center, int totalGrids)
{
JPanel temp;
for(int i = 0; i < totalGrids; i++)
{
temp = new JPanel();
Dimension size = new Dimension(10, 10);
temp.setSize(size);
temp.setPreferredSize(size);
temp.setMinimumSize(size);
temp.setMaximumSize(size);
// temp.setResizeable(false); not an option for JPanels
center.add(temp);
if(i == 1 || i == 44) //for visual aid
{
temp.setBackground(Color.yellow);
}
}
} //end fillGrid method
}
I know layout managers have magical powers to do what they need to do, just wish there was more documentation to predict their dictator regimes!
Thanks for taking a look,
Jonathan
Re: JPanel inside CENTER of BorderLayout is given GRID LAYOUT
I took out the scroller and it didn't make a difference as I suspected. (but it is needed to test the behavior of window resizing)
Re: JPanel inside CENTER of BorderLayout is given GRID LAYOUT
Try wrapping the scroller in a JPanel with a SpringLayout.
Re: JPanel inside CENTER of BorderLayout is given GRID LAYOUT
ha ha, will do. Can I ask why?
Re: JPanel inside CENTER of BorderLayout is given GRID LAYOUT
JPanels are weak mindless creatures, they need to grow some back bone and stand up to those Layout managers. I am close to giving up and either not using a layout manager, or trying something other then JPanels that can have its size locked.
Re: JPanel inside CENTER of BorderLayout is given GRID LAYOUT
Even shorter SSCCE
This code just uses GridLayout, and fills it with exactly as many JPanels as needed to fill the grid.
in this demo we have 10x10 grid, filled with 100 JPanels, they order properly (10 Columbus, 10 rows)
but I cant get them to stay one size... AND I HAVE TRIED 3000+ lines of code in options.
Code :
import java.awt.*;
import javax.swing.*;
public class SSCCE extends JFrame {
public static void main(String[] args)
{
//This block creates a panel(which uses grid layout)
JPanel center = new JPanel();
center.setVisible(true);
center.setBackground(Color.red);
center.setLayout(new GridLayout(10,10));
//This all does nothing from what I can tell
Dimension forceMe = new Dimension(100, 100);
center.setSize(forceMe);
center.setPreferredSize(forceMe);
center.setMinimumSize(forceMe);
center.setMaximumSize(forceMe);
//This fills the Grid with 100 JPanels using a simple for loop.
fillGrid(center, 10*10);
//setting up the frame
SSCCE frame = new SSCCE();
frame.add(center);
frame.setSize(100, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} // end main
// Attempt to make a method to fills the grid with JPanels that keep their size.
// All the sizing attemps seem to be efforts wasted.
public static void fillGrid(JPanel center, int totalGrids)
{
JPanel temp;
for(int i = 0; i < totalGrids; i++)
{
temp = new JPanel();
Dimension size = new Dimension(10, 10);
temp.setSize(size);
temp.setPreferredSize(size);
temp.setMinimumSize(size);
temp.setMaximumSize(size);
// temp.setResizeable(false); not an option for JPanels
center.add(temp);
if(i == 1 || i == 44) //for visual aid
{
temp.setBackground(Color.yellow);
}
}
} //end fillGrid method
}
Re: JPanel inside CENTER of BorderLayout is given GRID LAYOUT
BorderLayout and GridLayout expand contained components as needed. From the API of BorderLayout:
Quote:
the CENTER component may stretch both horizontally and vertically to fill any space left over
Try integrating the following snippet that uses flowLayout (default for JPanel) - similar behavior should be observed with SpringLayout
Code :
//This fills the Grid with 100 JPanels using a simple for loop.
fillGrid(center, 10*10);
//wrap the GRidLayout
JPanel wrapper = new JPanel();
wrapper.add(center);
//setting up the frame
SSCCE frame = new SSCCE();
frame.add(wrapper);
Re: JPanel inside CENTER of BorderLayout is given GRID LAYOUT
[Mark as solved]
Well, good thing you kept insisting on a wrapper Copeg, I tried it again after 100 other things, and it worked this time.
No idea how I failed to add it right the first time, must have gotten mixed up with my scroller.