Solved

arranging labels and textfields inside of border by using GBL layout

Posted on 2004-04-01
4
345 Views
Last Modified: 2006-11-17
hi!

I need put some JLabels and JTextFields inside of border the following manner.

   label_1:  textField
   label_2:  testField
   ...
   ...

Somehow, mine just arrange all horizontally.
I try to change around,  but didn't help.
How do I correct that?
**********************************
  public void createSinDataPanel() {
    sinDataPanel = new JPanel();
    GridBagLayout layout = new GridBagLayout();
    sinDataPanel.setLayout(layout);

    sinCheckBox = new JCheckBox("Sine plot");
    constraints = new GridBagConstraints();

    arrageComponent(sinCheckBox, 0, 0, 1, 1);
    constraints.insets = new java.awt.Insets(2, 2, 2, 2);
    constraints.anchor = constraints.WEST;
    sinDataPanel.add(sinCheckBox, constraints);
//----------------------
    JPanel inputPanel = new JPanel();
    inputPanel.setBorder( BorderFactory.createTitledBorder("Sine parameters") );
    arrageComponent(inputPanel, 0, 1, 1, 1);
    sinDataPanel.add(inputPanel, constraints);

    JLabel ampLabel = new JLabel("Amplitude: ");
    JLabel periodLabel = new JLabel("Period: ");
    JLabel minXLabel = new JLabel("Min X: ");
    JLabel maxXLabel = new JLabel("Max X: ");
    JLabel cgcLabel = new JLabel("Current Graph Color: ");

    ampTField = new JTextField("1.0", 5);
    periodTField = new JTextField("1.0", 5);
    minXSinTField = new JTextField("0.0", 5);
    maxXSinTField = new JTextField("1.0", 5);
                                                                           //--------------------labels and textfields inside of border
    arrageComponent(ampLabel, 0, 1, 1, 1);
    constraints.anchor = GridBagConstraints.WEST;
    inputPanel.add(ampLabel, constraints);

    arrageComponent(ampTField, 1, 1, 1, 1);
    constraints.anchor = GridBagConstraints.EAST;
    inputPanel.add(ampTField, constraints);

    arrageComponent(periodLabel, 0, 2, 1, 1);
    constraints.anchor = GridBagConstraints.WEST;
    inputPanel.add(periodLabel, constraints);

    arrageComponent(periodTField, 1, 2, 1, 1);
    constraints.anchor = GridBagConstraints.EAST;
    inputPanel.add(periodTField, constraints);

...
...
}

*****************************
  private void arrageComponent(Component component, int column, int row,
                               int width, int height) {
    constraints.gridx = column;
    constraints.gridy = row;

    constraints.gridwidth = width;
    constraints.gridheight = height;
    constraints.anchor = GridBagConstraints.LINE_END;
  }
0
Comment
Question by:dkim18
  • 3
4 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 200 total points
ID: 10732367
You'll never learn it ;)

inputPanel.setLayout( new GridBagLayout() );

before adding items to it
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10732375
When you do

new JPanel()

the panel has a *FlowLayout by default* (which explains the horizontal layout)
0
 

Author Comment

by:dkim18
ID: 10732577
I wasn't realizing that simple mistake...
you are right..java  is rather straightforward...

Thanks,
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10732643
You're welcome.
Thanks for accepting
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now