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

Thread: Problem with JPanel [...]

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Problem with JPanel [...]

    	public void init(Board board) {
     
    		JPanel Panel = new JPanel();
    		Panel.setLayout(new GridLayout(board.getWidth(), board.getHeight()));
    		getContentPane().add(Panel, BorderLayout.CENTER);
    		Panel.setBorder(new LineBorder(Color.BLACK));              // it does not work also
     
    		bottomPanel = new JPanel(new FlowLayout());
    		bottomPanel.setSize(1000, 100);
    		bottomPanel.setBackground(Color.darkGray);
    		getContentPane().add(bottomPanel, BorderLayout.SOUTH);
     
    		t1 = new JButton();
    		t2 = new JButton();
    		t3 = new JButton();
     
    		JPanel kit = new JPanel();
    		kit.setSize(400, 90);
    		bottomPanel.add(kit, BorderLayout.WEST);
     
    		kit.add(t1);
    		kit.add(t2);
    		kit.add(t3);
    	}

    I have a JFrame. I added two JPanels to the JFrame. The first one is GridLayout and it should be situated at CENTER. Another one is bottomPanel, and that one should be situated at SOUTH.
    I would like to add two new JPanels(kit and another one) to the bottomPanel.

    Compiler does not show any warning or errors.

    The problem is, that bottomPanel situates at NORTH, not at SOUTH. kit situates at CENTER of bottomPanel, not at WEST.

    How to resolve it?

    Thanks for your suggestions.

    Regards.


  2. #2
    Member
    Join Date
    Apr 2014
    Posts
    135
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Problem with JPanel [...]

    public void init(Board board) 
    {
     
    		JPanel Panel = new JPanel();
    		Panel.setLayout(new GridLayout(3,1); //this should center it for you but I did not test it
    	}

  3. #3
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Problem with JPanel [...]

    jocdrew21, it should, but it does not.

  4. #4
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Problem with JPanel [...]

    Quote Originally Posted by fkmk View Post
    I have a JFrame. I added two JPanels to the JFrame. The first one is GridLayout and it should be situated at CENTER. Another one is bottomPanel, and that one should be situated at SOUTH.
    I would like to add two new JPanels(kit and another one) to the bottomPanel.

    Compiler does not show any warning or errors.

    The problem is, that bottomPanel situates at NORTH, not at SOUTH. kit situates at CENTER of bottomPanel, not at WEST.
    Your description is not very clear. The following is my slightly modified version of your code to make the layout of components more obvious, and I also rectified the setting of component sizes.
    public void init() {
        // To make Panel's location more obvious.
        JPanel northPanel = new JPanel();
        northPanel.add(new JButton("north"));
        getContentPane().add(northPanel, BorderLayout.NORTH);
     
        JPanel Panel = new JPanel();
        Panel.setLayout(new GridLayout(1, 2));
     
        Panel.add(new JButton("1"));
        Panel.add(new JButton("2"));
     
        getContentPane().add(Panel, BorderLayout.CENTER);
        Panel.setBorder(new LineBorder(Color.BLACK)); // it does not work also
     
        JPanel bottomPanel = new JPanel(new FlowLayout());
     
        // bottomPanel.setSize(1000, 100);
        // Use JComponent's setPreferredSize(), not Component's setSize().
        bottomPanel.setPreferredSize(new Dimension(1000, 100));
     
        bottomPanel.setBackground(Color.darkGray);
        getContentPane().add(bottomPanel, BorderLayout.SOUTH);
     
        JButton t1 = new JButton("t1");
        JButton t2 = new JButton("t2");
        JButton t3 = new JButton("t3");
     
        JPanel kit = new JPanel();
     
        // kit.setSize(400, 90);
        // Use JComponent's setPreferredSize(), not Component's setSize().
        kit.setPreferredSize(new Dimension(400, 90));
        bottomPanel.add(kit, BorderLayout.WEST);
     
        kit.add(t1);
        kit.add(t2);
        kit.add(t3);
    }
    With the above code, Panel, which has a GridLayout, is located in the CENTER of the window. bottomPanel, which contains 3 buttons, t1, t2 and t3, is located in the SOUTH of the window.

    The 3 buttons in bottomPanel are located in the center of the panel, and this is because you're using FlowLayout for bottomPanel: "JPanel bottomPanel = new JPanel(new FlowLayout())". You'll need to change this to BorderLayout in order for "bottomPanel.add(kit, BorderLayout.WEST)" to take effect.

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

    fkmk (May 22nd, 2014)

  6. #5
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Problem with JPanel [...]

    jashburn, thank you!

Similar Threads

  1. Problem with updating JPanel.
    By alegra in forum AWT / Java Swing
    Replies: 0
    Last Post: July 17th, 2012, 04:41 AM
  2. Replies: 3
    Last Post: February 9th, 2012, 11:35 PM
  3. Problem with JPanel
    By fahien in forum AWT / Java Swing
    Replies: 4
    Last Post: March 4th, 2011, 07:01 AM
  4. [SOLVED] Extends JPanel painting problem
    By Philip in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 28th, 2010, 03:16 PM
  5. problem with drawing images on JPanel
    By Asido in forum What's Wrong With My Code?
    Replies: 13
    Last Post: July 19th, 2010, 03:07 PM