Solved

arranging labels and textfields inside of border by using GBL layout

Posted on 2004-04-01
4
348 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to see all occupied ports on windows 10 laptop 15 92
Adding multiple JVM environments to RedHat 6 7 78
Html split(text) 2 44
Java import explained 4 44
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 first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

734 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