codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  line breaks in GUI  gosi at 15:48 on Saturday, November 12, 2005
 

hi, it´s me again, i´m having problems with the design in GUI, no matter what I try I can´t put a break between JLabel and TextField, so it will look like this:

Name:
[textField]
Address:
[textField]

here's my code:

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


public class NameTest {

public static void main(String[]args)
{
String title = (args.length == 0 ? "Name" : args[0]);
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLayout(new BorderLayout());
Panel southPanel = new Panel(new FlowLayout(FlowLayout.RIGHT));
southPanel.setBackground(Color.lightGray);
southPanel.add(new Button("Cancel"));
southPanel.add(new Button("Submit"));
frame.add(southPanel, BorderLayout.SOUTH);

JList outputList = new JList();
Dimension outputBoxSize = new Dimension(250,50);
Panel westPanel = new Panel(new GridLayout(1,0));
westPanel.add(outputList);
westPanel.setPreferredSize(outputBoxSize);
frame.add(westPanel, BorderLayout.WEST);


Panel centerPanel = new Panel (new FlowLayout(FlowLayout.LEFT));

JLabel Name = new JLabel ("Name:");
centerPanel.add(Name);
frame.add(centerPanel, BorderLayout.CENTER);

TextField nameT = new TextField(30);
centerPanel.add(nameT);
frame.add(centerPanel, BorderLayout.CENTER);


JLabel address = new JLabel ("Address:");
centerPanel.add(address);
frame.add(centerPanel,BorderLayout.CENTER);

TextField addressT = new TextField(30);
centerPanel.add(addressT);
frame.add(centerPanel, BorderLayout.CENTER);

frame.setSize(700, 300);
frame.setVisible(true);

}
}



  Re: line breaks in GUI  crwood at 19:08 on Saturday, November 12, 2005
 

You can build up some nested panels. It takes some experimenting to get the look you want. A JPanel with GridLayout(0,1) could contain four rows of JPanels with your FlowLayout.LEFT layout manager and one component. The centerPanel will expand to fill the center section of the JFrames BorderLayout (which is the default layout manager for JFrame). So you would have to use an intermediate JPanel ('panel') to preserve the sizes of your GridLayout components. FlowLayout allows its components to be shown at their preferred size. GridLayout ignores the preferred sizes of its components.

GridBagLayout is more advanced and gives more options for placement. The tutorial has information and examples on this subject starting with Lesson: Laying Out Components Within a Container.

It is generally recommended that we do not mix AWT and Swing components. For more on this see Mixing heavy and light components.
Here's two possibilities: one with nested panels and one with GridBagLayout:

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

public class NT {

private JPanel getGBPanel()
{
JPanel panel = new JPanel (new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2,5,1,1); // spacer
gbc.weightx = 1.0; // allow horiz dispersion
gbc.anchor = GridBagConstraints.WEST; // align left (requires ^)
gbc.gridwidth = GridBagConstraints.REMAINDER; // one component per row

JLabel Name = new JLabel ("Name:");
panel.add(Name, gbc);

JTextField nameT = new JTextField(30);
panel.add(nameT, gbc);

JLabel address = new JLabel ("Address:");
panel.add(address, gbc);

JTextField addressT = new JTextField(30);
gbc.weighty = 1.0; // optional to push everything up
gbc.anchor = gbc.NORTHWEST; // or they will stay in the center
panel.add(addressT, gbc);
return panel;
}

private JPanel getNestedPanel()
{
JPanel grid = new JPanel(new GridLayout(0,1)); // single column
grid.setBackground(Color.cyan);
JLabel Name = new JLabel ("Name:");
JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
p.add(Name);
grid.add(p);

JTextField nameT = new JTextField(30);
p = new JPanel(new FlowLayout(FlowLayout.LEFT)); // reuse reference
p.add(nameT);
grid.add(p);

JLabel address = new JLabel ("Address:");
p = new JPanel(new FlowLayout(FlowLayout.LEFT));
p.add(address);
grid.add(p);

JTextField addressT = new JTextField(30);
p = new JPanel(new FlowLayout(FlowLayout.LEFT));
p.add(addressT);
grid.add(p);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel.setBackground(Color.pink);
panel.add(grid);
return panel;
}

public static void main(String[]args)
{
NT nameTest = new NT();
String title = (args.length == 0 ? "Name" : args[0]);
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLayout(new BorderLayout());
Panel southPanel = new Panel(new FlowLayout(FlowLayout.RIGHT));
southPanel.setBackground(Color.lightGray);
southPanel.add(new Button("Cancel"));
southPanel.add(new Button("Submit"));
frame.add(southPanel, BorderLayout.SOUTH);

JList outputList = new JList();
Dimension outputBoxSize = new Dimension(250,50);
Panel westPanel = new Panel(new GridLayout(1,0));
westPanel.add(outputList);
westPanel.setPreferredSize(outputBoxSize);
frame.add(westPanel, BorderLayout.WEST);

frame.add(nameTest.getGBPanel(), BorderLayout.CENTER);
// frame.add(nameTest.getNestedPanel(), BorderLayout.CENTER);

frame.setSize(700, 300);
frame.setVisible(true);
}
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: Help with a simple program
•  Re: need help with quiz
•  Why Use Method?
•  Re: Help with filesystem object & displaying in a table
•  Re: Genetic Algorithm Help
•  Re: How to make an investment calculator
•  Re: line breaks in GUI
•  Re: Graph in Gui...
•  Graph in Gui...


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005