File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Creating JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Creating JPanel" Watch "Creating JPanel" New topic

Creating JPanel

Kwesi Prat

Joined: Dec 02, 2003
Posts: 7
I wrote this program, and I want the first 5 textfilds to be verically aligned on the left, the ok and cancel buttons to be horizontally alligned in the center, and the last 5 text fields to be vetically aligned on the right. Could anyone help me with the code to do that.
Thanks in advace.
import javax.swing.*;
import java.awt.event.*;
import java.awt.Container.*;
public class TextDemo extends JFrame implements ActionListener {
private JButton button, button1;
private JTextField field1, field2, field3, field4, field5;
private JTextField field6, field7, field8, field9, field10;
// Main Method
public static void main (String args[]) {
TextDemo frame = new TextDemo();
frame.setVisible (true);
public TextDemo() {
// Program is closed when (x) button is pressed
setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE);

// Set a size
setSize (200, 200);

// Left Panel
JPanel panel = new JPanel();
field1 = new JTextField (10);
field2 = new JTextField (10);
field3= new JTextField (10);
field4 = new JTextField (10);
field5 = new JTextField (10);
panel.add (field1);
panel.add (field2);
panel.add (field3);
panel.add (field4);
panel.add (field5);

//Center panel
JPanel panel2 = new JPanel();
JButton button = new JButton ("OK");
panel.add (button);
JButton button1 = new JButton("cancel");
panel.add (button1);

// Right Panel
JPanel panel3 = new JPanel();
field6 = new JTextField (10);
field7 = new JTextField (10);
field8 = new JTextField (10);
field9 = new JTextField (10);
field10 = new JTextField (10);
panel.add (field6);
panel.add (field7);
panel.add (field8);
panel.add (field9);
panel.add (field10);
getContentPane().add (panel);

public void actionPerformed (ActionEvent event) {

Joe Ess

Joined: Oct 29, 2001
Posts: 9189

I think I already did in this thread. The Layout part of the Swing Tutorial covers alignment.

[How To Ask Questions On JavaRanch]
I agree. Here's the link:
subject: Creating JPanel
It's not a secret anymore!