下面列出了javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns the baseline.
*
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
* @see javax.swing.JComponent#getBaseline(int, int)
* @since 1.6
*/
public int getBaseline(final JComponent c, final int width, final int height) {
super.getBaseline(c, width, height);
UIDefaults lafDefaults = UIManager.getLookAndFeelDefaults();
Component renderer = (Component) lafDefaults.get(BASELINE_COMPONENT_KEY);
if (renderer == null) {
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
renderer = tcr.getTableCellRendererComponent(table, "a", false, false, -1, -1);
lafDefaults.put(BASELINE_COMPONENT_KEY, renderer);
}
renderer.setFont(table.getFont());
int rowMargin = table.getRowMargin();
return renderer.getBaseline(Integer.MAX_VALUE, table.getRowHeight() -
rowMargin)
+ rowMargin / 2;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
Component c = renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
JLabel l = (JLabel) c;
double v = (Double) value;
if (unit.equals(UnknownUnit.getSingleton())) {
l.setText(nf.format(value));
} else {
double speedInMinutes = v * unit.getSpeed();
l.setText(DSCalculator.formatTimeInMinutes(speedInMinutes));
}
Color col = null;
if (v <= markerMin) {
col = ColorGradientHelper.getGradientColor(100f, Color.RED, Color.GREEN);
} else if ((v > markerMin) && (v < markerMax)) {
double range = markerMax - markerMin;
double val = v - markerMin;
col = ColorGradientHelper.getGradientColor((float) (100.0 - (100.0 * val / range)), Color.RED, Color.GREEN);
} else if (v >= markerMax) {
col = ColorGradientHelper.getGradientColor(0f, Color.RED, Color.GREEN);
}
c.setBackground(col);
return c;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
Component c = renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
if (value instanceof Double) {
String s = NumFormat.nice(((Double) value).doubleValue());
c = renderer.getTableCellRendererComponent(table, s, isSelected, hasFocus, row, col);
((JLabel) c).setHorizontalAlignment(SwingConstants.RIGHT);
}
return c;
}
/**
* Sets an initial <code>value</code> for the editor. This will cause the editor to <code>stopEditing</code>
* and lose any partially edited value if the editor is editing when this method is called. <p>
* <p>
* Returns the component that should be added to the client's <code>Component</code> hierarchy. Once installed
* in the client's hierarchy this component will then be able to draw and receive user input.
*
* @param table the <code>JTable</code> that is asking the editor to edit; can be <code>null</code>
* @param value the value of the cell to be edited; it is up to the specific editor to interpret and draw the
* value. For example, if value is the string "true", it could be rendered as a string or it could be rendered
* as a check box that is checked. <code>null</code> is a valid value
* @param isSelected true if the cell is to be rendered with highlighting
* @param row the row of the cell being edited
* @param column the column of the cell being edited
* @return the component for editing
*/
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row,
int column) {
final JPanel renderPanel = new JPanel(new BorderLayout());
final DefaultTableCellRenderer defaultRenderer = new DefaultTableCellRenderer();
final Component label = defaultRenderer.getTableCellRendererComponent(table, value, isSelected,
false, row, column);
renderPanel.add(label);
renderPanel.add(button, BorderLayout.EAST);
this.value[0] = (String) value;
return renderPanel;
}