Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JPanel Help 2

Status
Not open for further replies.

PCSAARON

Programmer
Jul 9, 2002
131
US
I need some help here. I am new to the Java GUI world.
I have an existing JPanel. For one of the rows I need to have a label followed by a text box, then followed by another label, and another text box. Do I need to add another jpanel within the row somehow to get several text boxes/labels in that row?

Here is what I have so far:

public void addComponents() {
this.setFrameIcon(icon);
lCompanyName = new JLabel("Company");
lPhoneNumber = new JLabel("Phone");
lFullName = new JLabel("FullName");
lAddress1 = new JLabel("Address 1");
lAddress2 = new JLabel("Address 2");
lCity = new JLabel("City");
lState = new JLabel("State");
lZip = new JLabel("Zip");


JPanel labelPane = new JPanel();
labelPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
labelPane.setLayout(new GridLayout(0, 1, 20, 10));
labelPane.add(lCompany);
labelPane.add(lPhone);
labelPane.add(lFullName);

JPanel labelPane2 = new JPanel();
labelPane2.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
labelPane2.setLayout(new GridLayout(0, 1, 20, 10));
labelPane2.add(lAddress1);
labelPane2.add(lAddress2);
labelPane2.add(lCity);
labelPane2.add(lState);
labelPane2.add(lZip);
}

What I really want is to have the city state and zip on the same line. Can someone offer some advice? Thanks in advance.
 
Code:
import javax.swing.*;
import java.awt.*;
   class jp extends JFrame
  {
   JLabel lCompanyName,lPhoneNumber,lFullName,lAddress1,lAddress2,lCity,lState,lZip,dummy;
   JPanel labelPane,labelPane2,zipStatePane;
   JTextField jtState;
   public jp() {
        //this.setFrameIcon(icon);
        lCompanyName = new JLabel("Company");
        lPhoneNumber = new JLabel("Phone");
        lFullName = new JLabel("FullName");
        lAddress1 = new JLabel("Address 1");
        lAddress2 = new JLabel("Address 2");
        lCity = new JLabel("City");
        lState = new JLabel("State");
        jtState = new JTextField();
        lZip = new JLabel("Zip");
        dummy = new JLabel();
 
        labelPane = new JPanel();
        labelPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        labelPane.setLayout(new GridLayout(4, 1, 20, 10));
        labelPane.add(lCompanyName);
        labelPane.add(lPhoneNumber);
        labelPane.add(lFullName);
        labelPane.add(dummy);
 
        labelPane2 = new JPanel();
        zipStatePane =  new JPanel();
        zipStatePane.setLayout(new GridLayout(1,3));

        labelPane2.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        labelPane2.setLayout(new GridLayout(4, 1, 20, 10));

        labelPane2.add(lAddress1);
        labelPane2.add(lAddress2);
        labelPane2.add(lZip);
        labelPane2.add(zipStatePane);        

        zipStatePane.add(lCity);
        zipStatePane.add(lState);
        zipStatePane.add(jtState);

        getContentPane().setLayout(new GridLayout(2,1));
        getContentPane().add(labelPane);
        getContentPane().add(labelPane2);
   }
   public static void main(String args[])
          {
           jp jpObj = new jp();
           jpObj.setSize(400,400);
           jpObj.setVisible(true);
          }
  }
 
Thanks for your help. It is a very simple concept, your example was a big help. Can you explain these:

GridLayout(1,3));

createEmptyBorder(10, 10, 10, 10));
GridLayout(4, 1, 20, 10));

I tried looking up the GridLayout and I must have overlooked something. Thanks.
 
GridLayout(1,3) //y,x one row and 3 columns
createEmptyBorder(10, 10, 10, 10) // set the width of the border of top border, left border right border and bottom border
GridLayout(4, 1, 20, 10) create a layout with 4 rows and 1 column, there is 20 pixel space between two columns in width and there is 10 pixel space between two rows in height
 
Thanks for all your input on this. I was able to accomplish the task at hand. I do have one more question, is there a way to actually set the columns width individually? The way I see this is it splits the grid evenly. Thanks.
 
No. With GridLayout all the cells share the same dimensions. To have uneven columns you'll have to either resort to using separate JPanels with a GridLayout on each, or take the plunge and use a GridBagLayout (which is not for the faint-hearted).

Does anyone know of any alternatives?

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Thanks for all your help with the GridLayout. I seem to have a slight problem with a combobox on the grid. There is a checkbox I added right below the combobox and when you click on the combobox, the row that the checkbox is in shows up on top of the combobox's list. Kind of like it painted the background on top so you can't see the row of the combobox. Is there some sort of ordering I am not taking into consideration?

Thanks.
 
have a look at gui genie. its a free gui creator. you can get it from here

its a nice easy tool to use. it'll show you a few more of the various layouts you can use and allow you to mess about with the gui a lot easier. you have to create the gui and then paste in the code it generates into your own application.

hope thats a help
 
Thanks dexter195...

That is a very nice tool... I wish I had that before I started the project! :) Anyway, I did find the problem using that GUIGenie to make an example just like the one I have. It seems that I was using a CheckBox instead of a JCheckBox. When I switched the coding around to use the JCheckBox, it worked fine.

Thanks again...
 
i found it was a great program to get the gui's done nice and fast and without any mucking around.

glad it helped you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top