下面列出了javax.swing.JLabel#getText ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void checkDisplayValues(Long latest, Long my, Long original) throws Exception {
waitForPrompting();
String latestExpected = getValueString(latest);
String myExpected = getValueString(my);
String originalExpected = getValueString(original);
waitForPostedSwingRunnables();
Window window = windowForComponent(getMergePanel());
final VerticalChoicesPanel comp = findComponent(window, VerticalChoicesPanel.class);
JLabel latestLabel = (JLabel) findComponentByName(comp, comp.getComponentName(1, 1));
JLabel myLabel = (JLabel) findComponentByName(comp, comp.getComponentName(2, 1));
JLabel originalLabel = (JLabel) findComponentByName(comp, comp.getComponentName(3, 1));
String latestValue = latestLabel.getText();
String myValue = myLabel.getText();
String originalValue = originalLabel.getText();
assertEquals(latestExpected, latestValue);
assertEquals(myExpected, myValue);
assertEquals(originalExpected, originalValue);
}
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;
}
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);
}
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();
}
@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;
}
private int getTagConflictCount() throws Exception {
VerticalChoicesPanel mergePanel = getMergePanel(VerticalChoicesPanel.class);
assertNotNull(mergePanel);
JLabel header = (JLabel) TestUtils.getInstanceField("headerLabel", mergePanel);
String headerStr = header.getText();
headerStr = headerStr.replaceAll("[^0-9]+", " ");
String[] numbers = headerStr.trim().split(" ");
if (numbers.length >= 2) {
return Integer.valueOf(numbers[1]);
}
return -1;
}
private void checkStatusMessage(String expectedMessage) {
PluginTool pluginTool = plugin.getTool();
DockingWindowManager windowManager = pluginTool.getWindowManager();
Object rootNode = TestUtils.invokeInstanceMethod("getRootNode", windowManager);
StatusBar statusBar = (StatusBar) TestUtils.getInstanceField("statusBar", rootNode);
JLabel statusLabel = (JLabel) TestUtils.getInstanceField("statusLabel", statusBar);
String statusMessage = statusLabel.getText();
assertEquals(expectedMessage, statusMessage);
}
public String getLabeledBy() {
if (getComponent() instanceof JComponent) {
try {
JLabel label = (JLabel) ((JComponent) getComponent()).getClientProperty("labeledBy");
if (label != null && label.getText() != null && !label.getText().equals("")) {
return stripLastColon(label.getText().trim());
}
} catch (ClassCastException e) {
}
}
return null;
}
public BlingTimer(Component tab, String name) {
super(300, null);
addActionListener(this);
component = tab;
if (component != null && component instanceof JPanel) {
label = (JLabel)((JPanel)component).getComponent(0);
src = label.getText();
if (src == null || src.isEmpty()) {
src = name;
}
d = label.getSize();
label.setPreferredSize(d);
}
}
@Override
protected void paintText(Graphics2D g, JLabel label, String text,
boolean isSelected, boolean isIndicated) {
if (isSelected) {
roundRect.setRoundRect(textRect.x - 4, textRect.y,
textRect.width + 8, textRect.height, 16, 16);
g.setColor(selectedTextBackground);
g.fill(roundRect);
g.setColor(Color.white);
} else {
g.setColor(Color.black);
}
super.paintText(g, label, label.getText(), isSelected, isIndicated);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
JLabel l = (JLabel) c;
String text = l.getText();
l.setText("<html><head>" + BBCodeFormatter.getStyles() + "</head><body><nobr>" + BBCodeFormatter.toHtml(text) + "</nobr></body></html>");
l.setToolTipText("<html><head>" + BBCodeFormatter.getStyles() + "</head><body>" + BBCodeFormatter.toHtml(text) + "</body></html>");
return l;
}
public int getBaseline(JComponent c, int width, int height) {
if (c == null) {
throw new NullPointerException("Component must be non-null");
}
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Width and height must be >= 0");
}
JLabel label = (JLabel) c;
String text = label.getText();
if (text == null || "".equals(text)) {
return -1;
}
Insets i = label.getInsets();
Rectangle viewRect = new Rectangle();
Rectangle textRect = new Rectangle();
Rectangle iconRect = new Rectangle();
viewRect.x = i.left;
viewRect.y = i.top;
viewRect.width = width - (i.right + viewRect.x);
viewRect.height = height - (i.bottom + viewRect.y);
// layout the text and icon
SeaGlassContext context = getContext(label);
FontMetrics fm = context.getComponent().getFontMetrics(context.getStyle().getFont(context));
context.getStyle().getGraphicsUtils(context).layoutText(context, fm, label.getText(), label.getIcon(),
label.getHorizontalAlignment(), label.getVerticalAlignment(), label.getHorizontalTextPosition(),
label.getVerticalTextPosition(), viewRect, iconRect, textRect, label.getIconTextGap());
View view = (View) label.getClientProperty(BasicHTML.propertyKey);
int baseline;
if (view != null) {
baseline = BasicHTML.getHTMLBaseline(view, textRect.width, textRect.height);
if (baseline >= 0) {
baseline += textRect.y;
}
} else {
baseline = textRect.y + fm.getAscent();
}
context.dispose();
return baseline;
}
/**
* Returns the rendered component.
*/
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = baseRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (c instanceof JLabel && units!=null) {
JLabel label = (JLabel)c;
if (label.getText()!=null && !label.getText().equals("")) //$NON-NLS-1$
label.setText(label.getText()+units);
label.setToolTipText(tooltip);
}
return c;
}
/**
* Adjusts the column width of the row headers table containg the number
* column. The font metrics are extracted from the label of the row at the
* bottom of the viewport and used to determing the appropriate width.
*
* The reason why this method is important, is that when the row number increases by an extra digit
* the column needs to get wider. It also needs to shrink when scrolling to smaller digit numbers.
*
* @param rowHeadersTable - single column table in the row header
* @param label - label used to get font metrics
* @param scrollBarValue - int value for determing point of lowest row
*/
private static void adjustColumnWidth(final JTable rowHeadersTable, final JLabel label, int scrollBarValue) {
label.setFont(rowHeadersTable.getFont());
label.setOpaque(true);
label.setHorizontalAlignment(JLabel.CENTER);
int v = rowHeadersTable.getVisibleRect().height;
int row = rowHeadersTable.rowAtPoint(new Point(0, v + scrollBarValue));
Integer modelValue = null;
if (row != -1) {
modelValue = (Integer) rowHeadersTable.getModel().getValueAt(row, 0);
} else {
RowHeadersTableModel tm = (RowHeadersTableModel) rowHeadersTable.getModel();
modelValue = new Integer(tm.getMaxIntValue());
}
label.setText("" + modelValue);
FontMetrics fontMetrics = label.getFontMetrics(label.getFont());
int widthFactor = 0;
if (fontMetrics != null && label.getText() != null) {
widthFactor = fontMetrics.stringWidth(label.getText());
rowHeadersTable.setPreferredScrollableViewportSize(new Dimension(widthFactor + 8, 100)); // height
// is
// ignored
rowHeadersTable.repaint();
}
}
protected String getSelectedUser() {
JLabel label = (JLabel) participantsList.getSelectedValue();
if (label != null) {
return label.getText();
}
return null;
}
/**
* @see javax.swing.DefaultListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean)
*/
public Component getListCellRendererComponent(JList list, final Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected,
cellHasFocus);
final JCheckBox box = new JCheckBox(label.getText());
box.setBackground(label.getBackground());
box.setSelected(((ConfigurableEmitter) value).isEnabled());
checks.put(value, box);
return box;
}
protected String getStringValue(ROW_OBJECT rowObject, int column) {
// we have to account for 'magic' hidden columns
if (columnModel instanceof GTableColumnModel) {
if (!((GTableColumnModel) columnModel).isVisible(column)) {
return null;
}
}
if (columnUsesConstraintFilteringOnly(column)) {
// This allows columns to be ignored for default text filtering while still being
// filterable through the column constraints API
return null;
}
// note: this call can be slow when columns dynamically calculate values from the database
Object value = model.getColumnValueForRow(rowObject, column);
if (value == null) {
return null;
}
/*
Methods for turning the cell value into a string to be filtered (in preference order):
1) Use the dynamic column's renderer (if applicable), as this is the most
direct way for clients to specify the filter value
2) See if the value is an instance of DisplayStringProvider, which describes how
it should be rendered
3) See if it is a label (this is uncommon)
4) Rely on the toString(); this works as intended for Strings. This is the
default way that built-in table cell renderers will generate display text
*/
// 1)
String renderedString = getRenderedColumnValue(value, column);
if (renderedString != null) {
return renderedString;
}
// 2) special plug-in point where clients can specify a value object that can return
// its display string
if (value instanceof DisplayStringProvider) {
return ((DisplayStringProvider) value).toString();
}
// 3
if (value instanceof JLabel) { // some models do this odd thing
JLabel label = (JLabel) value;
String valueString = label.getText();
return valueString == null ? "" : valueString;
}
// 4)
return value.toString();
}
public String getText(String cellName) {
JLabel cell = getCellByName(cellName);
return (cell != null) ? cell.getText() : null;
}
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);
}
@Override
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.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 );
}