Solved

Right-justifying an icon in JToolBar

Posted on 2002-05-16
6
937 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
ID: 7015625
try adding invisible component with very big preferred width just before the Help icon.
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 7015667
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
ID: 7015678
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
ID: 7021738
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
ID: 8766575
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

919 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

13 Experts available now in Live!

Get 1:1 Help Now