Solved

arranging labels and textfields inside of border by using GBL layout

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

707 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

18 Experts available now in Live!

Get 1:1 Help Now