Solved

Right-justifying an icon in JToolBar

Posted on 2002-05-16
6
951 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
[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
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet filter example 37 85
web application structure 18 115
hibernate insert example 13 39
hibernate example issues from command prompt 10 65
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

726 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