1 7 8 package org.gjt.jclasslib.util; 9 10 import javax.swing.*; 11 import java.awt.*; 12 13 21 public class ExtendedJLabel extends JLabel implements Scrollable { 22 23 private boolean underlined = false; 24 private boolean autoTooltip = false; 25 26 29 public ExtendedJLabel() { 30 } 31 32 36 public ExtendedJLabel(String text) { 37 super(text); 38 } 39 40 public Dimension getPreferredScrollableViewportSize() { 41 return getSize(); 42 } 43 44 public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { 45 return getWidth() / 10; 46 } 47 48 public boolean getScrollableTracksViewportWidth() { 49 return false; 50 } 51 52 public boolean getScrollableTracksViewportHeight() { 53 return false; 54 } 55 56 public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { 57 return 10; 58 } 59 60 64 public boolean isUnderlined() { 65 return underlined; 66 } 67 68 72 public void setUnderlined(boolean underlined) { 73 this.underlined = underlined; 74 repaint(); 75 } 76 77 82 public boolean getAutoTooltip() { 83 return autoTooltip; 84 } 85 86 91 public void setAutoTooltip(boolean autoTooltip) { 92 this.autoTooltip = autoTooltip; 93 setToolTipText(getText()); 94 } 95 96 public void setText(String text) { 97 super.setText(text); 98 if (autoTooltip) { 99 setToolTipText(text); 100 } 101 } 102 103 107 public void setText(short number) { 108 setText(String.valueOf(number)); 109 } 110 111 115 public void setText(int number) { 116 setText(String.valueOf(number)); 117 } 118 119 123 public void setText(double number) { 124 setText(String.valueOf(number)); 125 } 126 127 131 public void setText(float number) { 132 setText(String.valueOf(number)); 133 } 134 135 139 public void setText(long number) { 140 setText(String.valueOf(number)); 141 } 142 143 public void paint(Graphics g) { 144 super.paint(g); 145 146 if (underlined) { 147 Insets i = getInsets(); 148 FontMetrics fm = g.getFontMetrics(); 149 150 Rectangle textRect = new Rectangle(); 151 Rectangle viewRect = new Rectangle(i.left, i.top, getWidth() - (i.right + i.left), getHeight() - (i.bottom + i.top) ); 152 153 SwingUtilities.layoutCompoundLabel( 154 this, 155 fm, 156 getText(), 157 getIcon(), 158 getVerticalAlignment(), 159 getHorizontalAlignment(), 160 getVerticalTextPosition(), 161 getHorizontalTextPosition(), 162 viewRect, 163 new Rectangle(), 164 textRect, 165 getText() == null ? 0 : ((Integer )UIManager.get("Button.textIconGap")).intValue() 166 ); 167 168 169 int offset = 2; 170 if (UIManager.getLookAndFeel().isNativeLookAndFeel() && System.getProperty("os.name").startsWith("Windows")) { 171 offset = 1; 172 } 173 g.fillRect(textRect.x + ((Integer )UIManager.get("Button.textShiftOffset")).intValue() , 174 textRect.y + fm.getAscent() + ((Integer )UIManager.get("Button.textShiftOffset")).intValue() + offset, 175 textRect.width, 176 1); 177 } 178 } 179 } 180 181 | Popular Tags |