Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

arranging labels and textfields inside of border by using GBL layout

Posted on 2004-04-01
4
Medium Priority
?
353 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 800 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

636 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