wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes trouble with layouts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "trouble with layouts" Watch "trouble with layouts" New topic
Author

trouble with layouts

Joe Panully
Greenhorn

Joined: Oct 10, 2004
Posts: 22
I'm having trouble with layouts. I've only ever messed around with flow and border layouts, but neither one are coming through for me on what i'm working on now. Basically, I want to have a button taking up the entire top of my gui, then a text label and a text field on the next row below that button to the left of it, and a text label than text field on that same row but to the left, then I want to have a label in the center of the next row and a text area under that.


let me try to draw this in symbols



Arigato.

[ November 19, 2004: Message edited by: Joe Panully ]
[ November 19, 2004: Message edited by: Joe Panully ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi Joe,

We've got a whole forum for GUI stuff, so I'm going to move this post over there.


[Jess in Action][AskingGoodQuestions]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

There are a few ways you could go about this. You could use a GridBagLayout for a single JPanel and place all your components with it. However, GridBagLayout can be quite complicated although it is the most robust and flexable.

Another option is to split your frame into several different Panels. So for example...



Green - This is the main panel which will have a border layout. You would place a JButton on BorderLayout.NORTH.

Red - This is another panel. This is placed on the green panel at BorderLayout.CENTER. Give this a BorderLayout as well.

Yellow - Put this panel on Red at BorderLayout.NORTH and give it a flow layout or a GridLayout of 4 columns and 1 row.

Pink - this goes on the red panel at BorderLayout.CENTER and give it a layout of borderLayout. Put label3 at BorderLayout.NORTH and the textfield at BorderLayout.CENTER.

This is no means the only way, but it should give you a good starting place.


GenRocket - Experts at Building Test Data
Joe Panully
Greenhorn

Joined: Oct 10, 2004
Posts: 22
How do you add one layout to another? I tried a couple of different ways and I can't get it to work.

I get an error that I am adding containers parent to themselves.


[ November 19, 2004: Message edited by: Joe Panully ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

You don't add a layout. You add a component.

Joe Panully
Greenhorn

Joined: Oct 10, 2004
Posts: 22
Sorry about all these questions but Im a complete java/especially GUI noob. I get this to compile now but it just shows a blank screen.


[ November 19, 2004: Message edited by: Joe Panully ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Could you provide the entire class so that I can test for you?
Joe Panully
Greenhorn

Joined: Oct 10, 2004
Posts: 22
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I am asuming p1 is your top level container that contains everything else on the JFrame? You never add it to the JFrame's contentpane.

setContentPane(p1); before you setVislble()
Pat Hays
Ranch Hand

Joined: Aug 20, 2004
Posts: 138
Hello Joe,

Try the following code. it will draw the window you want.
------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LayoutWindow extends JFrame
{
private JPanel contentPane;

private JButton jButton_giant;
private JPanel jPanel_top;

private JLabel jLabel1;
private JTextField jTextField1;
private JPanel jPanel1;

private JLabel jLabel2;
private JTextField jTextField2;
private JPanel jPanel2;

private JLabel jLabel3;
private JLabel jLabel_leftGap;
private JLabel jLabel_rightGap;
private JLabel jLabel_bottomGap;
private JTextArea jTextArea1;
private JScrollPane jScrollPane1;
private JPanel jPanel_center;

public LayoutWindow()
{
super();
initializeComponent();
this.setVisible(true);
}

private void initializeComponent()
{
contentPane = (JPanel)this.getContentPane();

jButton_giant = new JButton();
jPanel_top = new JPanel();

jLabel1 = new JLabel();
jTextField1 = new JTextField();
jPanel1 = new JPanel();

jLabel2 = new JLabel();
jTextField2 = new JTextField();
jPanel2 = new JPanel();

jLabel3 = new JLabel();
jLabel_leftGap = new JLabel();
jLabel_rightGap = new JLabel();
jLabel_bottomGap = new JLabel();
jTextArea1 = new JTextArea();
jScrollPane1 = new JScrollPane();
jPanel_center = new JPanel();

contentPane.setLayout(new BorderLayout(0, 5));
contentPane.add(jPanel_top, BorderLayout.NORTH);
contentPane.add(jPanel_center, BorderLayout.CENTER);

jButton_giant.setText("GIANT BUTTON");
jButton_giant.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton_giant_actionPerformed(e);
}

});

jPanel_top.setLayout(new BorderLayout(0, 0));
jPanel_top.add(jPanel1, BorderLayout.WEST);
jPanel_top.add(jPanel2, BorderLayout.EAST);
jPanel_top.add(jButton_giant, BorderLayout.NORTH);

jLabel1.setText("jLabel1");

jTextField1.setPreferredSize(new Dimension(100, 24));
jTextField1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jTextField1_actionPerformed(e);
}

});

jPanel1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
jPanel1.add(jLabel1, 0);
jPanel1.add(jTextField1, 1);

jLabel2.setText("jLabel2");

jTextField2.setPreferredSize(new Dimension(100, 24));
jTextField2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jTextField2_actionPerformed(e);
}

});

jPanel2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
jPanel2.add(jLabel2, 0);
jPanel2.add(jTextField2, 1);

jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
jLabel3.setText("jLabel3");

jLabel_leftGap.setText(" ");

jLabel_rightGap.setText(" ");

jLabel_bottomGap.setText(" ");

jTextArea1.setText("jTextArea1");

jScrollPane1.setViewportView(jTextArea1);

jPanel_center.setLayout(new BorderLayout(0, 5));
jPanel_center.add(jLabel3, BorderLayout.NORTH);
jPanel_center.add(jScrollPane1, BorderLayout.CENTER);
jPanel_center.add(jLabel_leftGap, BorderLayout.WEST);
jPanel_center.add(jLabel_rightGap, BorderLayout.EAST);
jPanel_center.add(jLabel_bottomGap, BorderLayout.SOUTH);

this.setTitle("LayoutWindow - extends JFrame");
this.setLocation(new Point(96, 56));
this.setSize(new Dimension(437, 342));
}


private void jButton_giant_actionPerformed(ActionEvent e)
{
System.out.println("jButton_giant_actionPerformed(ActionEvent e) called.");
}

private void jTextField1_actionPerformed(ActionEvent e)
{
System.out.println("jTextField1_actionPerformed(ActionEvent e) called.");
}

private void jTextField2_actionPerformed(ActionEvent e)
{
System.out.println("jTextField2_actionPerformed(ActionEvent e) called.");
}


public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception ex)
{
System.out.println("Failed loading L&F: ");
System.out.println(ex);
}
new LayoutWindow();
}
}


Download Java GUI Builder, <a href="http://www.mars3000.com" target="_blank" rel="nofollow">http://www.mars3000.com</a>
Joe Panully
Greenhorn

Joined: Oct 10, 2004
Posts: 22
thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trouble with layouts
 
Similar Threads
Customizing JTree leaf nodes
nesting trys and catches
How do you make a form?
Another error in the book?
much trouble reading files