0

How can I do this with GridLayout or another Gridxxx?

enter link description here

I want add to panel JLabel and JTextField with different size.

Is it possible?

I have this code.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
class TEST_rozlozenia {

TEST_rozlozenia() {
    JPanel panel = new JPanel();
    JPanel lFields = new JPanel(new BorderLayout(2,2));
    JPanel labels = new JPanel(new GridLayout(0,1));
    labels.setBorder(new TitledBorder("Labels"));
    JPanel fields = new JPanel(new GridLayout(0,1));
    fields.setBorder(new TitledBorder("TextFields"));

    labels.add(new JLabel("Label"));
    labels.add(new JLabel("Label1"));
    fields.add(new JTextField(55));
    fields.add(new JTextField(10));

    lFields.add(labels, BorderLayout.WEST);
    lFields.add(fields, BorderLayout.EAST);

    panel.add(lFields, BorderLayout.NORTH);

    JOptionPane.showMessageDialog(null, panel);
}

public static void main(String[] args) throws Exception {
    SwingUtilities.invokeLater(new Runnable(){
        @Override
        public void run() {
            new TEST_rozlozenia();
        }
    });
}
}

Whats wrong?

I don't have a different textfields size.

Cœur
  • 37,241
  • 25
  • 195
  • 267
NINJO
  • 59
  • 1
  • 7
  • 2
    You could take a look at [`GridBagLayout`](http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html) – MadProgrammer Nov 15 '13 at 08:49
  • Also consider [`GroupLayout`](http://docs.oracle.com/javase/tutorial/uiswing/layout/group.html), seen [here](http://stackoverflow.com/a/8504753/230513). – trashgod Nov 15 '13 at 11:26
  • But JTextField1 size must be different as JTextField2 size. All examples has JTextField the same size. – NINJO Nov 15 '13 at 14:31
  • that's what you coded :-) Hint: read the api doc of GridLayout. BTW: please learn java naming conventions and stick to them. – kleopatra Nov 18 '13 at 14:03
  • The image in the link you posted is ugly, is that really the layout you want to give your users? – jfpoilpret Dec 26 '13 at 21:39
  • Yes I want the same layout as is in the image. – NINJO Jan 13 '14 at 10:47

1 Answers1

0

All examples has JTextField the same size

Then create you text fields with different sizes:

JTextField textField1 = new JTextField(10);
JTextField textField2 = new JTextField(20);

The parameter in the constructor will make a suggestion as to how big the text field should be. So any layout manager that respects the preferred size of the component will use this information.

camickr
  • 321,443
  • 19
  • 166
  • 288