Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 573
  • Last Modified:

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

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
FaithNoMore
Asked:
FaithNoMore
1 Solution
 
CEHJCommented:
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
 
FaithNoMoreAuthor Commented:
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
 
CEHJCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
FaithNoMoreAuthor Commented:
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
 
sct75Commented:
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
 
CEHJCommented:
>>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
 
FaithNoMoreAuthor Commented:
>>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
 
CEHJCommented:
I see, yes. You could also call setMinimumSize set to the length of the longest caption
0
 
antonsigurCommented:
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
 
FaithNoMoreAuthor Commented:
Changing the layout manager worked, and so did antonsigur's idea (setMin/setMax).  Since Sct75 answered first he/she wins

Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now