Java Code Examples for javax.swing.JLabel#getIcon()

The following examples show how to use javax.swing.JLabel#getIcon() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThumbnailLabelUI.java    From pumpernickel with MIT License 6 votes vote down vote up
protected void calculateGeometry(JLabel label) {
	FontMetrics fm = label.getFontMetrics(label.getFont());
	String text = label.getText();
	Icon icon = label.getIcon();
	int verticalAlignment = label.getVerticalAlignment();
	int horizontalAlignment = label.getHorizontalAlignment();
	int verticalTextPosition = label.getVerticalTextPosition();
	int horizontalTextPosition = label.getHorizontalTextPosition();
	int textIconGap = label.getIconTextGap();
	viewR.setFrame(0, 0, getViewWidth(label), label.getHeight());
	SwingUtilities.layoutCompoundLabel(fm, text, icon, verticalAlignment,
			horizontalAlignment, verticalTextPosition,
			horizontalTextPosition, viewR, iconRect, textRect, textIconGap);

	textRect.x = label.getWidth() / 2 - textRect.width / 2;
	iconRect.x = label.getWidth() / 2 - iconRect.width / 2;
	iconRect.y = 0;
	textRect.y = iconRect.height + label.getIconTextGap();
}
 
Example 2
Source File: AbstractBox.java    From jclic with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object clone() {
  AbstractBox bx = (AbstractBox) super.clone();
  if (specialShape)
    bx.shape = new Area(shape);
  else
    bx.shape = bx;
  if (hostedComponent != null) {
    if (hostedComponent instanceof JLabel) {
      JLabel lb = (JLabel) hostedComponent;
      bx.hostedComponent = new JLabel(lb.getText(), lb.getIcon(), lb.getHorizontalAlignment());
      JComponent jc = bx.getContainerResolve();
      if (jc != null)
        jc.add(hostedComponent);
    } else
      bx.hostedComponent = null;
  }
  return bx;
}
 
Example 3
Source File: RotateLabelUI.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
private String layout(JLabel label, FontMetrics fm, int width, int height) {
    Insets insets = label.getInsets(paintViewInsets);
    String text = label.getText();
    Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();
    paintViewR.x = insets.left;
    paintViewR.y = insets.top;

    if (vertical) {
        paintViewR.height = width - (insets.left + insets.right);
        paintViewR.width = height - (insets.top + insets.bottom);
    } else {
        paintViewR.width = width - (insets.left + insets.right);
        paintViewR.height = height - (insets.top + insets.bottom);
    }

    paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
    paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
    return layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR);
}
 
Example 4
Source File: FirstRowCellEditor.java    From ramus with GNU General Public License v3.0 6 votes vote down vote up
public boolean isCellEditable(final EventObject anEvent) {
    if (anEvent instanceof MouseEvent) {
        final MouseEvent event = (MouseEvent) anEvent;
        final int row = treeTable.rowAtPoint(event.getPoint());
        final Rectangle bounds = tree.getRowBounds(row);
        int offset = bounds.x;
        final Object node = tree.getPathForRow(row).getLastPathComponent();
        final boolean leaf = tree.getModel().isLeaf(node);
        final boolean expanded = tree.isExpanded(row);
        final TreeCellRenderer tcr = tree.getCellRenderer();
        final Component treeComponent = tcr.getTreeCellRendererComponent(
                tree, node, true, expanded, leaf, row, false);
        if (treeComponent instanceof JLabel) {
            final JLabel label = (JLabel) treeComponent;

            final Icon icon = label.getIcon();
            if (icon != null) {
                offset += icon.getIconWidth() + label.getIconTextGap();
            }

        }
        if (event.getPoint().x < offset)
            return false;
    }
    return deligate.isCellEditable(anEvent);
}
 
Example 5
Source File: ResourceReferenceChooser.java    From gate-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
  JLabel renderer = (JLabel)super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
  if(value instanceof PluginTreeNode) {
    renderer.setEnabled(true);
    renderer.setIcon(pluginIcon);
  } else if(value instanceof PluginResourceTreeNode) {
    PluginResourceTreeNode node = (PluginResourceTreeNode)value;
    if(!node.isLeaf() || ((PluginFileFilter)pluginFileFilterBox.getSelectedItem()).pattern.matcher(node.fullPath).find()) {
      renderer.setEnabled(true);
    } else {
      // trick lifted from DefaultTreeCellRenderer to get the right disabled icon
      Icon icon = renderer.getIcon();
      LookAndFeel laf = UIManager.getLookAndFeel();
      Icon disabledIcon = laf.getDisabledIcon(tree, icon);
      if (disabledIcon != null) icon = disabledIcon;
      renderer.setDisabledIcon(icon);
      renderer.setEnabled(false);
    }
  }
  return renderer;
}
 
Example 6
Source File: QueryTableCellRenderer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static String computeFitText(JLabel label) {
    String text = label.getText();
    if(text == null) text = "";
    if (text.length() <= VISIBLE_START_CHARS + 3) return text;
    
    Icon icon = label.getIcon();
    int iconWidth = icon != null ? icon.getIconWidth() : 0;
    
    FontMetrics fm = label.getFontMetrics(label.getFont());
    int width = label.getSize().width - iconWidth;

    String sufix = "...";                                                   // NOI18N
    int sufixLength = fm.stringWidth(sufix);
    int desired = width - sufixLength;
    if (desired <= 0) return text;

    for (int i = 0; i <= text.length() - 1; i++) {
        String prefix = text.substring(0, i);
        int swidth = fm.stringWidth(prefix);
        if (swidth >= desired) {
            return prefix.length() > 0 ? prefix + sufix: text;
        }
    }
    return text;
}
 
Example 7
Source File: FlatTableHeaderUI.java    From FlatLaf with Apache License 2.0 6 votes vote down vote up
@Override
public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected,
	boolean hasFocus, int row, int column )
{
	Component c = delegate.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column );
	if( !(c instanceof JLabel) )
		return c;

	l = (JLabel) c;

	if( sortIconPosition == SwingConstants.LEFT ) {
		if( oldHorizontalTextPosition < 0 )
			oldHorizontalTextPosition = l.getHorizontalTextPosition();
		l.setHorizontalTextPosition( SwingConstants.RIGHT );
	} else {
		// top or bottom
		sortIcon = l.getIcon();
		origBorder = l.getBorder();
		l.setIcon( null );
		l.setBorder( this );
	}

	return l;
}
 
Example 8
Source File: UnitImageAnimation.java    From freecol with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void executeWithUnitOutForAnimation(JLabel unitLabel) {
    final GUI gui = getGUI();

    // Tile position should now be valid.
    if (gui.getAnimationTilePosition(this.tile) == null) {
        logger.warning("Failed attack animation for " + this.unit
            + " at tile: " + this.tile);
        return;
    }

    final Rectangle rect = gui.getAnimationTileBounds(this.tile);
    final ImageIcon icon = (ImageIcon)unitLabel.getIcon();
    for (AnimationEvent event : animation) {
        long time = System.nanoTime();
        if (event instanceof ImageAnimationEvent) {
            final ImageAnimationEvent ievent = (ImageAnimationEvent)event;
            Image image = ievent.getImage();
            if (mirror) {
                // FIXME: Add mirroring functionality to SimpleZippedAnimation
                image = ImageLibrary.createMirroredImage(image);
            }
            icon.setImage(image);
            gui.paintImmediately(rect);
            time = ievent.getDurationInMs()
                - (System.nanoTime() - time) / 1000000;
            if (time > 0) Utils.delay(time, "Animation delayed.");
        }
    }
    gui.refresh();
}
 
Example 9
Source File: XTable.java    From scelight with Apache License 2.0 5 votes vote down vote up
/**
 * Positions tool tips exactly over the cell they belong to, also avoids showing empty tool tips (by returning <code>null</code>).
 */
@Override
public Point getToolTipLocation( final MouseEvent event ) {
	// If tool tip is provided by the renderer or the table itself, use default location:
	if ( super.getToolTipText( event ) != null )
		return super.getToolTipLocation( event );
	
	// If no tool tip, return null to prevent displaying an empty tool tip
	if ( getToolTipText( event ) == null )
		return null;
	
	// Else align to the cell:
	final Point point = event.getPoint();
	
	final int column = columnAtPoint( point );
	final int row = rowAtPoint( point );
	
	if ( column < 0 || row < 0 ) // event is not on a cell
		return null;
	
	final Point location = getCellRect( row, column, false ).getLocation();
	// Adjust due to the tool tip border and padding:
	location.x += 1;
	location.y -= 4;
	
	// If the renderer has an icon, take icon size and gap into consideration:
	final Component renderer = prepareRenderer( getCellRenderer( row, column ), row, column );
	if ( renderer instanceof JLabel ) {
		final JLabel l = (JLabel) renderer;
		if ( l.getIcon() != null )
			location.x += l.getIcon().getIconWidth() + l.getIconTextGap();
	}
	
	return location;
}
 
Example 10
Source File: ChatPanel.java    From xyTalk-pc with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 解析输入框中的内容并发送消息
 */
private void sendMessage() {

	List<Object> inputDatas = parseEditorInput();
	boolean isImageOrFile = false;
	for (Object data : inputDatas) {
		if (data instanceof String && !data.equals("\n")) {
			sendTextMessage(null, (String) data);
		} else if (data instanceof JLabel) {
			isImageOrFile = true;
			JLabel label = (JLabel) data;
			ImageIcon icon = (ImageIcon) label.getIcon();
			String path = icon.getDescription();
			if (path != null && !path.isEmpty()) {
				/*
				 * sendFileMessage(path); showSendingMessage();
				 */

				shareAttachmentUploadQueue.add(path);
			}
		} else if (data instanceof FileEditorThumbnail) {
			isImageOrFile = true;

			FileEditorThumbnail component = (FileEditorThumbnail) data;
			System.out.println(component.getPath());
			shareAttachmentUploadQueue.add(component.getPath());

		}
	}

	if (isImageOrFile) {
		// 先上传第一个图片/文件
		dequeueAndUpload();
	}

	messageEditorPanel.getEditor().setText("");

}
 
Example 11
Source File: FlatBusyLabelUI.java    From FlatLaf with Apache License 2.0 5 votes vote down vote up
@Override
protected void installDefaults( JLabel c ) {
	super.installDefaults( c );

	disabledForeground = UIManager.getColor( "Label.disabledForeground" );

	// force recreation of busy painter for correct colors when switching LaF
	if( c.getIcon() != null ) {
		JXBusyLabel busyLabel = (JXBusyLabel) c;
		boolean oldBusy = busyLabel.isBusy();
		busyLabel.setBusy( false );
		busyLabel.setBusyPainter( null );
		busyLabel.setBusy( oldBusy );
	}
}
 
Example 12
Source File: ThumbnailLabelUI.java    From pumpernickel with MIT License 5 votes vote down vote up
protected void paintIcon(Graphics2D g, JLabel label, boolean isSelected,
		boolean isIndicated) {
	Icon icon = label.getIcon();
	if (icon == null)
		return;

	icon.paintIcon(label, g,
			iconRect.x + iconRect.width / 2 - icon.getIconWidth() / 2,
			iconRect.y + iconRect.height / 2 - icon.getIconHeight() / 2);
}
 
Example 13
Source File: SeaGlassLabelUI.java    From seaglass with Apache License 2.0 5 votes vote down vote up
protected void paint(SeaGlassContext context, Graphics g) {
    JLabel label = (JLabel) context.getComponent();
    Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();

    g.setColor(context.getStyle().getColor(context, ColorType.TEXT_FOREGROUND));
    g.setFont(style.getFont(context));
    context.getStyle().getGraphicsUtils(context).paintText(context, g, label.getText(), icon, label.getHorizontalAlignment(),
        label.getVerticalAlignment(), label.getHorizontalTextPosition(), label.getVerticalTextPosition(),
        label.getIconTextGap(), label.getDisplayedMnemonicIndex(), 0);
}
 
Example 14
Source File: SeaGlassLabelUI.java    From seaglass with Apache License 2.0 5 votes vote down vote up
public Dimension getPreferredSize(JComponent c) {
    JLabel label = (JLabel) c;
    Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();
    SeaGlassContext context = getContext(c);
    Dimension size = context.getStyle().getGraphicsUtils(context).getPreferredSize(context,
        context.getStyle().getFont(context), label.getText(), icon, label.getHorizontalAlignment(),
        label.getVerticalAlignment(), label.getHorizontalTextPosition(), label.getVerticalTextPosition(),
        label.getIconTextGap(), label.getDisplayedMnemonicIndex());

    context.dispose();
    return size;
}
 
Example 15
Source File: SeaGlassLabelUI.java    From seaglass with Apache License 2.0 5 votes vote down vote up
public Dimension getMinimumSize(JComponent c) {
    JLabel label = (JLabel) c;
    Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();
    SeaGlassContext context = getContext(c);
    Dimension size = context.getStyle().getGraphicsUtils(context).getMinimumSize(context, context.getStyle().getFont(context),
        label.getText(), icon, label.getHorizontalAlignment(), label.getVerticalAlignment(), label.getHorizontalTextPosition(),
        label.getVerticalTextPosition(), label.getIconTextGap(), label.getDisplayedMnemonicIndex());

    context.dispose();
    return size;
}
 
Example 16
Source File: SeaGlassLabelUI.java    From seaglass with Apache License 2.0 5 votes vote down vote up
public Dimension getMaximumSize(JComponent c) {
    JLabel label = (JLabel) c;
    Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();
    SeaGlassContext context = getContext(c);
    Dimension size = context.getStyle().getGraphicsUtils(context).getMaximumSize(context, context.getStyle().getFont(context),
        label.getText(), icon, label.getHorizontalAlignment(), label.getVerticalAlignment(), label.getHorizontalTextPosition(),
        label.getVerticalTextPosition(), label.getIconTextGap(), label.getDisplayedMnemonicIndex());

    context.dispose();
    return size;
}
 
Example 17
Source File: AvatarImagesScreen.java    From magarena with GNU General Public License v3.0 5 votes vote down vote up
void setSelectedAvatar(final JLabel iconLabel) {
    final Icon icon = iconLabel.getIcon();
    final BufferedImage bi = ImageHelper.getCompatibleBufferedImage(
            icon.getIconWidth(), icon.getIconHeight(),
            BufferedImage.TRANSLUCENT
    );
    final Graphics g = bi.createGraphics();
    // paint the Icon to the BufferedImage.
    icon.paintIcon(null, g, 0,0);
    g.dispose();
    setRightFooter(PlainMenuButton.build(this::doSaveAvatarAndClose,
            new ImageIcon(ImageHelper.scale(bi, 46, 46)),
            MText.get(_S1), MText.get(_S2))
    );
}
 
Example 18
Source File: RotateLabelUI.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
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;

      Graphics2D g2 = (Graphics2D) g;
  	AffineTransform transform = null;

      if (rotation != ROTATE_0) {
          transform = g2.getTransform();
          g2.rotate(rotation);
          
          if (rotation == ROTATE_90) {
              g2.translate(0, -c.getWidth());
          } else if (rotation == ROTATE_180) {
              g2.translate(-c.getWidth(), -c.getHeight());
          } else if (rotation == ROTATE_270) {
              g2.translate(-c.getHeight(), 0);
          }
      }

      FontMetrics fm = g.getFontMetrics();
      String clippedText = layout(label, fm, c.getWidth(), c.getHeight());

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

      if (text != null) {
   View v = (View)c.getClientProperty(BasicHTML.propertyKey);
   if (v != null) {
v.paint(g, paintTextR);
   } else {
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);
}
   }
      }

      if (transform != null) g2.setTransform(transform);
  }
 
Example 19
Source File: VerticalLabelUI.java    From pdfxtk with Apache License 2.0 4 votes vote down vote up
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;
 
   FontMetrics  fm = g.getFontMetrics();
   paintViewInsets = c.getInsets(paintViewInsets);
   
   paintViewR.x      = paintViewInsets.top;
   paintViewR.y      = paintViewInsets.left;
   paintViewR.width  = c.getHeight() - (paintViewInsets.top  + paintViewInsets.bottom);
   paintViewR.height = c.getWidth()  - (paintViewInsets.left + paintViewInsets.right);
   
   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, g2, paintIconR.x, paintIconR.y);
   }
   
   if (text != null) {
     View v = (View) c.getClientProperty(BasicHTML.propertyKey);
     if (v != null) {
v.paint(g2, paintTextR);
     } else {
int textX = paintTextR.x;
int textY = paintTextR.y + fm.getAscent();

if (label.isEnabled())
  paintEnabledText(label, g2, clippedText, textX, textY);
else
  paintDisabledText(label, g2, clippedText, textX, textY);
     }
   }
 }
 
Example 20
Source File: ImageRGBEditor.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
private static void findImageUseImpl(com.codename1.ui.Image resourceValue, Vector users, EditableResources res, JLabel borderPreview) {
    for(String themeName : res.getThemeResourceNames()) {
        Hashtable theme = res.getTheme(themeName);
        for(Object key : theme.keySet()) {
            Object value = theme.get(key);
            if(value instanceof com.codename1.ui.Image) {
                if(value.equals(resourceValue)) {
                    addToUsers((String)key, users);
                }
            }
            if(value instanceof Border) {
                Border b = (Border)value;
                // BORDER_TYPE_IMAGE
                if(Accessor.getType(b) == Accessor.TYPE_IMAGE || Accessor.getType(b) == Accessor.TYPE_IMAGE_HORIZONTAL ||
                        Accessor.getType(b) == Accessor.TYPE_IMAGE_VERTICAL) {
                    com.codename1.ui.Image[] images = Accessor.getImages(b);
                    for(int i = 0 ; i < images.length ; i++) {
                        if(images[i] == resourceValue) {
                            addToUsers((String)key, users);
                            if(borderPreview != null && borderPreview.getIcon() == null) {
                                int borderWidth = Math.max(100, b.getMinimumWidth());
                                int borderHeight = Math.max(100, b.getMinimumHeight());
                                com.codename1.ui.Image img = com.codename1.ui.Image.createImage(borderWidth, borderHeight);
                                com.codename1.ui.Label l = new com.codename1.ui.Label("Preview");
                                l.getStyle().setBorder(b);
                                l.setSize(new com.codename1.ui.geom.Dimension(borderWidth, borderHeight));
                                l.paintComponent(img.getGraphics());
                                CodenameOneImageIcon icon = new CodenameOneImageIcon(img, borderWidth, borderHeight);
                                borderPreview.setIcon(icon);
                            }
                        }
                    }
                }
            }
        }
    }
    // check if a timeline is making use of said image and replace it
    for(String image : res.getImageResourceNames()) {
        com.codename1.ui.Image current = res.getImage(image);
        if(current instanceof com.codename1.ui.animations.Timeline) {
            com.codename1.ui.animations.Timeline time = (com.codename1.ui.animations.Timeline)current;
            for(int iter = 0 ; iter < time.getAnimationCount() ; iter++) {
                com.codename1.ui.animations.AnimationObject o = time.getAnimation(iter);
                if(AnimationAccessor.getImage(o) == resourceValue) {
                    addToUsers(image, users);
                }
            }
        }
    }

    // check if a UI resource is making use of the image
    UIBuilderOverride builder = new UIBuilderOverride();
    for(String uiResource : res.getUIResourceNames()) {
        com.codename1.ui.Container c = builder.createContainer(res, uiResource);
        if(ResourceEditorView.findImageInContainer(c, resourceValue)) {
            addToUsers(uiResource, users);
        }
    }
}