Layout management problems with MigLayout

2

I'm having trouble managing the layout of a JFrame that is a test.

How are you:

HowIwouldlikeittobe:

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

public class TesteMigImagem {

    public TesteMigImagem() {
        JFrame frame = new JFrame();
        frame.setLayout(new GridLayout(1,1));

        JPanel panel = new JPanel(new MigLayout());
        JPanel imagem = new JPanel();
        imagem.setBackground(Color.red);


        panel.add(new JLabel("Codigo"));
        panel.add(new JTextField(), "width 100%, wrap");
        panel.add(new JLabel("Nome"));
        panel.add(new JTextField(),"split, grow x");
        panel.add(imagem, "width :100:, height :100:, spany 2, wrap");
        panel.add(new JLabel("Endreço"));
        panel.add(new JTextField(), "grow x, split");

        panel.add(new JLabel("Numero"));
        panel.add(new JTextField(), "grow x");

        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        new TesteMigImagem();
    }
}
    
asked by anonymous 12.05.2014 / 20:22

1 answer

1

I can not validate this solution now, but following the example in the Merging and Splitting Cells section of MigLayout (page 2), to make a " col span ", change the line:

panel.add(imagem, "width :100:, height :100:, spany 2, wrap");

To:

panel.add(imagem, "width :100:, height :100:, span 1 2, wrap");

This should tell MigLayout to "do span " in one column and two lines. The spany command does not exist.

Anyway, as I said, I can not test this and maybe a few more adjustments are needed, but that's the way.

Update

I got a little time and made some changes that leave the layout closer to the desired one, but I still had to adjust the widths:

public class MigLayoutTest {
 public MigLayoutTest() {
     JFrame frame = new JFrame();
     frame.setLayout(new GridLayout(1,1));
  JPanel panel = new JPanel( new MigLayout() );
     JPanel imagem = new JPanel();
     imagem.setBackground(Color.red);

     panel.add(new JLabel("Codigo"));
  panel.add( new JTextField() , "width 100%, span 4, wrap" );
     panel.add(new JLabel("Nome"));
  panel.add( new JTextField() , "grow x, width :100:, span 3" );
  panel.add( imagem , "width :100:, height :100:, span 1 2, wrap" );
  panel.add( new JLabel( "Endreço" ) );
  panel.add( new JTextField() , "grow x, width :100: " );
  panel.add( new JLabel( "Numero" ) );
  panel.add( new JTextField() , "grow x, width :100:" );
     frame.add(panel);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setSize(300, 300);
     frame.setVisible(true);
 }
 public static void main(String args[]) {
     new MigLayoutTest();
 }
}

The key is to understand that MigLayout works as a table. In the first line I define 5 "imaginary columns" when adding the label and a field with span 4 .

Then, just add the other components thinking about how they will stay in this table.

    
12.05.2014 / 21:35