Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2004

Tip: Looking for answers? Try searching our database.

Button Size & Swing

Thread view: 
Glodalec - 09 Mar 2004 07:12 GMT
Hi !

Can someone tell me, how to set Dimension of a Button to exactly size in
GridLayout.

Below is my example which doesn't work.

import java.awt.*;
import javax.swing.* ;

public class ButtonTest extends JFrame
{  
  public ButtonTest() {
      initComponents();
  }

  private void initComponents() {
      setDefaultLookAndFeelDecorated(false) ;
      setTitle("Button test") ;
      setSize( new Dimension(400,250));            
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
      getContentPane().setLayout(new GridLayout(2,1));      
      getContentPane().add(new Label("A label")) ;
      JButton testButton=new JButton("A button") ;
      testButton.setSize(new Dimension(50,50));  // NOR THIS
      testButton.setMaximumSize(new Dimension(50,50)); // OR THIS WORKS
      getContentPane().add(testButton) ;
      setVisible(true) ;
  }      
 
  public static void main(String args[]) {
      new ButtonTest().show();
  }
}
Andrew Thompson - 09 Mar 2004 08:06 GMT
> Can someone tell me, how to set Dimension of a Button to exactly size in
> GridLayout.

Quoting from the JDocs for GridLayout.
"The GridLayout class is a layout manager
that lays out a container's components in
a rectangular grid. The container is
divided into *equal*-*sized* rectangles,..."
(The asterisks are my addition)

Use a different Layout if you want the
butten a different size, possibly a
BorderLayout.

I will also point out that it is uncommon
to need to set a component's size, and usually
an indication that you are 'fighting' the
LayoutManager.  

Let the LayoutManagers handle what they
are good at.  Ensuring a consistent and
logical layout across multiple systems,
font sizes & font faces on monitors of
different resolutions.

Signature

Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology

Karsten Lentzsch - 09 Mar 2004 08:29 GMT
> Let the LayoutManagers handle what they
> are good at.  Ensuring a consistent and
> logical layout across multiple systems,
> font sizes & font faces on monitors of
> different resolutions.

If only every advice to (novice) developers
would end with this phrase.

Karsten
Andrew Thompson - 09 Mar 2004 09:02 GMT
>> Let the LayoutManagers handle what they
>> are good at.  
<snip>
> If only every advice to (novice) developers
> would end with this phrase.

I considered adding the bit about
'setPreferredSize', but felt the OP
should investigate LayoutManager's
_extensively_ before using it.   ;-)

Signature

Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology

Glodalec - 09 Mar 2004 10:50 GMT
> > Can someone tell me, how to set Dimension of a Button to exactly size in
> > GridLayout.
[quoted text clipped - 20 lines]
> font sizes & font faces on monitors of
> different resolutions.

Hi !

I had tried all of them, but each one has something the other hasn't.
What I need is actually like a FlowLayout, which adds objects
sequentially from left to right, but I need to control a line-break (
FlowLayout breaks into newline, when it wraps only). If that's is
somehow possible, that is my Layout manager :).
Andrew Thompson - 09 Mar 2004 11:19 GMT
<snip prior question, as this has now
gone much wider to Layouts in general>

> I had tried all of them, but each one has something the other hasn't.
> What I need is actually like a FlowLayout, which adds objects
> sequentially from left to right, but I need to control a line-break (
> FlowLayout breaks into newline, when it wraps only). If that's is
> somehow possible, that is my Layout manager :).

(shakes head slowly) I do not quite
follow the layout you mean.
Could you actually
a) Draw it in a paint program, or..
b) Sketch it on paper, scan it and..
upload it to a website for me to see?

Signature

Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology

Laird Nelson - 09 Mar 2004 12:41 GMT
> (shakes head slowly) I do not quite
> follow the layout you mean.
> Could you actually
> a) Draw it in a paint program, or..
> b) Sketch it on paper, scan it and..
> upload it to a website for me to see?

The OP means something like this:

+----+----------+----+
|    |          |    |
+----+---+----+-+----+
|        |    |      |
+--------+----+------+

...where each square area is a component.

Can't use a GridLayout because the boxes can't be varying sizes.  Can't
use a FlowLayout because you can't control how many components are in a row.

To the OP: this is custom made for GridBagLayout.  Or a couple of Boxes.
 Or SpringLayout.

Laird
Glodalec - 10 Mar 2004 10:40 GMT
> > (shakes head slowly) I do not quite
> > follow the layout you mean.
[quoted text clipped - 20 lines]
>
> Laird

Yep. Mostly in my programs (I used to make GUI's in Delphi), there is a
several labels (or various input area's), and on the button, there are
few buttons. Using existing Layout managers, I must use at least two,
one for top area and one for bottom (probably FlowLayout for buttons).
I have seen JFC Visual Swing examples, but are very basic. Let's say I
have such GUI:

+--------------------------------------+
|  Enter phone: __________             +
|  Enter name: ___________             +
|--------------------------------------+
| +-------+   +-------+    +--------+  |
| |  Ok   |   | Apply |    | Cancel |  |
| +-------+   +-------+    +--------+  |
+--------------------------------------+

What would be the most convenient way using Layout managers to do it ?
Karsten Lentzsch - 10 Mar 2004 11:58 GMT
> +--------------------------------------+
> |  Enter phone: __________             +
[quoted text clipped - 6 lines]
>
> What would be the most convenient way using Layout managers to do it ?

In this case I would use two layouts: one for
the labels, field, and a button bar, and a
sublayout for the buttons in the button bar.

I recommend to use a single grid if it helps
align components. In the above design you don't
align the buttons with the labels or fields.
Extracting a sublayout makes the main layout simpler.

In Forms you can write the above as:
FormLayout layout = new FormLayout("pref, 3dlu, pref");
DefaultFormBuilder builder = new DefaultFormBuilder(layout);
builder.setDefaultDialogBorder();

builder.append("Enter phone:", phoneField);
builder.append("Enter name:",  nameField);
builder.append(ButtonBarFactory.buildOKCancelApplyBar(...), 3);
return builder.getPanel();

I recommend to use "Phone:" instead of "Enter phone:".

Regards,
Karsten
Karsten Lentzsch - 09 Mar 2004 08:06 GMT
> Can someone tell me, how to set Dimension of a Button to exactly size in
> GridLayout.

You can set the button's preferred and minimum sizes.
See #setPreferredSize.

Anyway, I recommend to let a layout manager compute
and set the size. Since a button lacks information
about its usage context it can hardly report sizes
appropriate for well designed multi-platform UIs.

For example an OK and Cancel button report different
preferred width because of the text length difference.
However, the Apple Human Interface Guidelines require
to set a button width of 68 pixel for both buttons.

And even harder, the Microsoft Layout Guidelines
demand a width of 50dlu, a size unit that honors
the font, font size and screen resolution (dpi)
when it is mapped to concrete pixels. For example
50dlu map to 85 pixel for a 8pt Tahoma on 96dpi.

Since the resolutions on recent LCDs vary between
86 dpi (many LCDs) and 147 dpi (e.g. Dell's WUXGA),
pixel sizes lead to poor screen design.

I provide a layout manager and helper classes that
can layout buttons well. It addresses all issues
mentioned above and many others. It reverses the
button order when necessary (OK, Cancel on PC vs.
Cancel, OK on Mac). And you can build button bars
quickly with either the ButtonBarBuilder or the
ButtonBarFactory. The latter vends well designed
button bars for frequently used button combinations.

The FormLayout is open source and free of charge,
http://forms.dev.java.net/

Hope this helps.
Karsten Lentzsch
JGoodies :: Java User Interface Design

> Below is my example which doesn't work.
>
[quoted text clipped - 25 lines]
>    }
> }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.