4

I use SpringLayout on my form, But as you see, its look isn't good (large and bad size)!

public class t8 extends JFrame {

JButton okButton, cancellButton;
JTextField idTF, nameTf;
JLabel idlbl, namelbl;

public t8() {
    add(createPanel(), BorderLayout.CENTER);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 500);
    setLocation(400, 100);
    setVisible(true);
}

public static void main(String[] args) {
    new t8();
}

public JPanel createPanel() {
    JPanel panel = new JPanel();
    okButton = new JButton("Ok");
    cancellButton = new JButton("Cancel");
    idTF = new JTextField(10);
    nameTf = new JTextField(10);
    idlbl = new JLabel("ID");
    namelbl = new JLabel("Name");
    panel.add(idlbl);
    panel.add(idTF);
    panel.add(namelbl);
    panel.add(nameTf);
    panel.add(okButton);
    panel.add(cancellButton);

    panel.setLayout(new SpringLayout());
    SpringUtilities.makeCompactGrid(panel, 3, 2, 20, 50, 50, 100);
    return panel;
}
}

I change makeCompactGrid numbers, But was not success!

(The width of JTextFields are large, and my button's size are different) enter image description here

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Sajad
  • 2,273
  • 11
  • 49
  • 92
  • Would `GourpLayout`, seen [here](http://stackoverflow.com/a/8504753/230513), be an alternative? – trashgod Aug 06 '13 at 01:27
  • 1
    +1 see [one of kicks](http://stackoverflow.com/a/16602249/714968) by @aterai, ratio is possible to set with (very similair and /or with anchor) GridBagLayout, rest of ideas is implemented in MigLayout – mKorbel Aug 06 '13 at 06:21

2 Answers2

1

I would suggest you to use Netbeans drag and drop tool which will allow you to set the components physically and will also give you the preview. If you want to do it with code just use the free layout and set the location and size of every component manually By setSize() and setLocation() methods, although this will require more lines of code but will ensure you that all the components are in their correct position.

MMujtabaRoohani
  • 483
  • 4
  • 19
  • 1
    AFAIK, `SpringLayout` is not supported in the NetBeans GUI editor. – trashgod Aug 04 '13 at 00:50
  • Ohh yeah, I forgot to mention this, thank you for doing it, secondly I don't think there is any need of springlayout in NetBeans, its drag and drop tool is very easy to use – MMujtabaRoohani Aug 04 '13 at 04:09
  • -1 for suggesting manual component positioning. That won't work if the look-and-feel is changed or even if the window is resized. – tbodt Aug 06 '13 at 07:55
  • Sorry to say but this is a further problem, for example the answer which is accepted now for this question can create further problems but that doesn't mean that His Answer is Invalid, Secondly, I am using Netbeans, The way he told to use GridLayout and GrigBagLayout, both of them is easily applicable graphically, using netbeans, and Thirdly, If you use your logic then you can manage a window resized or changed look and feel event for not harming you positioning, like I answered it stackoverflow.com/questions/17886096/creating-a-gui-via-netbeans/17893848#17893848 in this question. – MMujtabaRoohani Aug 07 '13 at 04:08
1

If you don't care about the layout manager, but only care about the layout, then you should use a GridLayout, or if you don't want all component to be the same size, a GridBagLayout. Here's how with a grid layout (only the modified method is shown):

public JPanel createPanel() {
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout());
    okButton = new JButton("Ok");
    cancellButton = new JButton("Cancel");
    idTF = new JTextField(10);
    nameTf = new JTextField(10);
    idlbl = new JLabel("ID");
    namelbl = new JLabel("Name");
    panel.add(idlbl);
    panel.add(idTF);
    panel.add(namelbl);
    panel.add(nameTf);
    panel.add(okButton);
    panel.add(cancellButton);
    return panel;
}

And with a GridBagLayout:

public JPanel createPanel() {
    JPanel panel = new JPanel();
    GridBagLayout gb = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    panel.setLayout(gb);
    okButton = new JButton("Ok");
    cancellButton = new JButton("Cancel");
    idTF = new JTextField(10);
    nameTf = new JTextField(10);
    idlbl = new JLabel("ID");
    namelbl = new JLabel("Name");
    add(panel, idlbl, 0, 0, 1, 1, gb, gbc, false);
    add(panel, idTF, 0, 1, 1, 1, gb, gbc, true);
    add(panel, namelbl, 1, 0, 1, 1, gb, gbc, false);
    add(panel, nameTf, 1, 1, 1, 1, gb, gbc, true);
    add(panel, okButton, 2, 0, 1, 1, gb, gbc, false);
    add(panel, cancellButton, 2, 1, 1, 1, gb, gbc, true);
    return panel;
}

private void add(Container outer, Component c, int x, int y, int w, int h, GridBagLayout gb, GridBagConstraints gbc, boolean wide) {
    gbc.gridx = x;
    gbc.gridy = y;
    gbc.gridwidth = w;
    gbc.gridheight = h;
    if (wide) {
        gbc.weightx = 100;
    } else {
        gbc.weightx = 0;
    }
    gb.setConstraints(c, gbc);
    outer.add(c);
}

I believe that the extra GridBagLayout complexity just might be worth it.

tbodt
  • 16,609
  • 6
  • 58
  • 83