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

Thread: problem with BorderLayout

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    29
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default problem with BorderLayout

    I'm trying to layout a JPanel with a button and a scrollable textarea so that the button is at the top and the textarea takes up all the available remaining space. From what I read borderlayout should be able to do that but when I run it, all I get is a blank container. When I change to borderlayout to the default flowlayout the button and textarea show up but don't fill out the panel. So I have 2 questions: why is nothing showing up and when it does show up will this do what I want it to do?

    package Test;
     
    import java.awt.*;
    import javax.swing.*;
     
    public class TestWindow
    {
    	private JFrame frame;
    	private CustomPanel panel;
     
    	public TestWindow()
    	{
    		frame = new JFrame("Test");
    		frame.setPreferredSize(new Dimension(800,600));
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		panel = new CustomPanel();
    		//panel.setLayout(new BorderLayout());
    		frame.add(panel);
    		frame.pack();
    		frame.setVisible(true);
    	}
     
    	public static void main(String[] args)
    	{
    		SwingUtilities.invokeLater( new Runnable()
    		{
    			public void run()
    			{
    				TestWindow test = new TestWindow();		
    			}
    		});
    	}
     
    }
     
    class CustomPanel extends JPanel
    {
    	private JTextArea display;
     
    	public CustomPanel()
    	{
    		super();
    		JButton button = new JButton("test");
    		add(button, BorderLayout.PAGE_START);
    		display = new JTextArea();
    		display.setText("test message aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");		
    		JScrollPane scrollpane = new JScrollPane(display);
    		scrollpane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    		add(scrollpane, BorderLayout.CENTER);		
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: problem with BorderLayout

    JPanel's layout is not BorderLayout by default, it's FlowLayout. You have to set JPanel's Layout to be BorderLayout:

    // in the CustomPanel() constructor:
    setLayout( new BorderLayout() );

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    29
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: problem with BorderLayout

    I know, I forgot to uncomment the line before i copy-pasted the code into my post. (it's right below panel = new CustomPanel(); on line 17)

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: problem with BorderLayout

    I'm not getting the results you describe. I have a "test" button across the top and the rest of the panel filled with the JTextArea, BECAUSE the layout is set FIRST and then the components are added to the panel. Do things in the correct place in the correct order. Put the setLayout() statement near the top of the constructor as I suggested.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    Harry Blargle (March 8th, 2014)

  6. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    29
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: problem with BorderLayout

    I see, I had no idea that mattered, but that explains it. It does indeed work now.

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: problem with BorderLayout

    Just think about it a bit - I hope you have.

    Your original version creates the panel in the TestClass() constructor by calling the CustomPanel() constructor. The CustomPanel() constructor adds all of the components to the panel while it has the default FlowLayout. After the CustomPanel() constructor is done, control returns to the TestClass() constructor which then sets the panel's layout to BorderLayout, but at that point it's too late. The components have already been added to the panel, and changing the layout has no effect - at least not until more components are added to the panel, and then I'm not sure what will happen. I think it won't be pleasant. In fact, that might be why you sometimes saw a blank. Perhaps the pack() statement did something to the panel using the new layout, but I'm not sure.

Similar Threads

  1. BorderLayout makes everything disappear
    By jason3460 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 6th, 2013, 03:35 AM
  2. [SOLVED] Sinple GUI Program which uses the BorderLayout.
    By javahelp123456 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 3rd, 2012, 03:25 PM
  3. BorderLayout and getHeight/getWidth
    By DOLZero in forum Java Theory & Questions
    Replies: 2
    Last Post: May 5th, 2012, 08:59 AM
  4. [SOLVED] JPanel inside CENTER of BorderLayout is given GRID LAYOUT
    By JonLane in forum AWT / Java Swing
    Replies: 9
    Last Post: February 25th, 2012, 12:20 PM
  5. BackgroundPanel.java cannot be placed in BorderLayout.NORTH
    By chopficaro in forum AWT / Java Swing
    Replies: 7
    Last Post: August 31st, 2010, 06:27 PM