Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: JPanel inside CENTER of BorderLayout is given GRID LAYOUT

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default 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.
    	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
    Last edited by JonLane; February 24th, 2012 at 09:46 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: JPanel inside CENTER of BorderLayout is given GRID LAYOUT

    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

  4. #4
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default 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)

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JPanel inside CENTER of BorderLayout is given GRID LAYOUT

    Try wrapping the scroller in a JPanel with a SpringLayout.

  6. #6
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: JPanel inside CENTER of BorderLayout is given GRID LAYOUT

    ha ha, will do. Can I ask why?

  7. #7
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default 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.

  8. #8
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default 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.
    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
    }

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JPanel inside CENTER of BorderLayout is given GRID LAYOUT

    BorderLayout and GridLayout expand contained components as needed. From the API of BorderLayout:

    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

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

  10. The Following User Says Thank You to copeg For This Useful Post:

    JonLane (February 25th, 2012)

  11. #10
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default 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.

Similar Threads

  1. Replies: 6
    Last Post: February 21st, 2012, 09:41 PM
  2. Replies: 1
    Last Post: April 14th, 2011, 07:50 AM
  3. Grid bag layout inside grid bag layout
    By kiddkoder in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 29th, 2011, 08:07 AM
  4. How to align the items in a form (grid layout)?
    By onlybarca in forum AWT / Java Swing
    Replies: 4
    Last Post: November 27th, 2010, 11:38 PM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM