Example usage for javax.swing JLabel isOpaque

List of usage examples for javax.swing JLabel isOpaque

Introduction

In this page you can find the example usage for javax.swing JLabel isOpaque.

Prototype

public boolean isOpaque() 

Source Link

Document

Returns true if this component is completely opaque.

Usage

From source file:Main.java

@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
        boolean leaf, int row, boolean hasFocus) {
    Component c = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
    if (c instanceof JLabel) {
        JLabel label = (JLabel) c;
        editorPane.setText(label.getText());
        editorPane.setToolTipText(label.getToolTipText());
        editorPane.setOpaque(label.isOpaque());
        editorPane.setBackground(label.getBackground());
        editorPane.setBorder(label.getBorder());
    }//  w  w  w. j  ava2 s . c  o m
    return editorPane;
}

From source file:com.intel.stl.ui.common.view.ComponentFactory.java

/**
 * // w  w w.  j a  va  2s.co  m
 * <i>Description:</i> simple method that creates single/multi line label
 * with specified label width based on a source label. For more advanced
 * label attributes, it's the developer's responsibility to set them back.
 * 
 * @param source
 * @param wrap
 * @param maxWidth
 * @return
 */
public static JLabel deriveLabel(JLabel source, final boolean wrap, final int maxWidth) {
    JXLabel label = new JXLabel(source.getText(), source.getIcon(), source.getHorizontalAlignment()) {
        private static final long serialVersionUID = -4816144910055350011L;

        private Font cachedFont;

        private String chahedRawText, chahedText;

        /*
         * (non-Javadoc)
         * 
         * @see javax.swing.JLabel#getText()
         */
        @Override
        public String getText() {
            String text = super.getText();
            if (wrap || maxWidth <= 0 || text == null || text.isEmpty()) {
                return text;
            }

            if (getFont().equals(cachedFont) && text.equals(chahedRawText)) {
                return chahedText;
            }

            chahedRawText = text;
            cachedFont = getFont();
            FontMetrics fm = getFontMetrics(cachedFont);
            char[] chars = text.toCharArray();
            int width = fm.charsWidth(chars, 0, chars.length);
            if (width < maxWidth) {
                chahedText = text;
            } else {
                width += fm.charWidth('.') * 3;
                int pos = chars.length - 1;
                for (; pos >= 0 && width > maxWidth; pos--) {
                    width -= fm.charWidth(chars[pos]);
                }
                chahedText = new String(chars, 0, pos) + "...";
                if (getToolTipText() == null) {
                    setToolTipText(text);
                }
            }

            return chahedText;
        }

    };
    if (wrap) {
        label.setLineWrap(true);
    }
    if (maxWidth > 0) {
        label.setMaxLineSpan(maxWidth);
    }
    label.setEnabled(source.isEnabled());
    label.setForeground(source.getForeground());
    label.setOpaque(source.isOpaque());
    label.setBackground(source.getBackground());
    label.setFont(source.getFont());
    label.setBorder(source.getBorder());
    label.setToolTipText(source.getToolTipText());
    return label;
}