Question

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)

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top