Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

resize a button

Posted on 1998-03-16
11
Medium Priority
?
168 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
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!

 
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 200 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

971 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