Solved

resize a button

Posted on 1998-03-16
11
164 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
[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
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
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

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Certificat to iSeries KeyStore 1 30
iterator/ListIterator approach 17 68
How  can  i  resolve  HTTP Status 404 -? 8 78
Coding for the first time 9 100
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

734 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