简体   繁体   中英

Java/JButton: How do I get rid of ellipses on JButton?

I'm having this weird issue and it doesn't seem to be happening on Mac systems, but on Windows. I'm working on this GUI and am trying to make all of the buttons the same length (the length of the longest button) but for some reason on Windows, one of the buttons will not display the entire string and will truncate it with an ellipsis (...).

Here is the method I am using to check and set the longest button label:

    //Temp button to hold the longest button for resizing
    JButton longestButton = new JButton();

    //Find size of the largest of the 6 buttons
    for(int i = 0; i < 6; i++){
        if (buttons[i].getText().length() > longestButton.getText().length()){
            longestButton = buttons[i];
        }
    }

    //Match size of 6 hex buttons to the size of the longest one
    for(int i = 0; i < 6; i++){
        buttons[i].setPreferredSize(longestButton.getPreferredSize());
    }

Anybody have some insight into this?

Don't use setPreferredSize() when you really mean to override getPreferredSize() . BoxLayout works well for this: "For a top-to-bottom box layout, the preferred width of the container is that of the maximum preferred width of the children." In the example below, BorderLayout.LINE_END adopts the container's preferred width. Each button overrides getMaximumSize() to effectively remove the upper limit on the button's width. The result works well across platforms.

图片

import java.awt.*;
import javax.swing.*;

/**
 * @see https://stackoverflow.com/a/20085489/230513
 */
public class TestViewer {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new ButtonPanel(), BorderLayout.LINE_END);
                frame.pack();
                frame.setSize(500, 300);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    private static class ButtonPanel extends Box {

        public ButtonPanel() {
            super(BoxLayout.Y_AXIS);
            this.add(createButton("Button 1"));
            this.add(createButton("Button 2"));
            this.add(createButton("Long Button 3"));
            this.add(createButton("Button 4"));
            this.add(createButton("Button 5"));
        }

        private JButton createButton(String name) {
            final JButton b = new JButton(name) {

                @Override
                public Dimension getMaximumSize() {
                    return new Dimension(
                        Short.MAX_VALUE, getPreferredSize().height);
                }
            };
            b.setAlignmentX(0.5f);
            return b;
        }
    }
}

Instead of finding the button with the biggest .getText().length(), you could find the button with the widest preferredSize().

for(int i = 0; i < 6; i++){
    if (buttons[i].getPreferredSize().getWidth() > longestButton.getPreferredSize().getWidth()){
        longestButton = buttons[i];
    }
} 

This fixes the issue vandale mentioned in the comments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM