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

Tip: Looking for answers? Try searching our database.

Adding JButtons on JPanel vertically

Thread view: 
Camford - 03 Sep 2005 00:29 GMT
Hi,

I'm trying to add buttons to a JPanel vertically, without using a layout
manager explictly.

button_panel.setBorder(new LineBorder(Color.BLACK));
JButton add_class_button = new JButton("Add class");
JButton add_arrow_button = new JButton("Add arrow");
button_panel.add(add_class_button);
button_panel.add(Box.createVerticalStrut(10));
button_panel.add(add_arrow_button);

I thought adding a vertical strut would make it add the buttons vertically,
but it didn't work. So I'm wondering is there a way of aligning the buttons
vertically with something simple like a setAlignment statement.
Thomas A. Russ - 03 Sep 2005 02:20 GMT
> Hi,
>
> I'm trying to add buttons to a JPanel vertically, without using a layout
> manager explictly.

Whynot use a layout manager?  That's what they're there for.

It would seem that BoxLayout set to vertical should do exactly what you
want.  Use BoxLayout.Y_AXIS to get the vertical layout.

Signature

Thomas A. Russ,  USC/Information Sciences Institute

Camford - 03 Sep 2005 03:14 GMT
I did, but it didn't leave any spacing around the edge of the button. I then
tried the following with the BoxLayout:

 button_panel.add(Box.createHorizontalStrut(10));
 button_panel.add(add_class_button);
 button_panel.add(Box.createHorizontalStrut(10));
 button_panel.add(Box.createVerticalStrut(10));
 button_panel.add(Box.createHorizontalStrut(10));
 button_panel.add(add_arrow_button);
 button_panel.add(Box.createHorizontalStrut(10));
 button_panel.add(Box.createVerticalGlue());

It creates a gap too big for my liking vertically between the two buttons.
I've no idea what I have done wrong.

When there is one button on the JPanel, and without stating which layout to
use, it shows up quite nicely with right sort of spacing around the edges of
the button. But if I add another button, it would not add it vertically. I'm
guessing there is a way of adding buttons in the vertical direction, but I
just don't know what it is.
Michael Dunn - 03 Sep 2005 05:49 GMT
>I did, but it didn't leave any spacing around the edge of the button. I
>then tried the following with the BoxLayout:
[quoted text clipped - 16 lines]
> vertically. I'm guessing there is a way of adding buttons in the vertical
> direction, but I just don't know what it is.

look into GridLayout
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/GridLayout.html

particularly the constructor which includes hgap, vgap

here's a simple demo (4 buttons aligned vertically) with no gaps

import java.awt.*;
import javax.swing.*;
class Testing extends JFrame
{
 final int ROWS = 4,COLS = 0;
 public Testing()
 {
   setLocation(400,300);
   setDefaultCloseOperation(EXIT_ON_CLOSE);
   JPanel panel = new JPanel(new GridLayout(ROWS,COLS));
   for(int x = 0; x < ROWS; x++) panel.add(new JButton("Button "+x));
   getContentPane().add(panel);
   pack();
 }
 public static void main(String[] args){new Testing().setVisible(true);}
}

same program, with spacing of 10, vertically

import java.awt.*;
import javax.swing.*;
class Testing extends JFrame
{
 final int ROWS = 4,COLS = 0;
 public Testing()
 {
   setLocation(400,300);
   setDefaultCloseOperation(EXIT_ON_CLOSE);
   JPanel panel = new JPanel(new GridLayout(ROWS,COLS,0,10));//<---------10
spacing vertically
   for(int x = 0; x < ROWS; x++) panel.add(new JButton("Button "+x));
   getContentPane().add(panel);
   pack();
 }
 public static void main(String[] args){new Testing().setVisible(true);}
}
Camford - 07 Sep 2005 00:30 GMT
Is there anyway to stop the buttons from resizing if you resize the main
window?
Michael Dunn - 07 Sep 2005 00:40 GMT
> Is there anyway to stop the buttons from resizing if you resize the main
> window?

import java.awt.*;
import javax.swing.*;
class Testing extends JFrame
{
 final int ROWS = 4,COLS = 0;
 public Testing()
 {
   setLocation(400,300);
   setDefaultCloseOperation(EXIT_ON_CLOSE);
   JPanel holdingPanel = new JPanel(new GridBagLayout());
   JPanel panel = new JPanel(new GridLayout(ROWS,COLS));
   for(int x = 0; x < ROWS; x++) panel.add(new JButton("Button "+x));
   holdingPanel.add(panel,new GridBagConstraints());
   getContentPane().add(holdingPanel);
   pack();
 }
 public static void main(String[] args){new
Testing().setVisible(true);}
}


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.