views:

29

answers:

2

I am using a Miglayout to create a table-like layout for one of my panels. I need all my panels to have a fixed width of 200 pixels. When i add components in the panel everything works OK but when i try to insert a button that has a long text (and therefore needs more space than 200 px to display) the button overflows its cell and overlaps neighboring buttons. This code should demonstrate my problem:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;


/**
 * @author Savvas Dalkitsis
 */
public class Test {

    public static void main(String[] args) {
        JFrame f = new JFrame();
        JPanel content = new JPanel(new MigLayout("wrap 5","[200!]","[50!]"));
        JButton b = new JButton("Button 1");
        content.add(b,"growx");
        b = new JButton("Button 2");
        content.add(b,"growx");
        b = new JButton("Button with a very long text which should not be visible");
        content.add(b,"growx");
        b = new JButton("Button 4");
        content.add(b,"growx");
        b = new JButton("Button 5");
        content.add(b,"growx");
        b = new JButton("Button 6");
        content.add(b,"growx");
        b = new JButton("Button 7");
        content.add(b,"growx");
        b = new JButton("Button 8");
        content.add(b,"growx");
        b = new JButton("Button 9");
        content.add(b,"growx");
        b = new JButton("Button 10");
        content.add(b,"growx");
        f.setContentPane(content);
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

}

What i would like is the button to display all the text it can fit in the 200 pixels and then maybe some trailing periods like "Button with ver..."

Does any one have an idea on how to achieve this?

(you can get miglayout from here for testing)

+2  A: 

Just downloaded the layout to check it out. Is your solution just:

    b = new JButton("Button with a very long text which should not be visible");
    content.add(b,"growx, wmax 200");

It worked for me.

Mark Peters
ok this is weird... the [200!] in the layout constructor is supposed to do just that... I'm afraid though that even though your solution works it doesn't help me as i need to specify the max width in one place in code only (the reasons are complicated to explain). but +1 for the answer (not yet though as i'd like some more exposure in the not upvoted list but soon :)
Savvas Dalkitsis
I don't think the column constraint is supposed to be the same as the width constraint of a certain component. The quickstart guide says "A size not specified will default to the **component's**corresponding size." It says nothing about the column width. Remember the max width of the column hasn't been violated in that the next column starts at the same offset regardless of the size of components in the previous column.
Mark Peters
indeed... Any way to enforce a maximum width for the components globally in the layout constructor though? I temporarily "fixed" my code by adding the wmax constraint but it's ugly and not really extensible.
Savvas Dalkitsis
+1  A: 

Hello,

Mark Peters is correct. The size of the column does not really limit the size of the components in it.

Why not create a template component with maximum size set?

Cheers, Mikael Grev

Mikael Grev