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 / May 2005

Tip: Looking for answers? Try searching our database.

JButton layout - equal width with no absurd stretch

Thread view: 
wasteheap@email.com - 17 May 2005 15:43 GMT
Thank you for taking the time to enlighten me.  I have been searching
and trying different things but nothing seems to work for me.  All I
want to do is have a column of JButtons to the left of a JScrollPane.
I want the buttons to have equal widths (the widest button at its
minimum text-fitting width, all others stretched to an equal width) and
be only as high as they need to be for their text. The scroll pane can
fill the rest of the width (and height for that matter).  Something
like this:

+----------------------------------+
|                                  |
| +--------+ +-------------------+ |
| |   A    | |                   | |
| +--------+ |                   | |
| | ABCDEF | |                   | |
| +--------+ |    JScrollPane    | |
| |  ABCD  | |                   | |
| +--------+ |                   | |
|            |                   | |
|            +-------------------+ |
|                                  |
+----------------------------------+

I tried box layout, but then the buttons are not equal widths.  Border
layout makes the buttons equal widths, but stretches them to be as high
as the scroll pane.  Grid layout stretches them both vertically and
horizontally.  I think I tried gridbag layout and had the same problem
as grid layout. The type of layout I want for the buttons reminds me of
how a table would layout in an HTML document. I want to avoid using
literal size values because of L&F issues on other platforms. Shrinking
the height of the scroll pane is not an option. Does anyone have any
suggestions?
Michael Rauscher - 17 May 2005 15:52 GMT
wasteheap@email.com schrieb:
> Thank you for taking the time to enlighten me.  I have been searching
> and trying different things but nothing seems to work for me.  All I
[quoted text clipped - 4 lines]
> fill the rest of the width (and height for that matter).  Something
> like this:

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

public class Test {

    Test() {
    }

    public static final void main( String args[] ) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

        JPanel buttonPanel = new JPanel( new GridLayout(0,1) );
        buttonPanel.add( new JButton("Button1") );
        buttonPanel.add( new JButton("Button2") );
        buttonPanel.add( new JButton("Button3") );

        JPanel leftColumnPanel = new JPanel( new FlowLayout() );
        leftColumnPanel.add( buttonPanel );

        JPanel emptyPanel = new JPanel();
        emptyPanel.setPreferredSize( new Dimension(600,400) );

        JScrollPane scrollPane = new JScrollPane(emptyPanel);
        frame.getContentPane().add( scrollPane, BorderLayout.CENTER );
        frame.getContentPane().add( leftColumnPanel,BorderLayout.WEST );

        frame.pack();
        frame.setVisible(true);
    }
}

Bye
Michael
wasteheap@email.com - 18 May 2005 14:09 GMT
Yes!!! GridLayout inside FlowLayout -- I'll have to remember that one.
sarma - 17 May 2005 16:01 GMT
Hai
This could not be possible with one layout, do the following procedure
that might help you.

. set your panel layout to BorderLayout .
. create a panel with  GridLayout and add your buttons to this layout.

 add  this panel to the above BorderLayouted panel's East/West
 direction
. and in the BorderLayout.center add Scrollpane.

this can serve the basic need Your Ideas are always welcome.


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.