Example usage for javax.swing JLabel getIcon

List of usage examples for javax.swing JLabel getIcon

Introduction

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

Prototype

public Icon getIcon() 

Source Link

Document

Returns the graphic image (glyph, icon) that the label displays.

Usage

From source file:Main.java

public static Rectangle getTextRectangle(JLabel label) {

    String text = label.getText();
    Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();

    if ((icon == null) && (text == null)) {
        return null;
    }/* ww w. j  a v  a 2s  .  c om*/

    Rectangle paintIconR = new Rectangle();
    Rectangle paintTextR = new Rectangle();
    Rectangle paintViewR = new Rectangle();
    Insets paintViewInsets = new Insets(0, 0, 0, 0);

    paintViewInsets = label.getInsets(paintViewInsets);
    paintViewR.x = paintViewInsets.left;
    paintViewR.y = paintViewInsets.top;
    paintViewR.width = label.getWidth() - (paintViewInsets.left + paintViewInsets.right);
    paintViewR.height = label.getHeight() - (paintViewInsets.top + paintViewInsets.bottom);

    Graphics g = label.getGraphics();
    if (g == null) {
        return null;
    }
    String clippedText = SwingUtilities.layoutCompoundLabel(label, g.getFontMetrics(), text, icon,
            label.getVerticalAlignment(), label.getHorizontalAlignment(), label.getVerticalTextPosition(),
            label.getHorizontalTextPosition(), paintViewR, paintIconR, paintTextR, label.getIconTextGap());

    return paintTextR;
}

From source file:MainClass.java

public Transferable createTransferable(JComponent comp) {
    image = null;/*ww w. j  a v a2  s . c  om*/

    if (comp instanceof JLabel) {
        JLabel label = (JLabel) comp;
        Icon icon = label.getIcon();
        if (icon instanceof ImageIcon) {
            image = ((ImageIcon) icon).getImage();
            return this;
        }
    } else if (comp instanceof AbstractButton) {
        AbstractButton button = (AbstractButton) comp;
        Icon icon = button.getIcon();
        if (icon instanceof ImageIcon) {
            image = ((ImageIcon) icon).getImage();
            return this;
        }
    }
    return null;
}

From source file:ImageSelection.java

public Transferable createTransferable(JComponent comp) {
    // Clear//from  w  w  w .j a  va  2 s .c  om
    image = null;

    if (comp instanceof JLabel) {
        JLabel label = (JLabel) comp;
        Icon icon = label.getIcon();
        if (icon instanceof ImageIcon) {
            image = ((ImageIcon) icon).getImage();
            return this;
        }
    } else if (comp instanceof AbstractButton) {
        AbstractButton button = (AbstractButton) comp;
        Icon icon = button.getIcon();
        if (icon instanceof ImageIcon) {
            image = ((ImageIcon) icon).getImage();
            return this;
        }
    }
    return null;
}

From source file:DragImage.java

public Transferable createTransferable(JComponent comp) {
    // Clear/*from  w w  w .  j  a v  a 2  s. c o  m*/
    source = null;
    image = null;

    if (comp instanceof JLabel) {
        JLabel label = (JLabel) comp;
        Icon icon = label.getIcon();
        if (icon instanceof ImageIcon) {
            image = ((ImageIcon) icon).getImage();
            source = label;
            return this;
        }
    }
    return null;
}

From source file:Main.java

public void paint(Graphics g, JComponent c) {

    JLabel label = (JLabel) c;
    String text = label.getText();
    Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();

    if ((icon == null) && (text == null)) {
        return;//from   w  w w. j a va 2 s  . co m
    }

    FontMetrics fm = g.getFontMetrics();
    paintViewInsets = c.getInsets(paintViewInsets);

    paintViewR.x = paintViewInsets.left;
    paintViewR.y = paintViewInsets.top;

    // Use inverted height & width
    paintViewR.height = c.getWidth() - (paintViewInsets.left + paintViewInsets.right);
    paintViewR.width = c.getHeight() - (paintViewInsets.top + paintViewInsets.bottom);

    paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
    paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;

    String clippedText = layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR);

    Graphics2D g2 = (Graphics2D) g;
    AffineTransform tr = g2.getTransform();
    if (clockwise) {
        g2.rotate(Math.PI / 2);
        g2.translate(0, -c.getWidth());
    } else {
        g2.rotate(-Math.PI / 2);
        g2.translate(-c.getHeight(), 0);
    }

    if (icon != null) {
        icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
    }

    if (text != null) {
        int textX = paintTextR.x;
        int textY = paintTextR.y + fm.getAscent();

        if (label.isEnabled()) {
            paintEnabledText(label, g, clippedText, textX, textY);
        } else {
            paintDisabledText(label, g, clippedText, textX, textY);
        }
    }

    g2.setTransform(tr);
}

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

/**
 * /*w w w  .  j a  v a 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;
}

From source file:net.sf.jabref.gui.menus.RightClickMenu.java

private Icon getFileIconForSelectedEntry() {
    if (panel.getMainTable().getSelectedRowCount() == 1) {
        BibEntry entry = panel.getMainTable().getSelected().get(0);
        if (entry.hasField(FieldName.FILE)) {
            JLabel label = FileListTableModel.getFirstLabel(entry.getFieldOptional(FieldName.FILE).get());
            if (label != null) {
                return label.getIcon();
            }/*from   w ww  .  j av  a  2s . co  m*/
        }
    }
    return IconTheme.JabRefIcon.FILE.getSmallIcon();
}

From source file:net.sf.jabref.gui.maintable.MainTableSelectionListener.java

/**
 * Process popup trigger events occurring on an icon cell in the table. Show a menu where the user can choose which
 * external resource to open for the entry. If no relevant external resources exist, let the normal popup trigger
 * handler do its thing instead.//from w w  w  .jav  a  2  s  .  c o  m
 *
 * @param e The mouse event defining this popup trigger.
 * @param row The row where the event occurred.
 * @param column the MainTableColumn associated with this table cell.
 */
private void showIconRightClickMenu(MouseEvent e, int row, MainTableColumn column) {
    BibEntry entry = tableRows.get(row);
    JPopupMenu menu = new JPopupMenu();
    boolean showDefaultPopup = true;

    // See if this is a simple file link field, or if it is a file-list
    // field that can specify a list of links:
    if (!column.getBibtexFields().isEmpty()) {
        for (String field : column.getBibtexFields()) {
            if (FieldName.FILE.equals(field)) {
                // We use a FileListTableModel to parse the field content:
                FileListTableModel fileList = new FileListTableModel();
                entry.getFieldOptional(field).ifPresent(fileList::setContent);
                for (int i = 0; i < fileList.getRowCount(); i++) {
                    FileListEntry flEntry = fileList.getEntry(i);
                    if (column.isFileFilter()
                            && (!flEntry.type.get().getName().equalsIgnoreCase(column.getColumnName()))) {
                        continue;
                    }
                    String description = flEntry.description;
                    if ((description == null) || (description.trim().isEmpty())) {
                        description = flEntry.link;
                    }
                    menu.add(new ExternalFileMenuItem(panel.frame(), entry, description, flEntry.link,
                            flEntry.type.get().getIcon(), panel.getBibDatabaseContext(), flEntry.type));
                    showDefaultPopup = false;
                }
            } else {
                if (SpecialFieldsUtils.isSpecialField(column.getColumnName())) {
                    // full pop should be shown as left click already shows short popup
                    showDefaultPopup = true;
                } else {
                    if (entry.hasField(field)) {
                        String content = entry.getField(field);
                        Icon icon;
                        JLabel iconLabel = GUIGlobals.getTableIcon(field);
                        if (iconLabel == null) {
                            icon = IconTheme.JabRefIcon.FILE.getIcon();
                        } else {
                            icon = iconLabel.getIcon();
                        }
                        menu.add(new ExternalFileMenuItem(panel.frame(), entry, content, content, icon,
                                panel.getBibDatabaseContext(), field));
                        showDefaultPopup = false;
                    }
                }
            }
        }
        if (showDefaultPopup) {
            processPopupTrigger(e, row);
        } else {
            menu.show(table, e.getX(), e.getY());
        }
    }
}

From source file:com.vgi.mafscaling.LogView.java

private void createLogViewPanel() {
    logViewPanel = new JPanel();
    GridBagLayout gbl_logViewPanel = new GridBagLayout();
    gbl_logViewPanel.columnWidths = new int[] { 0 };
    gbl_logViewPanel.rowHeights = new int[] { 0, 0 };
    gbl_logViewPanel.columnWeights = new double[] { 1.0 };
    gbl_logViewPanel.rowWeights = new double[] { 1.0, 0.0 };
    logViewPanel.setLayout(gbl_logViewPanel);
    try {/*w  w  w .j  a va 2  s. co  m*/
        logDataTable = new DBTable();
        logDataTable.copyColumnHeaderNames = true;
        logDataTable.defaultClickCountToStartEditor = 2;
        logDataTable.doNotUseDatabaseSort = true;
        logDataTable.listenKeyPressEventsWholeWindow = true;
        logDataTable.createControlPanel(DBTable.READ_NAVIGATION);
        logDataTable.enableExcelCopyPaste();
        logDataTable.setSortEnabled(false);
        logDataTable.setSkin(new TableSkin());
        logDataTable.refresh(new String[1][25]);
        logDataTable.setComparator(new DoubleComparator());
        logDataTable.getTable().setCellSelectionEnabled(true);
        logDataTable.getTable().setColumnSelectionAllowed(true);
        logDataTable.getTable().setRowSelectionAllowed(true);
        logDataTable.getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JTextField rowTextField = ((JTextField) logDataTable.getControlPanel().getComponent(3));
        rowTextField.setPreferredSize(null);
        rowTextField.setColumns(5);

        GridBagConstraints gbc_logDataTable = new GridBagConstraints();
        gbc_logDataTable.insets = insets0;
        gbc_logDataTable.anchor = GridBagConstraints.PAGE_START;
        gbc_logDataTable.fill = GridBagConstraints.BOTH;
        gbc_logDataTable.gridx = 0;
        gbc_logDataTable.gridy = 0;
        logViewPanel.add(logDataTable, gbc_logDataTable);
        listModel = new DefaultListModel<JLabel>();

        selectionCombo.removeAllItems();
        String name;
        JTableHeader tableHeader = logDataTable.getTableHeader();
        for (int i = 0; i < logDataTable.getColumnCount(); ++i) {
            Column col = logDataTable.getColumn(i);
            col.setNullable(true);
            col.setHeaderRenderer(new CheckboxHeaderRenderer(i + 1, tableHeader));
            name = col.getHeaderValue().toString();
            selectionCombo.addItem(name);
            listModel.addElement(new JLabel(name, new CheckBoxIcon(), JLabel.LEFT));
        }

        JList<JLabel> menuList = new JList<JLabel>(listModel);
        menuList.setOpaque(false);
        menuList.setCellRenderer(new ImageListCellRenderer());
        menuList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        menuList.setLayoutOrientation(JList.VERTICAL);
        menuList.setFixedCellHeight(25);
        menuList.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                try {
                    if (e.getClickCount() == 1 && colors.size() > 0) {
                        JList<?> list = (JList<?>) e.getSource();
                        int index = list.locationToIndex(e.getPoint());
                        if (index >= 0) {
                            int colIdx = logDataTable.getCurrentIndexForOriginalColumn(index);
                            Column col = logDataTable.getColumn(colIdx);
                            if (col.getHeaderRenderer() instanceof CheckboxHeaderRenderer) {
                                CheckboxHeaderRenderer renderer = (CheckboxHeaderRenderer) col
                                        .getHeaderRenderer();
                                JLabel label = (JLabel) list.getModel().getElementAt(index);
                                CheckBoxIcon checkIcon = (CheckBoxIcon) label.getIcon();
                                checkIcon.setChecked(!checkIcon.isChecked());
                                if (checkIcon.isChecked()) {
                                    checkIcon.setColor(colors.pop());
                                    JTable table = logDataTable.getTable();
                                    TableModel model = table.getModel();
                                    addXYSeries(model, index, col.getHeaderValue().toString(),
                                            checkIcon.getColor());
                                } else {
                                    colors.push(checkIcon.getColor());
                                    checkIcon.setColor(renderer.getDefaultColor());
                                    removeXYSeries(index);
                                }
                                list.repaint();
                            }
                        }
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        headerScrollPane = new JScrollPane(menuList);
        GridBagConstraints gbc_headersTree = new GridBagConstraints();
        gbc_headersTree.insets = insets0;
        gbc_headersTree.anchor = GridBagConstraints.PAGE_START;
        gbc_headersTree.fill = GridBagConstraints.BOTH;
        gbc_headersTree.gridx = 0;
        gbc_headersTree.gridy = 1;

        logViewPanel.add(headerScrollPane, gbc_headersTree);
        headerScrollPane.setVisible(false);

    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e);
    }
}

From source file:org.openmicroscopy.shoola.agents.measurement.util.ui.ResultsCellRenderer.java

/**
 * Adds the appropriate shape icon to the label.
 * /*from   w ww  .jav  a  2s.c o  m*/
 * @param shape above.
 */
private JComponent makeShapeIcon(JLabel label, String shape) {
    if (FigureUtil.SCRIBBLE_TYPE.equals(shape))
        label.setIcon(SCRIBBLE);
    else if (FigureUtil.LINE_TYPE.equals(shape))
        label.setIcon(LINE);
    else if (FigureUtil.LINE_CONNECTION_TYPE.equals(shape))
        label.setIcon(CONNECTION);
    else if (FigureUtil.POLYGON_TYPE.equals(shape))
        label.setIcon(POLYGON);
    else if (FigureUtil.POINT_TYPE.equals(shape))
        label.setIcon(POINT);
    else if (FigureUtil.RECTANGLE_TYPE.equals(shape))
        label.setIcon(RECTANGLE);
    else if (FigureUtil.ELLIPSE_TYPE.equals(shape))
        label.setIcon(ELLIPSE);
    else if (FigureUtil.TEXT_TYPE.equals(shape))
        label.setIcon(TEXT);
    else if (FigureUtil.MASK_TYPE.equals(shape))
        label.setIcon(MASK);
    else
        label.setText(shape);
    if (label.getIcon() != null) {
        JPanel p = new JPanel();
        FlowLayout layout = new FlowLayout(FlowLayout.CENTER);
        layout.setVgap(0);
        p.setBorder(BorderFactory.createEmptyBorder());
        p.setLayout(layout);
        p.setOpaque(true);
        p.setBackground(getBackground());
        p.add(label);
        return p;
    }
    return label;
}