/** * Returns the preferred size to set a component at in order to render * an html string. You can specify the size of one dimension. * * @see <a href="http://blog.nobel-joergensen.com/2009/01/18/changing-preferred-size-of-a-html-jlabel/">reference for this implementation</a> */ private Dimension calculatePreferredSize(JLabel labelWithHtmlText, boolean width, int preferredSize) { View view = (View) labelWithHtmlText.getClientProperty(BasicHTML.propertyKey); view.setSize(width ? preferredSize : 0, width ? 0 : preferredSize); float w = view.getPreferredSpan(View.X_AXIS); float h = view.getPreferredSpan(View.Y_AXIS); return new Dimension((int) Math.ceil(w), (int) Math.ceil(h)); }
/** * Helper method for {@linkplain #getHtmlLabelTextExtent(java.lang.String, int, boolean)}. This * is required because we are creating and invisibly rendering a {@code JLabel} object in this * method, and being virtuous in our Swing usage we should only do that on the event dispatch * thread. * * @param labelText the text to render, optionally enclosed in {@code <html>...</html>} tags * @param fixedDimSize the size of the fixed dimension (either width or height * @param width {@code true} if the fixed dimension is width; {@code false} for height * @return the rendered label text extent */ private static Dimension doGetHtmlTextExtent( String labelText, int fixedDimSize, boolean width) { final JLabel label = new JLabel(); if (labelText.startsWith("<html>")) { label.setText(labelText); } else { label.setText("<html>" + labelText + "</html>"); } View view = (View) label.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey); view.setSize(width ? fixedDimSize : 0, width ? 0 : fixedDimSize); float w = view.getPreferredSpan(View.X_AXIS); float h = view.getPreferredSpan(View.Y_AXIS); return new java.awt.Dimension((int) Math.ceil(w), (int) Math.ceil(h)); } }
if (o instanceof JLabel){ JLabel label = (JLabel)o; Integer labelId = (Integer)label.getClientProperty("id"); }
private Icon getTextRenderingIcon(JLabel lbl) { return (Icon) lbl.getClientProperty(ZoomableLabel.TEXT_RENDERING_ICON); }
private static int getLabelBaselineResizeBehavior(JLabel label) { if (label.getClientProperty("html") != null) { return BRB_OTHER; } switch(label.getVerticalAlignment()) { case JLabel.TOP: return BRB_CONSTANT_ASCENT; case JLabel.BOTTOM: return BRB_CONSTANT_DESCENT; case JLabel.CENTER: return BRB_CENTER_OFFSET; } return BRB_OTHER; }
public void propertyChange(PropertyChangeEvent evt) { if ("opaque".equals(evt.getPropertyName())) { if (!Boolean.TRUE.equals(c .getClientProperty(SubstanceButtonUI.LOCK_OPACITY))) { c.putClientProperty(SubstanceButtonUI.OPACITY_ORIGINAL, evt.getNewValue()); // System.out // .println("PCL: " // + b.getText() // + "->" // + b // .getClientProperty(SubstanceButtonUI.OPACITY_ORIGINAL) // ); } } } };
@Override public void propertyChange(PropertyChangeEvent evt) { if ("opaque".equals(evt.getPropertyName())) { if (!Boolean.TRUE.equals(c .getClientProperty(SubstanceButtonUI.LOCK_OPACITY))) { c.putClientProperty(SubstanceButtonUI.OPACITY_ORIGINAL, evt.getNewValue()); // System.out // .println("PCL: " // + b.getText() // + "->" // + b // .getClientProperty(SubstanceButtonUI.OPACITY_ORIGINAL) // ); } } } };
public class LabelUI extends MetalLabelUI { private static final int SWING_ELLIPSES_LEN = "...".length(); private static final ComponentUI LABEL_UI_INSTANCE = new LabelUI(); private String displayedText; public static ComponentUI createUI(JComponent c) { return LABEL_UI_INSTANCE; } public String getText() { return displayedText; } protected String layoutCL(JLabel label, FontMetrics fontMetrics, String text, Icon icon, Rectangle viewR, Rectangle iconR, Rectangle textR) { displayedText = super.layoutCL(label, fontMetrics, text, icon, viewR, iconR, textR); String truncationString = (String)label.getClientProperty("LabelUI.truncationString"); if (truncationString != null && !displayedText.equals(text)) { displayedText = displayedText.subSequence(0, displayedText.length() - SWING_ELLIPSES_LEN) + truncationString; } return displayedText; } }
public static void processText(JLabel label) { MessageFormat format = (MessageFormat) label.getClientProperty(PROPERTY_FORMAT); // NOI18N Pattern pattern = (Pattern) label.getClientProperty(PROPERTY_HIGHLIGHT_PATTERN); // NOI18N String s = computeFitText(label); if(format != null || pattern != null) { StringBuffer sb = new StringBuffer(); sb.append("<html>"); // NOI18N s = TextUtils.escapeForHTMLLabel(s); if(format != null) { format.format(new Object[] {s}, sb, null); } if(pattern != null) { sb.append(highLight(s, pattern)); } sb.append("</html>"); // NOI18N s = sb.toString(); } label.setText(s); }
void setTextFit(JLabel label, String text) { Font originalFont = (Font)label.getClientProperty("originalfont"); // Get the original Font from client properties if (originalFont == null) { // First time we call it: add it originalFont = label.getFont(); label.putClientProperty("originalfont", originalFont); } int stringWidth = label.getFontMetrics(originalFont).stringWidth(text); int componentWidth = label.getWidth(); if (stringWidth > componentWidth) { // Resize only if needed // Find out how much the font can shrink in width. double widthRatio = (double)componentWidth / (double)stringWidth; int newFontSize = (int)Math.floor(originalFont.getSize() * widthRatio); // Keep the minimum size // Set the label's font size to the newly determined size. label.setFont(new Font(originalFont.getName(), originalFont.getStyle(), newFontSize)); } else label.setFont(originalFont); // Text fits, do not change font size label.setText(text); }
@Override protected void paintEnabledText(JLabel l, Graphics g, String s, int textX, int textY) { View v = (l != null) ? (View) l.getClientProperty("html") : null; if (v != null) { super.paintEnabledText(l, g, s, textX, textY); } else { paintStyledText((StyledLabel) l, g, textX, textY); } }
@Override protected void paintDisabledText(JLabel l, Graphics g, String s, int textX, int textY) { View v = (l != null) ? (View) l.getClientProperty("html") : null; if (v != null) { super.paintDisabledText(l, g, s, textX, textY); } else { paintStyledText((StyledLabel) l, g, textX, textY); } }
public void mouseReleased (MouseEvent e) { // if the mouse was released within the bounds of the button, go // ahead and select it properly if (_selectedButton != null) { if (_selectedButton.contains(e.getX(), e.getY())) { // tell the model that the selection has changed (and // we'll respond and do our business Object elem = _selectedButton.getClientProperty("element"); _model.setSelectedItem(elem); } else { _selectedButton.setBorder(DESELECTED_BORDER); _selectedButton.repaint(); } // clear out the selected button indicator _selectedButton = null; } }
/** * Get the lines of text contained in the text label. The prepared lines is * cached as a client property, accessible via {@link #PROPERTY_KEY}. * * @param l * the label * @return the text lines of the label. */ @SuppressWarnings("unchecked") protected List<String> getTextLines(final JLabel l) { List<String> lines = (List<String>) l.getClientProperty(PROPERTY_KEY); if (lines == null) { lines = prepareLines(l); l.putClientProperty(PROPERTY_KEY, lines); } return lines; }
/** * Resizes the {@code View} object used by this label to match * the current size of the label. Resizing of the {@code View} * causes re-layout of HTML label (which affects its preferred size). * * @param label label whose {@code View} should be resized. */ private void resizeViewToMatchTheCurrentSize(JLabel label) { Object view = label.getClientProperty("html"); // NOI18N if (view instanceof View) { ((View)view).setSize(label.getWidth(), label.getHeight()); } }
protected void paintEnabledText(JLabel l, Graphics g, String s, int textX, int textY) { int mnemIndex; if (JTattooUtilities.getJavaVersion() >= 1.4) { mnemIndex = l.getDisplayedMnemonicIndex(); } else { mnemIndex = JTattooUtilities.findDisplayedMnemonicIndex(l.getText(), l.getDisplayedMnemonic()); } Object sc = l.getClientProperty("shadowColor"); if (sc instanceof Color) { g.setColor((Color)sc); JTattooUtilities.drawStringUnderlineCharAt(l, g, s, mnemIndex, textX, textY + 1); } g.setColor(l.getForeground()); JTattooUtilities.drawStringUnderlineCharAt(l, g, s, mnemIndex, textX, textY); }
JLabel labelBeingUsed = myLabel; View view = (View) labelBeingUsed.getClientProperty(BasicHTML.propertyKey); view.setSize(scrollPane1.getWidth(), 0.0f); float w = view.getPreferredSpan(View.X_AXIS); float h = view.getPreferredSpan(View.Y_AXIS); labelBeingUsed.setSize((int) w, (int) h);
JLabel labelBeingUsed = myLabel; View view = (View) labelBeingUsed.getClientProperty(BasicHTML.propertyKey); view.setSize(scrollPane1.getWidth(), 0.0f); float w = view.getPreferredSpan(View.X_AXIS); float h = view.getPreferredSpan(View.Y_AXIS); labelBeingUsed.setSize((int) w, (int) h);
private Dimension computePreferredSize() { Dimension preferredSize = null; final Insets insets = this.getInsets(); final int width = this.columns * this.getFontMetrics( this.getFont() ).charWidth( 'W' ) + ( insets != null ? insets.left + insets.right : 0 ); final JLabel label = new JLabel( this.getText() ); final Object html = label.getClientProperty( BasicHTML.propertyKey ); if ( html instanceof View ) { final View view = (View) html; view.setSize( width, 0 ); preferredSize = new Dimension( (int) view.getPreferredSpan( View.X_AXIS ), (int) view.getPreferredSpan( View.Y_AXIS ) ); } return preferredSize; }
public static void updateRenderer(JLabel c, String text) { View value = null; try{ View oldValue = (View)c.getClientProperty(propertyKey); if (isHTMLString(text)) { value = ScaledHTML.createHTMLView(c, text); } if (value != oldValue && oldValue != null) { for (int i = 0; i < oldValue.getViewCount(); i++) { oldValue.getView(i).setParent(null); } } } finally{ c.putClientProperty(BasicHTML.propertyKey, value); } }