Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

trouble with layouts

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joe,

We've got a whole forum for GUI stuff, so I'm going to move this post over there.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Joe Panully
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't add a layout. You add a component.

 
Joe Panully
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you provide the entire class so that I can test for you?
 
Joe Panully
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
 
Joe Panully
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic