Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Right-justifying an icon in JToolBar

Posted on 2002-05-16
6
Medium Priority
?
978 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 400 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 400 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

916 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