?
Solved

JToolBar; I would like all the buttons on the toolbar to be the same size

Posted on 2003-03-08
10
Medium Priority
?
536 Views
Last Modified: 2010-03-31
Here is my current ToolBar Class, which extends JToolBar, the buttons come from another class that extends JButton (nothing special in that class, mostly contains certain mouselisteners for look/feel effects).
As you may guess after reading my code, I dont fully understand how 'Component.setPreferredSize(Dimension)', works.  I am basically new to programming GUIs (currently a programming student, but we are not covering swing at all, so I am self taught) and have worked through most of my other problems, but this particular one is annoying since I have several JFrames that use this class.

What I am trying to accomplish:  I want all my buttons on the tool bar to be the same size (ie. like in Internet Explorer).

No points for this answer, but does anyone know of a place where I can download images for my buttons, I am by no means an artist :).


package mailtest;

import javax.swing.*;
import java.awt.*;
import java.util.Vector;
import java.awt.event.*;

public class ToolBar extends JToolBar
{
  Vector ToolButtons = new Vector( );
  Insets margins;
  String[] labels = new String[4];
  Dimension MaxButtonSize;


  public void setToolBarText( )
  {
    JButton comp;
    for(int i=0; i<ToolButtons.size(); i++)
    {
      comp = (ToolBarButton)ToolButtons.get(i);
      ((ToolBarButton)ToolButtons.get(i)).setText(comp.getToolTipText());
    }
  }

  public void getLargestLabel( )
  {
    int MaxLength = 0;
    int MaxSize = 0;

    for(int i=0; i<this.labels.length; i++)
    {
      String current = this.labels[i];
      if(current.length() > MaxLength)
      {
        MaxLength = current.length();
        MaxSize = i;
      }
    }
    this.MaxButtonSize = //Sets PreferredSize to the longest Button.
        ((ToolBarButton)ToolButtons.get(MaxSize)).getPreferredSize();
  }

  public void setButtonSize( )
  {
    for(int i=0; i<ToolButtons.size(); i++)
    {
      ((ToolBarButton)ToolButtons.get(i)).setPreferredSize(MaxButtonSize);
    }
  }

//Constructor numb 1
  public ToolBar(String[] labels)
  {
    margins = new Insets(0, 0, 0, 0);
    this.labels = labels;
    for(int i=0; i<labels.length; i++)
    {
      ToolButtons.addElement(new ToolBarButton(labels[i]));
      this.add((ToolBarButton) ToolButtons.get(i));
    }
   // getLargestLabel();
    //setButtonSize();
  }

//Constructor Numb 2
  public ToolBar(String[] labels, String[] icon)
  {
    margins = new Insets(0, 0, 0, 0);
    this.labels = labels;
    for(int i=0; i<labels.length; i++)
    {
      ToolButtons.addElement(new ToolBarButton(labels[i], icon[i]));
      this.add((ToolBarButton) ToolButtons.get(i));
      //((ToolBarButton)ToolButtons.get(i)).setPreferredSize(new Dimension(60, 50));
    }
    //Attempt at making buttons the same size.
    getLargestLabel();
    setButtonSize();
  }
  public static void main(String[] args)
  {
    String[] all = {"Reply", "Delete", "LONGNAMELONGNAME", "Address"};
    String[] icon = {"Left.gif", "Right.gif", "RotCCUp.gif",
        "TrafficRed.gif"};
    ToolBar toolBar1 = new ToolBar(all, icon);

    Frame frame = new Frame();
    frame.setLayout(new BorderLayout());
    frame.add(toolBar1, BorderLayout.NORTH);
    // frame.setLayout(new FlowLayout());
    // frame.add(toolBar1);
    toolBar1.setToolBarText();
    frame.setSize(300, 400);
    frame.setVisible(true);
   
  }
}
0
Comment
Question by:FaithNoMore
[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
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8093986
We can't run this code as ToolBarButton is not present. The button size should be determined by the size of the glyphs, so just make sure they're all the same size.
0
 
LVL 1

Author Comment

by:FaithNoMore
ID: 8094384
Sorry, I didnt think you would have to run the code, I am at work atm and dont have access to it.  I had the same problem when adding JButtons to a JScrollBar.  

The size of the buttons seems to be determined by the text label.  I've tried added spaces around my labels (ie. "  Reply  "), but java seems to trim them down.  

When I get home I can post my Button code.

By the way, what are glyphs, I;ve never heard of that term before.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8094402
By glyphs, i mean the images that are used on the buttons. If they merely have text captions, then you're right - the size would be determined by the captions.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:FaithNoMore
ID: 8094494
Here are the pictures I was using.  
http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/SimpleExamples/JToolBars/images/

I was using them for testing purposes, until I get usefull pictures.

Is the only way to make the buttons the same size, to make each Image the same (ie. add some padding on the side, to lengthen them)?
0
 
LVL 1

Accepted Solution

by:
sct75 earned 160 total points
ID: 8094726
Generally speaking, when any Layout manager is about to determine how to layout various GUI component, i.e, Panels, buttons, on a container, it will ask each component's preferred size. If available, most layout managers will grant such size to the individual component if it would not be beyond the whole container boundry or violate the layout algorithm. Though sometimes different layout managers may also care about the component's minimal or maximal size, most of them pay more attention on its preferred size during layout.

Button classes, as you realize, may use its contents to determine its preferred size, such as text labels or icons or both. You can call setPreferredSize(Dimension) to explicitly set the button preferred size, which might be bigger than the size of its content. By this means, you may reinforce that each button will have the same size.

However, as I mentioned above, different layout manager may have different layout strategy that might somehow influence the actual size of the button appearance. So, one way of doing is to make sure your icons or the contet to display have same size; the other way is to use preferred-size friendly layout managers, such as FlowLayout, etc.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8094799
>>Is the only way to make the buttons the same size

It looks to me that they're the same size anyway so you shouldn't have any problem.
0
 
LVL 1

Author Comment

by:FaithNoMore
ID: 8094915
>>It looks to me that they're the same size anyway so you shouldn't have any problem.

Its the labels that are giving me my problem.  If I were to remove the labels, the size of each button is the same.



TO: sct75

The JToolBar class has a default Layout (I am not sure which one).  I will see if I have the ability to change it.  Thanks for the info on setPreferredSize()
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8094974
I see, yes. You could also call setMinimumSize set to the length of the longest caption
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8097550
setPreferredSize is not working... do setMinimumSize and setMaximumSize too! that should fix it (I don't know why, but this preferred size never works for me..) you can create one dimensions:

Dimensions d = new Dimensions(30,40);

component.setPreferredSize(d);
component.setMaximumSize(d);
component.setMinumSize(d);

give it a try!
0
 
LVL 1

Author Comment

by:FaithNoMore
ID: 8097745
Changing the layout manager worked, and so did antonsigur's idea (setMin/setMax).  Since Sct75 answered first he/she wins

Thanks!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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…
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:
Suggested Courses
Course of the Month9 days, 6 hours left to enroll

765 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