Solved

resize a button

Posted on 1998-03-16
11
158 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now