Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

resize a button

Posted on 1998-03-16
11
Medium Priority
?
167 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 “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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

721 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