?
Solved

Set two toolbars

Posted on 2003-03-07
3
Medium Priority
?
209 Views
Last Modified: 2011-08-18
How can I put two JToolBar in a frame? The following code puts only one toolbar.

import java.awt.*;
import javax.swing.*;

public class Test2
{

    public static void main(String[] args)
    {
     TestFrame frame=new TestFrame();
     frame.show();
    }

}

class TestFrame extends JFrame
{

    TestFrame()
    {
     setSize(300, 300);
     JToolBar toolBar1= new JToolBar();
     toolBar1.add(new JButton("Test1"));
     
          JToolBar toolBar2= new JToolBar();
     toolBar2.add(new JButton("Test2"));
     
     Container contentPane = getContentPane();
     contentPane.add(toolBar1, BorderLayout.NORTH);
          contentPane.add(toolBar2, BorderLayout.NORTH);
    }


}
0
Comment
Question by:asukai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 8091585
 This is because you try to add them both at the NORTH location so the second overlaps the first. Change this line:

  contentPane.add(toolBar2, BorderLayout.NORTH);

  to

  contentPane.add(toolBar2, BorderLayout.SOUTH);

  and you will see them both.
0
 

Author Comment

by:asukai
ID: 8093458
Yes, but this will display the second toolbar to the bottom of the frame. How can I display the second toolbar just below the first one?
0
 
LVL 35

Accepted Solution

by:
girionis earned 160 total points
ID: 8093485
 You have to change the layout then. Try using GridLayout instead and add it to a panel. Then add the panel to the North of he frame:

TestFrame()
     {
          setSize(300, 300);
          JToolBar toolBar1= new JToolBar();
          toolBar1.add(new JButton("Test1"));

          JToolBar toolBar2= new JToolBar();
          toolBar2.add(new JButton("Test2"));
          Container contentPane = getContentPane();
          JPanel panel = new JPanel();
          panel.setLayout(new GridLayout(2, 0));
          panel.add(toolBar1);
          panel.add(toolBar2);
          contentPane.add(panel, BorderLayout.NORTH);
         
     }
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question