Solved

Right-justifying an icon in JToolBar

Posted on 2002-05-16
6
930 Views
Last Modified: 2008-02-26
I'd like to create a toolbar that contains a Help icon justified to the far right (or bottom, if vertically aligned).  Unfortunately, it doesn't look like I'm able to specify layouts in JToolBar.  I have thought about putting the whole JToolBar in a JPanel, and then adding the Help icon to the East of the JPanel, but then I lose the special relationship a JToolBar has with a JFrame (such as the ability to detach or be relocated around the frame).

How can I right-justify an icon in a JToolBar?

Thanks in advance for any help!
Dave
0
Comment
Question by:dmkoelle
6 Comments
 
LVL 16

Assisted Solution

by:heyhey_
heyhey_ earned 100 total points
Comment Utility
try adding invisible component with very big preferred width just before the Help icon.
0
 
LVL 7

Expert Comment

by:tomboshell
Comment Utility
I have not tried changing the Layout of the componenet, but I do use the add method from the Component class.  Located there is also the setLayout method.  You should be able to set the component's layout routine in a similar method as a frame.  This may be an oversimplification but still the capability is there and therefore should be possible.  Try applying a layout manager directly to the toolbar and add the components in such a manner.

I know that this works....
public JToolBar getToolBar(){
          JToolBar jtbar=new JToolBar("Standard");
          GridLayout gl=new GridLayout(2,2);;
          jtbar.setLayout(gl);
          jtbar.add(dateSearchAction);
          jtbar.add(nameSearchAction);
          jtbar.add(symptomSearchAction);

          jtbar.add(advanceSearchAction);

          jtbar.add(exitAction);
          return jtbar;
     }

So, you should just have to play around with the layout managers until you have the desired appearance that you want.

tom
0
 
LVL 7

Accepted Solution

by:
tomboshell earned 100 total points
Comment Utility
whoops...that GridLayout should be (3,2);

I wipped that up after stating that I didn't try something, I have tried it now and it did work for me.  If you can do a grid you also do other things.  The only limit I saw was in the toolBar's actual size rendering.  The size is determined by the number of components and their preferred sizes.  But then you could always have fun and add a JToolBar to a JToolBar with layouts and all specified.  That would start to develop more complex looks.

enjoy,
tom
0
 

Author Comment

by:dmkoelle
Comment Utility
I found both comments helpful.  My actual solution was to do something similar to heyhey's suggestion, although I found that the preferred size didn't need to have a large dimension (I tried Integer.MAX_VALUE and my program froze).  Dimension(1,0) works just fine.

The code below right-justifies an icon on a horizontal toolbar, and bottom-justifies an icon on vertical toolbar.


toolbar.add(left-justified-button-1)
toolbar.add(left-justified-button-2)
toolBar.add(new JComponent() {
    public Dimension getPreferredSize()
    {
        return new Dimension(1,0);
    }
});
toolBar.add(right-justified-button);
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- split points between heyhey_ and tomboshell

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now