Solved

resize a button

Posted on 1998-03-16
11
161 Views
Last Modified: 2010-04-16
Hi,

  I believe this should be easy but I just can't do it :-(
  I have several buttons in one panel and I am using GridBagLayout. However, the buttons have different default sizes(due to the text on it). I tried to use Button.resize(20,60) in order to make their sizes equal, but it didn't seem to work. Can anyone tell me what is the proper way to do it? Thank you.
0
Comment
Question by:sunjian061197
11 Comments
 

Author Comment

by:sunjian061197
ID: 1233590
Edited text of question
0
 
LVL 1

Expert Comment

by:froderik
ID: 1233591
Try the method setSize( int width, int height ) instead. It is defined in Component which is the superclas of Button. Another version of setSize() takes a Dimension as argument.

Hope this helps,
Fredrik
0
 

Author Comment

by:sunjian061197
ID: 1233592
Hi froderik,

  I am sorry but I have actually used setSize() method but it didn't work either...

Sun Jian
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Expert Comment

by:froderik
ID: 1233593
Why didn't it work? Didn't the source compile or didn't the Button change or did you get some runtime exception? Some code might help finding the problem.
0
 

Author Comment

by:sunjian061197
ID: 1233594
The program compiles well and no exception was thrown. But the buttons still remain their default sizes. The relevant codes:
....
  gbLayout = new GridBagLayout();
  setLayout(gbLayout);
  gbc = new GridBagConstraints();
  Button btTest = new Button(" Test ");
  gbLayout.setConstraints(btTest,gbc);
  add(btTest);
  btTest.setSize(60,20);
....
0
 
LVL 1

Expert Comment

by:froderik
ID: 1233595
Have you tried to set the size before the button is added to the Panel?

There is another method named setBounds(), maybe it works instead....

public void setBounds( int x, int y, int width, int height )
public void setBounds( Rectangle r );

I have no documentation about what the difference between these methods might be and I have to admit that I am no Java graphics expert.
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1233596
The call to resize will change the member values for your button, but wont actualy redraw them.

You must explisitly call either the pannells paint() method or the buttons paint() method, though calling the pannels is a better option since it will avoid overrwriting of other controlls.


0
 
LVL 1

Expert Comment

by:froderik
ID: 1233597
jpk:
Although I accept your good answer I still wonder if it wouldn't work to change the size before adding the Button to the panel?

0
 

Author Comment

by:sunjian061197
ID: 1233598
Hi jpk and froderik:

  I have tried the following:

1. use setSize() before adding the button to the panel
2. use setSize() after adding the button to the panel and then call Button.repaint()
3. use setSize() after adding the button to the panel and then call Panel.repaint()
4. use setBounds() instead of setSize() and repeat 1.2.3.
5. use Button.invalidate() instead of repaint()

but none of the above worked... really don't know what I am missing....

0
 
LVL 5

Accepted Solution

by:
fontaine earned 50 total points
ID: 1233599
The layout manager is looking at the component *preferred size* to do the layout. Because of this, modifying the size of the button with setSize() will simply have no effect. What you have do to is sub-class Button and override the getPreferredSize() method.

Example:

public class FixedSizeButton extends Button
{
 private int width;
 private int heigth;

 public FixedSizeButton(String label, int width, int heigth)
 {
  super(label);

  this.width = width;
  this.heigth = heigth;
 }

 public Dimension getPreferredSize()
 {
  return new Dimension(width,heigth);
 }

}

This is to answer your question. Now, if you have, for example, a row of three buttons in your layout, the best solution is to put them all in one panel using GridLayout and rivet it to your GridBagLayout panel.
0
 

Author Comment

by:sunjian061197
ID: 1233600
Hi,

  Thanks fontaine, both for ur answer and suggestion. I will use GridLayout then.

Sun Jian
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 85
java 8 lambda expresssions exception handling 3 92
How to convert String matching to regex in java 4 34
Selenium docs api java index 3 20
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

831 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