Solved

Right-justifying an icon in JToolBar

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mockito example issue 8 76
micro services spring boot application error 3 199
Java Timer (static) 9 46
eclipse console opening separately 2 21
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

825 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