4

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
marxin
  • 3,692
  • 3
  • 31
  • 44
  • Hadn't had much of the experience with `MigLayout`, though had you tried using a [CompoundBorder](http://docs.oracle.com/javase/7/docs/api/javax/swing/border/CompoundBorder.html), but had you tried using it along with your `TitledBorder`, simply add one `EmptyBorder` with this, hope this might can help in someway :-) – nIcE cOw Jun 10 '12 at 04:51

2 Answers2

2

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.

  • 1
    Of course no offence :) I forgot to mark this question as solved, I've used kleopatra solution year ago. But your answer is useful too, maybe someone will have same problem and then he can try both solutions. Thanks! – marxin Nov 06 '13 at 11:56
1

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

kleopatra
  • 51,061
  • 28
  • 99
  • 211