DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2004
    Posts
    3

    Using two layout managers in two panels but they are combining

    As I said before, the intent is to create an applett with 2 layout manager on two panels. the first panel is above the second panel. The first using the gridbaglayout manager. The second I will be using the borderlayout manager.
    Unfortunatly my panels seem to be over each other. the first panel seems to be left of the second panel

    Another thing . In my first panel i am supposed to have three lines with several objects on each line. I/e lables and txt boxes. but they are too far spread apart. Any help would be much appreciated. Here is my code.

    Sorry the code is not entirely complete. I


    package Week3;


    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;

    public class LayoutAssignment1 extends java.applet.Applet implements ActionListener, MouseListener
    {
    //Declare components

    Panel pnlInput = new Panel();
    Panel pnlOutput = new Panel();
    TextField txtName = new TextField(25);
    TextField txtLast = new TextField(25);
    TextField txtAdd = new TextField(30);
    TextField txtCity = new TextField(15);
    TextField txtState = new TextField(2);
    TextField txtZip = new TextField(10);
    //Button
    Button btnAdd = new Button("Display Address");

    //Text Area
    TextArea txaAdd = new TextArea(10, 30);

    Label fName = null;
    Label lName = null;
    Label address = null;
    Label city = null;
    Label state = null;
    Label zip = null;



    //Declare variables

    String strName;
    String strLast;
    String strAdd;
    String strCity;
    String strState;
    String strZip;


    //add components





    public void init() {




    fName = new Label("First Name");
    lName = new Label("Last Name");
    address = new Label("Address");
    city = new Label("City");
    state = new Label("State");
    zip = new Label("Zip");



    //Fonts and Background colors

    setForeground(Color.blue);
    Font g = new Font("Serif", Font.BOLD, 14);
    setFont(g);
    Font fntName = new Font("Serif", Font.BOLD, 14);
    setBackground(Color.lightGray);
    txtName.setFont(fntName);
    txtAdd.setFont(fntName);
    txtLast.setFont(fntName);
    txtCity.setFont(fntName);
    txtState.setFont(fntName);
    txtZip.setFont(fntName);
    btnAdd.setBackground(Color.yellow);
    btnAdd.setForeground(Color.red);
    txtName.requestFocus();

    //Create user interface
    DesignInputPanel();
    DesignOutputPanel();
    add(pnlInput);
    add(pnlOutput);
    txtName.requestFocus();
    btnAdd.addActionListener(this);
    btnAdd.addMouseListener(this);



    }

    public void actionPerformed(ActionEvent e) {

    String strOutputLine; //Declare local variable

    //Assign text fields
    strName = txtName.getText();
    strLast = txtLast.getText();
    strAdd = txtAdd.getText();
    strCity = txtCity.getText();
    strState = txtState.getText();
    strZip = txtZip.getText();

    //Move variables down
    strOutputLine = strName + ("\t") + strLast + ("\n") + strAdd + ("\n") + strCity + (" ") + strState + (", ") + strZip ;
    //Append
    txaAdd.append(strOutputLine);


    //Clear Text Fields
    txtName.setText("");
    txtLast.setText("");
    txtAdd.setText("");
    txtCity.setText("");
    txtState.setText("");
    txtZip.setText("");

    //set focus back to Firstname

    txtName.requestFocus();



    }
    public void DesignInputPanel()
    {

    //Layout manager

    GridBagLayout pnlInput = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    setLayout(pnlInput);
    constraints.insets = new Insets(5, 5,10, 5);



    //First Name
    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.weightx=1.0;
    constraints.anchor = GridBagConstraints.EAST;
    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(fName, constraints);
    add(fName);


    constraints.gridx = 1;
    constraints.gridy = 0;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(txtName, constraints);
    add(txtName);

    //Last Name
    constraints.gridx = 2;
    constraints.gridy = 0;
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.anchor = GridBagConstraints.EAST;
    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(lName, constraints);
    add(lName);

    constraints.gridx = 3;
    constraints.gridy = 0;
    constraints.weightx=3;
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(txtLast, constraints);
    add(txtLast);

    //Address
    constraints.gridx = 0;
    constraints.gridy = 1;
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.anchor = GridBagConstraints.EAST;
    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(address, constraints);
    add(address);



    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(txtAdd, constraints);
    add(txtAdd);


    //City
    constraints.gridx = 0;
    constraints.gridy = 2;
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.anchor = GridBagConstraints.EAST;
    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(city, constraints);
    add(city);



    constraints.gridx = 1;
    constraints.gridy = 2;
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(txtCity, constraints);
    add(txtCity);


    //State
    constraints.gridx = 2;
    constraints.gridy = 2;
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.anchor = GridBagConstraints.EAST;
    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(state, constraints);
    add(state);


    constraints.gridx = 3;
    constraints.gridy = 2;

    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(txtState, constraints);

    add(txtState);




    //zipcode
    constraints.gridx = 4;
    constraints.gridy = 2;
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.anchor = GridBagConstraints.EAST;
    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(zip, constraints);
    add(zip);


    constraints.gridx = 5;
    constraints.gridy = 2;
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(txtZip, constraints);
    add(txtZip);





    }


    public void DesignOutputPanel()
    {
    pnlOutput.setLayout(new BorderLayout(0,1));

    pnlOutput.add("South", txaAdd);




    }

    public void mouseClicked(MouseEvent e) {


    }

    public void mouseEntered(MouseEvent e) {
    // Changing of Button colors
    fName.setForeground(Color.RED);
    lName.setForeground(Color.RED);
    address.setForeground(Color.RED);
    city.setForeground(Color.RED);
    state.setForeground(Color.RED);
    zip.setForeground(Color.RED);
    btnAdd.setForeground(Color.magenta);
    btnAdd.setBackground(Color.green);

    //Status bar text
    showStatus("Format Address into Text Area");
    }

    public void mouseExited(MouseEvent e) {

    //Colors Back

    fName.setForeground(Color.blue);
    lName.setForeground(Color.blue);
    address.setForeground(Color.blue);
    city.setForeground(Color.blue);
    state.setForeground(Color.blue);
    zip.setForeground(Color.blue);
    btnAdd.setForeground(Color.red);
    btnAdd.setBackground(Color.yellow);



    //Clear Status bar
    showStatus("");

    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }}

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    I think you need to specify where each component is added.
    In a border layout, this would add the menuBar to top of the container.

    add(menuBar, BorderLayout.NORTH);

  3. #3
    Join Date
    Sep 2004
    Posts
    3
    Actually i was able to fix the problem of the top panel impeding on the bottom one.. this is what i have so far.

    http://pandimus.freeservers.com/snapshot.JPG

    this is what it si suppsed to look like
    http://pandimus.freeservers.com/Week3.jpg

    My code thus far..


    package Week3;


    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;

    public class LayoutAssignment1 extends java.applet.Applet implements ActionListener, MouseListener
    {
    //Declare components

    Panel pnlInput = new Panel();
    Panel pnlOutput = new Panel();
    TextField txtName = new TextField(25);
    TextField txtLast = new TextField(25);
    TextField txtAdd = new TextField(30);
    TextField txtCity = new TextField(15);
    TextField txtState = new TextField(2);
    TextField txtZip = new TextField(10);
    //Button
    Button btnAdd = new Button("Display Address");

    //Text Area
    TextArea txaAdd = new TextArea(10, 30);

    Label fName = null;
    Label lName = null;
    Label address = null;
    Label city = null;
    Label state = null;
    Label zip = null;



    //Declare variables

    String strName;
    String strLast;
    String strAdd;
    String strCity;
    String strState;
    String strZip;


    //add components





    public void init() {




    fName = new Label("First Name");
    lName = new Label("Last Name");
    address = new Label("Address");
    city = new Label("City");
    state = new Label("State");
    zip = new Label("Zip");



    //Fonts and Background colors

    setForeground(Color.blue);
    Font g = new Font("Serif", Font.BOLD, 14);
    setFont(g);
    Font fntName = new Font("Serif", Font.BOLD, 14);
    setBackground(Color.lightGray);
    txtName.setFont(fntName);
    txtAdd.setFont(fntName);
    txtLast.setFont(fntName);
    txtCity.setFont(fntName);
    txtState.setFont(fntName);
    txtZip.setFont(fntName);
    btnAdd.setBackground(Color.yellow);
    btnAdd.setForeground(Color.red);
    txtName.requestFocus();

    //Create user interface
    DesignInputPanel();
    DesignOutputPanel();
    add(pnlInput);
    add(pnlOutput);
    txtName.requestFocus();
    btnAdd.addActionListener(this);
    btnAdd.addMouseListener(this);



    }

    public void actionPerformed(ActionEvent e) {

    String strOutputLine; //Declare local variable

    //Assign text fields
    strName = txtName.getText();
    strLast = txtLast.getText();
    strAdd = txtAdd.getText();
    strCity = txtCity.getText();
    strState = txtState.getText();
    strZip = txtZip.getText();

    //Move variables down
    strOutputLine = strName + ("\t") + strLast + ("\n") + strAdd + ("\n") + strCity + (" ") + strState + (", ") + strZip ;
    //Append
    txaAdd.append(strOutputLine);


    //Clear Text Fields
    txtName.setText("");
    txtLast.setText("");
    txtAdd.setText("");
    txtCity.setText("");
    txtState.setText("");
    txtZip.setText("");

    //set focus back to Firstname

    txtName.requestFocus();



    }
    public void DesignInputPanel()
    {

    //Layout manager

    GridBagLayout pnlInput = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    setLayout(pnlInput);
    constraints.insets = new Insets(15,15,15,15);




    //First Name
    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.weightx=1.0;

    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(fName, constraints);
    add(fName);


    //First Text
    constraints.gridx = 1;
    constraints.gridy = 0;
    constraints.gridwidth = 30;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(txtName, constraints);
    add(txtName);


    //Last Name
    constraints.gridx = 2;
    constraints.gridy = 0;
    constraints.gridwidth = 10;

    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(lName, constraints);
    add(lName);

    //Last Text
    constraints.gridx = 3;
    constraints.gridy = 0;
    constraints.gridwidth = 30;

    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(txtLast, constraints);
    add(txtLast);

    //Address
    constraints.gridx = 0;
    constraints.gridy = 1;
    constraints.gridwidth = 30;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(address, constraints);
    add(address);


    //Address Text
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = GridBagConstraints.REMAINDER;

    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(txtAdd, constraints);
    add(txtAdd);


    //City
    constraints.gridx = 0;
    constraints.gridy = 2;
    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(city, constraints);
    add(city);


    //City Text
    constraints.gridx = 1;
    constraints.gridy = 2;
    constraints.gridwidth = 10;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(txtCity, constraints);
    add(txtCity);


    //State
    constraints.gridx = 2;
    constraints.gridy = 2;
    constraints.gridwidth = 6;
    constraints.anchor = GridBagConstraints.EAST;
    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(state, constraints);
    add(state);

    //State Text
    constraints.gridx = 3;
    constraints.gridy = 2;
    constraints.gridwidth = 2;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(txtState, constraints);
    add(txtState);




    //zipcode
    constraints.gridx = 4;
    constraints.gridy = 2;


    constraints.anchor = GridBagConstraints.EAST;
    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(zip, constraints);
    add(zip);

    //Zip Text
    constraints.gridx = 5;
    constraints.gridy = 2;
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.fill = GridBagConstraints.NONE;
    pnlInput.setConstraints(txtZip, constraints);
    add(txtZip);





    }


    public void DesignOutputPanel()
    {
    pnlOutput.setLayout(new BorderLayout(0,1));

    pnlOutput.add("Center", txaAdd);
    pnlOutput.add("North", btnAdd);



    }

    public void mouseClicked(MouseEvent e) {


    }

    public void mouseEntered(MouseEvent e) {
    // Changing of Button colors
    fName.setForeground(Color.RED);
    lName.setForeground(Color.RED);
    address.setForeground(Color.RED);
    city.setForeground(Color.RED);
    state.setForeground(Color.RED);
    zip.setForeground(Color.RED);
    btnAdd.setForeground(Color.magenta);
    btnAdd.setBackground(Color.green);

    //Status bar text
    showStatus("Format Address into Text Area");
    }

    public void mouseExited(MouseEvent e) {

    //Colors Back

    fName.setForeground(Color.blue);
    lName.setForeground(Color.blue);
    address.setForeground(Color.blue);
    city.setForeground(Color.blue);
    state.setForeground(Color.blue);
    zip.setForeground(Color.blue);
    btnAdd.setForeground(Color.red);
    btnAdd.setBackground(Color.yellow);



    //Clear Status bar
    showStatus("");

    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }}

  4. #4
    Join Date
    Sep 2004
    Posts
    18
    could you make a main panel, give that something like a box layout and then add the other two panels into that...?

  5. #5
    Join Date
    Sep 2004
    Posts
    3
    Actually thats what i had to do..Thanks..

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links