开发者

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?

开发者 https://www.devze.com 2023-02-18 14:45 出处:网络
On most systems, the cont开发者_Go百科ent 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:

On most systems, the cont开发者_Go百科ent 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.


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() );


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();
    }
    // ...
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消