PDA

View Full Version : Java Layouts question


ramapple
11-04-2006, 07:32 AM
In layouts like FlowLayout, i've searched through google and many other places but can not find the answer to what I am looking for: Is it possible to make a bunch of JButtons in a straight line on the BOTTOM of the page, and then ONE button at the top? How can i get a row of buttons to appear on the bottom? For the top, I was able to succeed by making a container, making a jpanel, and a few buttons. I added the buttons in the jpanel and the container, now how do i reposition it?

ess
11-04-2006, 12:34 PM
Here is what I would do.

1- set the jframe's layout to BorderLayout
2- all the buttons you want to display at the bottom, add them to a JPanel, which has a FlowLayout defined.
3- once you have filed the jpanel, add jpanel to jframe specifying that it should be displayed in the south.

here is an example that i have created to help you out.

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

public class Example
{
private JFrame frame;

private Example()
{
this.frame = new JFrame( "Example" );
this.frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

this.setGUIs();

this.frame.setSize( 400, 200 );
this.frame.setVisible( true );
} //-- ends class constructor

private void setGUIs()
{
Container pane = this.frame.getContentPane();
pane.setLayout( new BorderLayout() );

// the following buttons will be displayed at the top
JButton t1 = new JButton( "t1" );
JButton t2 = new JButton( "t2" );
JButton t3 = new JButton( "t3" );
JButton t4 = new JButton( "t4" );

JPanel tPanel = new JPanel( new FlowLayout() );
tPanel.add( t1 );
tPanel.add( t2 );
tPanel.add( t3 );
tPanel.add( t4 );

// add tPanel north of jframe
pane.add( tPanel, BorderLayout.NORTH );

// the following buttons will be displayed at the bottom
JButton b1 = new JButton( "b1" );
JButton b2 = new JButton( "b2" );
JButton b3 = new JButton( "b3" );
JButton b4 = new JButton( "b4" );

// panel to contain all buttons
JPanel bPanel = new JPanel( new FlowLayout() );

bPanel.add( b1 );
bPanel.add( b2 );
bPanel.add( b3 );
bPanel.add( b4 );
// add bPantel south of jframe...so it is displaed
// at the bottom
pane.add( bPanel, BorderLayout.SOUTH );
} //-- ends instance method setGUIs

public static void main( String [] args )
{
SwingUtilities.invokeLater( new Runnable() {
public void run() {
new Example();
}});
} //-- ends class method main

} //-- ends class definition

Before I go...I would like to recommend NetBeans (http://www.netbeans.org/). It is one of best IDE for Java...and has one of the best GUI designers also. Check it out, it i free.

all the best
:thumbsup:

ramapple
11-04-2006, 09:00 PM
Thank you ess, after reviewing the code I think I understand the process of how to arrange everything. Also, I will give netbeans a try; I've been using JCreator. I only have one question, if I add a public void paint into my program, even with nothing in it, my buttons are hid from view; does this code help it by calling it every time? :


public static void main( String [] args )
{
SwingUtilities.invokeLater( new Runnable() {
public void run() {
new Example();
}});
}


Do I really need to input this? Because I've been working with applets most of the time and if I want to implement this into an applet I will have to change a few things.

Also, is there a way to input something between top left of the screen and the middle of the screen? thanks

ess
11-05-2006, 01:17 AM
Hi ramapple,

I am going to refer you to Java 2 SE documentation for more information.

If you look at BorderLayout (http://java.sun.com/j2se/1.4.2/docs/api/java/awt/BorderLayout.html)documentation, and read this article (http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html), you find the answers there.

Good luck.

:thumbsup:

ramapple
11-05-2006, 04:37 AM
Thank you ess, but before I saw your post I was doing some more research, and found out that panels/buttons can be refreshed from doing button.repaint();. And from looking at the BorderLayout information, it doesn't seem as if it's flexible enoguh to add a random JPanel somewhere in the middle (not the center). In all, thank you for your help.

Edit: +rep