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 7 of 7

Thread: Need help with very specific panel layout

  1. #1
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    718
    Thanks
    0
    Thanked 132 Times in 115 Posts

    Default Need help with very specific panel layout

    Hi there.

    For the last couple of days I tried to lay out a panel in a specific way but no layoutManager I picked was able to get it quite right.
    So I am asking here whether someone can suggest to me what kind of layout manager(s) to use for this.

    Here is a picture of how the end result should look like:
    Layout.jpg

    It looks like the BorderLayout is the perfect choice, but the problem is, when I use the BorderLayout the top panel will never grow in height as rows are needed.
    The top will always have a fixed height of only a single row.

    I have also tried other layout managers most were not able to fix the top panels height, others had problems with the size restrictions on the toolbar or came with other problems.

    I would very much appreciate any advice on this, thank you very much.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,447
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: Need help with very specific panel layout

    Why doesn't the top panel grow using BorderLayout? Can you show us an SSCCE?

    BorderLayout seems reasonable, or BoxLayout maybe mixed with a JSplitPane in the center.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    718
    Thanks
    0
    Thanked 132 Times in 115 Posts

    Default Re: Need help with very specific panel layout

    I do not know why the top and bottom panels are not correctly growing in height. I have prepared an SSCCE, but it uses the MigLayout for the toolbar constraints. I hope this is not a problem:
    public class LayoutTest extends JPanel {
     
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					JFrame frame = new JFrame();
    					frame.setContentPane(new LayoutTest());
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	public LayoutTest() {
    		setLayout(new BorderLayout(0, 0));
     
    		JPanel top = new JPanel();
    		add(top, BorderLayout.NORTH);
     
    		for (int i = 0; i < 20; i++) {
    			JButton newBtn = new JButton("Button: "+i);
    			top.add(newBtn);
    		}
     
    		JPanel btm = new JPanel();
    		add(btm, BorderLayout.SOUTH);
     
    		for (int i = 0; i < 20; i++) {
    			JButton newBtn = new JButton("Button: "+i);
    			btm.add(newBtn);
    		}
     
    		JPanel center = new JPanel();
    		add(center, BorderLayout.CENTER);
    		center.setLayout(new MigLayout("", "[75%,grow][25%,grow]", "[grow]"));
     
    		JPanel body = new JPanel();
    		center.add(body, "cell 0 0,grow");
     
    		JToolBar toolBar = new JToolBar();
    		center.add(toolBar, "cell 1 0,grow");
     
    		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    		toolBar.add(tabbedPane);
     
    		JPanel panel = new JPanel();
    		tabbedPane.addTab("New tab", null, panel, null);
     
    		JPanel panel_2 = new JPanel();
    		tabbedPane.addTab("New tab", null, panel_2, null);
     
    		JPanel panel_1 = new JPanel();
    		tabbedPane.addTab("New tab", null, panel_1, null);
     
    	}
     
    }

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,447
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: Need help with very specific panel layout

    Interesting. Upon doing some more research, it turns out that the problem is with your FlowLayout. BorderLayout respect's the TOP component's preferred size, however, FlowLayout doesn't set its preferred size to make sure every row is visible. FlowLayout's preferred size assumes only one row of components will be added.

    To get around this, you can us a GridLayout or a BoxLayout, or you could use camickr's WrapLayout: Wrap Layout Java Tips Weblog
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    718
    Thanks
    0
    Thanked 132 Times in 115 Posts

    Default Re: Need help with very specific panel layout

    This is even more intersting since the FlowLayout claims that it would add additional Rows as needed.
    I will see if I can get it to work and update the thread.

    --- Update ---

    I cant seem to find a LayoutManager that does what the FlowLayout promises. (at least not without a lot of work)
    The problem is that I need to be able to dynamically add / remove Components to / from the top panel and have it resized automatically.

    Can you give me any suggestion what Layout might do this job without having to write too much code around it?
    Alternatively I guess I would create my own Layout which extends the FlowLayout and "fixes" this "feature".

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,447
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: Need help with very specific panel layout

    Doesn't Camickr's WrapLayout do exactly what you want?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    718
    Thanks
    0
    Thanked 132 Times in 115 Posts

    Default Re: Need help with very specific panel layout

    I am sorry, I must have overlooked that. It does indeed look exactly like what I need, thank you.

Similar Threads

  1. GUI Layout - Does anyone know how to set a layout?
    By mikemontesa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 20th, 2013, 03:35 PM
  2. Help with bouncing on a panel
    By hellhunt in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 9th, 2012, 07:28 PM
  3. [SOLVED] Layout Manager and Keyboard Panel
    By DOLZero in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 22nd, 2012, 12:19 PM
  4. Replies: 1
    Last Post: April 14th, 2011, 07:50 AM
  5. 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