Java JLabel/JButton: on some systems I get “…” (an ellipsis) and on some systems I don't. how can I force to disable the ellipsis at all?

一笑奈何 提交于 2019-11-26 21:55:17

问题


On most systems, the content in my JLabel just shows fine. It is also resided in a way that it should be always big enough to show its content text because I basically do this:

label.setText(text);
label.setFont(new Font(fontName, 0, 12));
int width = label.getFontMetrics(label.getFont()).stringWidth(text);
int height = 21; // this should always be enough
label.setBounds(new Rectangle(x, y, width, height));

But on some systems (not my own so I cannot really debug it that easy), it cuts the text and shows "..." at the end.

You can see the full code here and you can see the example here (Abbildungen_Bijektiv_X3).

I also have some similar case for JButton.

How can I force Swing to not do that? (Even if it thinks that the component is too small.)

Where exactly does Swing handle this? I browsed through the code of JButton and some related classes but I didn't really found the code where it cuts the text and adds the ellipsis.


回答1:


There should be no need to set the bounds of the label.

That is the job of a layout manager. Learn to use layout managers and you won't have this problem.

Edit:

Layout managers use:

label.setSize( label.getPreferredSize() );



回答2:


I am doing this now (for buttons but you could do it in a similar way for other controls):

static public class ButtonUI extends MetalButtonUI {
    public static ComponentUI createUI(JComponent c) {
        return new ButtonUI();
    }

    @Override public void paint(Graphics g, JComponent c) {
        JSimpleLabel.activateAntiAliasing(g);

        AbstractButton b = (AbstractButton) c;
        ButtonModel model = b.getModel();

        String text = b.getText();
        clearTextShiftOffset();

        // perform UI specific press action, e.g. Windows L&F shifts text
        if (model.isArmed() && model.isPressed()) {
            paintButtonPressed(g,b); 
        }

        FontMetrics metrics = g.getFontMetrics();
        Rectangle2D stringBounds = metrics.getStringBounds(text, g);
        g.drawString(text,
                (b.getWidth() - (int)stringBounds.getWidth()) / 2,
                metrics.getLeading() + metrics.getMaxAscent() + (b.getHeight() - (int)stringBounds.getHeight()) / 2);

        if (b.isFocusPainted() && b.hasFocus()) {
            Rectangle viewRect = new Rectangle();
            final int inset = 1;
            viewRect.x = inset;
            viewRect.y = inset;
            viewRect.width = b.getWidth() - (inset + viewRect.x) - 1;
            viewRect.height = b.getHeight() - (inset + viewRect.y) - 1;
            g.setColor(getFocusColor());
            g.drawRect(viewRect.x, viewRect.y, viewRect.width, viewRect.height);
        }
    }       
}

public void init() {
    try {
        UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel() {
            private static final long serialVersionUID = 1L;
            @Override public UIDefaults getDefaults() {
                UIDefaults table = super.getDefaults();
                table.put("ButtonUI", ButtonUI.class.getName());
                return table;
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
    // ...
}



回答3:


You could use a cross platform look and feel (Like Nimbus) to stop this occuring



来源:https://stackoverflow.com/questions/5380037/java-jlabel-jbutton-on-some-systems-i-get-an-ellipsis-and-on-some-syste

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!