Results 1 to 7 of 7

Thread: Layout Problem

  1. #1
    kris5228 is offline Member
    Join Date
    Nov 2011
    Posts
    17
    Rep Power
    0

    Question Layout Problem

    Hello,

    I want to make it so there are buttons in the left side with a BoxLayout and a textarea that covers the right side. This is my progress so far and I don't understand why it doesn't work.

    Java Code:
    public void sets() {
    		JFrame frame = new JFrame(title);
    		frame.setVisible(true);
    		frame.setSize(400, 400);
    		frame.setMinimumSize(new Dimension(400, 400));
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setResizable(true);
    		frame.setLayout(new BorderLayout());
    		
    		JButton button1 = new JButton("Essay");
    		JButton button2 = new JButton("Novelle");
    		JButton button3 = new JButton("Artikel");
    		JTextArea textarea = new JTextArea();
    		
    		JPanel panel2 = new JPanel();
    		panel2.setLayout(new BoxLayout(panel2, BoxLayout.PAGE_AXIS));
    		panel2.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    		panel2.add(textarea);
    		
    		JPanel panel = new JPanel();
    		panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
    		panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    		panel.add(button1);
    		panel.add(Box.createRigidArea(new Dimension(0,10)));
    		panel.add(button2);
    		panel.add(Box.createRigidArea(new Dimension(0,10)));
    		panel.add(button3);
    
    		frame.add(panel, BorderLayout.WEST);
    		frame.add(panel2, BorderLayout.EAST);
    		frame.pack();
    	}
    Panel2 or the textarea doesn't seem to be showing, why is that?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Layout Problem

    You are adding components after the parent container (JFrame) has been realized (set to visible). a) Call validate/revalidate followed by repaint when changing the components in this manner or b) (and better practice) add them prior to calling setVisible and pack

  3. #3
    kris5228 is offline Member
    Join Date
    Nov 2011
    Posts
    17
    Rep Power
    0

    Default Re: Layout Problem

    Putting
    Java Code:
    frame.setVisible(true);
    at the end after frame.pack(); doesn't seem to fix the problem either. The Buttons are showing up but the textarea aren't.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Layout Problem

    How do you know its 'not showing up'? Debug by adding some text to the text area, or set a color border around it to be sure it is 'showing up'.

  5. #5
    kris5228 is offline Member
    Join Date
    Nov 2011
    Posts
    17
    Rep Power
    0

    Default Re: Layout Problem

    Sorry, you're correct, it is working now thanks. Though you got me interested when you said a color border. What is the code for that?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Layout Problem

    Moved from 'New to Java'

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Layout Problem

    Quote Originally Posted by kris5228 View Post
    Though you got me interested when you said a color border. What is the code for that?
    Did you try google? First hit for me
    How to Use Borders (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

Similar Threads

  1. Problem with layout manager
    By mDennis10 in forum New To Java
    Replies: 8
    Last Post: 09-05-2011, 04:02 AM
  2. Layout problem
    By miller.bartek in forum AWT / Swing
    Replies: 3
    Last Post: 01-11-2011, 08:50 PM
  3. problem in managing layout
    By namreen in forum NetBeans
    Replies: 10
    Last Post: 09-04-2010, 10:41 AM
  4. java on mac - gui layout problem
    By dalo in forum AWT / Swing
    Replies: 0
    Last Post: 06-19-2010, 11:15 AM
  5. [SOLVED] Layout Problem
    By casid in forum New To Java
    Replies: 4
    Last Post: 06-03-2009, 12:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •