marxin
marxin

Reputation: 3922

Java + Miglayout - top margin border issue?

I have problem with margin. Probably its very easy to solve, but i dont know what is the cause. I have four components, three jscrollpanels and one jpanel. Components are placed like this:

components

Problem is marked with red ellipse. How to wipe this margin out? I know, that problem is related with border (even that im creating border with same method for every component). Im using this:

setBorder(BorderFactory.createTitledBorder("Sterowanie:"));

But when i dont set a border for JPanel ( component with label "Sterowanie" ), it fill all place without margin. With border, it fill just a bordered region. The code which i use to place components:

proxys = new ItemViewer("Numery:");
add(proxys, "height 65%, width 33%");

accs = new ItemViewer("Konta:");
add(accs, "height 65%, width 33%");

panel = new JPanel();
panelLayout = new MigLayout("insets 0 0 0 0");
panel.setBorder(BorderFactory.createTitledBorder("Sterowanie:"));
add(panel, "height 65%, width 34%, wrap");

log = new Log("Log:");
add(log, "height 35%, width 100%, span");

Hm?

Upvotes: 4

Views: 1704

Answers (2)

Yago Méndez Vidal
Yago Méndez Vidal

Reputation: 884

The way you use MigLayout is a bit weird for me (no offence, please, I just learned another way). Try this:

content.setLayout(new MigLayout("fill", "[sg, grow][sg, grow][sg, grow]", "[65%][35%]"));
content.add(topLeftComponent, "grow");
content.add(topMiddleComponent, "grow");
content.add(topRightComponent, "grow, wrap");
content.add(bottomComponent, "span 3, grow");

I know it's not the same "spirit" but I always found this style easier to build.

Upvotes: 2

kleopatra
kleopatra

Reputation: 51525

No idea why that's happening (my first guess was a different vertical default alignment of the ItemView vs the plain panel), but can reproduce - and workaround by making all cells growable, either in the cell or in the row contraints:

    JComponent comp = new JScrollPane(new JTable(20, 1));
    comp.setBorder(new TitledBorder("Some Item"));
    JComponent other = new JScrollPane(new JTable(10, 1));
    other.setBorder(new TitledBorder("Other items"));
    JComponent panel = new JPanel();
    panel.setBorder(new TitledBorder("Stewhatever"));
    JTextArea log = new JTextArea();
    log.setBorder(new TitledBorder("Log"));

    MigLayout layout = new MigLayout("wrap 3, debug"); //, "", "[fill, grow]"); 
    JComponent content = new JPanel(layout);
    String cc = "width 33%, height 65%, grow";
    content.add(comp, cc);
    content.add(other, cc);
    content.add(panel, cc);
    content.add(log, "height 35%, span, grow");

Without any grow, the snippet reproduces your screenshot, with a grow in either cc or the commented row constraint, all upper components are aligned at the top.

Not sure if this is a bug or should be expected

Upvotes: 1

Related Questions