Java Code Examples for javax.swing.table.TableCellRenderer

The following examples show how to use javax.swing.table.TableCellRenderer. These examples are extracted from open source projects. 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
/**
 * Installs alignment decorators in the table column at {@code colViewIndex}.
 *
 * @param table a table.
 * @param colViewIndex the index of the column in the table <i>view</i>.
 * @param alignment one of the following constants:
 *        <ul>
 *        <li>{@link SwingConstants#LEFT}</li>
 *        <li>{@link SwingConstants#CENTER} (the default for image-only labels)</li>
 *        <li>{@link SwingConstants#RIGHT}</li>
 *        <li>{@link SwingConstants#LEADING} (the default for text-only labels)</li>
 *        <li>{@link SwingConstants#TRAILING}</li>
 *        </ul>
 */
public static void installInOneColumn(JTable table, int colViewIndex, int alignment) {
  TableColumn tableColumn = table.getColumnModel().getColumn(colViewIndex);

  TableCellRenderer headerRenderer = tableColumn.getHeaderRenderer();
  if (headerRenderer == null) {
    headerRenderer = table.getTableHeader().getDefaultRenderer();
  }
  if (!(headerRenderer instanceof RendererAlignmentDecorator)) { // Don't install a redundant decorator.
    tableColumn.setHeaderRenderer(new RendererAlignmentDecorator(headerRenderer, alignment));
  }

  TableCellRenderer cellRenderer = tableColumn.getCellRenderer();
  if (cellRenderer == null) {
    cellRenderer = table.getDefaultRenderer(table.getColumnClass(colViewIndex));
  }
  if (!(cellRenderer instanceof RendererAlignmentDecorator)) { // Don't install a redundant decorator.
    tableColumn.setCellRenderer(new RendererAlignmentDecorator(cellRenderer, alignment));
  }
}
 
Example 2
Source Project: darklaf   Source File: DarkTableCellRendererDelegate.java    License: MIT License 6 votes vote down vote up
@Override
public Component getTableCellRendererComponent(final JTable table, final Object value,
                                               final boolean isSelected, final boolean hasFocus,
                                               final int row, final int column) {
    TableCellRenderer renderer = TableConstants.useBooleanEditorForValue(value, table)
            ? getBooleanRenderer(table)
            : super.getDelegate();
    Component component = renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

    boolean isRowFocus = DarkTableCellFocusBorder.isRowFocusBorder(table);
    boolean isLeadSelectionCell = DarkUIUtil.hasFocus(table) && hasFocus && !isRowFocus;
    boolean paintSelected = isSelected && !isLeadSelectionCell && !table.isEditing();

    if (component instanceof JComponent) {
        setupBorderStyle(table, row, column, (JComponent) component, isRowFocus);
    }
    CellUtil.setupTableForeground(component, table, paintSelected);
    CellUtil.setupTableBackground(component, table, paintSelected, row);
    return component;
}
 
Example 3
/**
 * Returns true if the given point is outside the preferredSize of the
 * item at the given row of the table.  (Column must be 0).
 * Does not check the "Table.isFileList" property. That should be checked
 * before calling this method.
 * This is used to make WindowsL&F JFileChooser act like native dialogs.
 */
public static boolean pointOutsidePrefSize(JTable table, int row, int column, Point p) {
    if (table.convertColumnIndexToModel(column) != 0 || row == -1) {
        return true;
    }
    TableCellRenderer tcr = table.getCellRenderer(row, column);
    Object value = table.getValueAt(row, column);
    Component cell = tcr.getTableCellRendererComponent(table, value, false,
            false, row, column);
    Dimension itemSize = cell.getPreferredSize();
    Rectangle cellBounds = table.getCellRect(row, column, false);
    cellBounds.width = itemSize.width;
    cellBounds.height = itemSize.height;

    // See if coords are inside
    // ASSUME: mouse x,y will never be < cell's x,y
    assert (p.x >= cellBounds.x && p.y >= cellBounds.y);
    return p.x > cellBounds.x + cellBounds.width ||
            p.y > cellBounds.y + cellBounds.height;
}
 
Example 4
private int getHeaderHeight() {
  int height = 0;
  TableColumnModel columnModel = header.getColumnModel();
  for (int column = 0; column < columnModel.getColumnCount(); column++) {
    TableColumn aColumn = columnModel.getColumn(column);
    TableCellRenderer renderer = aColumn.getHeaderRenderer();
    if (renderer == null)
      renderer = header.getDefaultRenderer();
    Component comp = renderer.getTableCellRendererComponent(header.getTable(),
        aColumn.getHeaderValue(), false, false, -1, column);
    int cHeight = comp.getPreferredSize().height;
    Enumeration<?> en = ((GroupableTableHeader) header).getColumnGroups(aColumn);
    if (en != null) {
      while (en.hasMoreElements()) {
        ColumnGroup cGroup = (ColumnGroup) en.nextElement();
        cHeight += cGroup.getSize(header.getTable()).height;
      }
    }
    height = Math.max(height, cHeight);
  }
  return height;
}
 
Example 5
Source Project: openjdk-jdk8u   Source File: XTable.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public Component prepareRenderer(TableCellRenderer renderer,
                                 int row, int column) {
    Component comp = super.prepareRenderer(renderer, row, column);

    if (normalFont == null) {
        normalFont = comp.getFont();
        boldFont = normalFont.deriveFont(Font.BOLD);
    }

    if (column == VALUE_COLUMN && isAttributeViewable(row, VALUE_COLUMN)) {
        comp.setFont(boldFont);
    } else {
        comp.setFont(normalFont);
    }

    return comp;
}
 
Example 6
Source Project: dragonwell8_jdk   Source File: XTable.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public Component prepareRenderer(TableCellRenderer renderer,
                                 int row, int column) {
    Component comp = super.prepareRenderer(renderer, row, column);

    if (normalFont == null) {
        normalFont = comp.getFont();
        boldFont = normalFont.deriveFont(Font.BOLD);
    }

    if (column == VALUE_COLUMN && isAttributeViewable(row, VALUE_COLUMN)) {
        comp.setFont(boldFont);
    } else {
        comp.setFont(normalFont);
    }

    return comp;
}
 
Example 7
/**
 * Returns true if the given point is outside the preferredSize of the
 * item at the given row of the table.  (Column must be 0).
 * Does not check the "Table.isFileList" property. That should be checked
 * before calling this method.
 * This is used to make WindowsL&F JFileChooser act like native dialogs.
 */
public static boolean pointOutsidePrefSize(JTable table, int row, int column, Point p) {
    if (table.convertColumnIndexToModel(column) != 0 || row == -1) {
        return true;
    }
    TableCellRenderer tcr = table.getCellRenderer(row, column);
    Object value = table.getValueAt(row, column);
    Component cell = tcr.getTableCellRendererComponent(table, value, false,
            false, row, column);
    Dimension itemSize = cell.getPreferredSize();
    Rectangle cellBounds = table.getCellRect(row, column, false);
    cellBounds.width = itemSize.width;
    cellBounds.height = itemSize.height;

    // See if coords are inside
    // ASSUME: mouse x,y will never be < cell's x,y
    assert (p.x >= cellBounds.x && p.y >= cellBounds.y);
    return p.x > cellBounds.x + cellBounds.width ||
            p.y > cellBounds.y + cellBounds.height;
}
 
Example 8
Source Project: ghidra   Source File: AbstractGenericTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Gets the rendered value for the specified table cell.  The actual value at the cell may
 * not be a String.  This method will get the String display value, as created by the table.
 * 
 * @param table the table to query
 * @param row the row to query
 * @param column the column to query
 * @return the String value
 * @throws IllegalArgumentException if there is no renderer or the rendered component is
 *         something from which this method can get a String (such as a JLabel)
 */
public static String getRenderedTableCellValue(JTable table, int row, int column) {

	return runSwing(() -> {

		TableCellRenderer renderer = table.getCellRenderer(row, column);
		if (renderer == null) {
			throw new IllegalArgumentException(
				"No renderer registered for row/col: " + row + '/' + column);
		}
		Component component = table.prepareRenderer(renderer, row, column);
		if (!(component instanceof JLabel)) {
			throw new IllegalArgumentException(
				"Do not know how to get text from a renderer " + "that is not a JLabel");
		}

		return ((JLabel) component).getText();
	});
}
 
Example 9
Source Project: ghidra   Source File: PreviewTable.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Must override this in order for horizontal scrolling to work. Scrolling
 * isn't automatically given when embedding a jtable in a scrollpanel; the
 * preferred width of the table must be explicitly set to the width of the
 * contents of the widest cell.
 * 
 * Note: We could override getPreferredSize() instead but we don't want to
 * change the default behavior for setting the preferred height, only the
 * width. So it's better to do it here.
 */
@Override
public boolean getScrollableTracksViewportWidth() {

	// Loop over all cells, getting the width of the largest cell.
	int width = 0;
	for (int row = 0; row < getRowCount(); row++) {
		TableCellRenderer rendererr = getCellRenderer(row, 0);
		Component comp = prepareRenderer(rendererr, row, 0);
		width = Math.max(comp.getPreferredSize().width, width);
	}

	// Now set the new preferred size using that max width, and the
	// existing preferred height.
	this.setPreferredSize(new Dimension(width, getPreferredSize().height));

	// Return true if the viewport has changed such that the table columns need to 
	// be resized.
	return getPreferredSize().width < getParent().getWidth();
}
 
Example 10
Source Project: ghidra   Source File: CompositeGhidraTableCellRenderer.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Component getTableCellRendererComponent(GTableCellRenderingData data) {

	Object value = data.getValue();
	JTable table = data.getTable();
	int row = data.getRowViewIndex();
	int column = data.getColumnViewIndex();
	boolean isSelected = data.isSelected();
	boolean hasFocus = data.hasFocus();

	Component rendererComponent = null;
	TableCellRenderer cellRenderer = getCellRenderer(table, row, column);
	if (cellRenderer != null) {
		rendererComponent =
			cellRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
				column);
	}
	else {
		// no defined renderer; use me
		rendererComponent =
			super.getTableCellRendererComponent(data);
	}

	return rendererComponent;
}
 
Example 11
Source Project: ghidra   Source File: CompositeGhidraTableCellRenderer.java    License: Apache License 2.0 6 votes vote down vote up
private TableCellRenderer getCellRenderer(JTable table, int row, int column) {

		// 
		// Step 1: See if we can use our custom rendering lookup        
		// 
		if (table instanceof GTable) {
			GTable gTable = (GTable) table;
			return gTable.getCellRendererOverride(row, column);
		}

		// 
		// Step 2: Locate normal JTable-style rendering   
		//
		TableColumn tableColumn = table.getColumnModel().getColumn(column);
		TableCellRenderer renderer = tableColumn.getCellRenderer();
		if (renderer == null) {
			renderer = table.getDefaultRenderer(table.getColumnClass(column));
		}
		return renderer;
	}
 
Example 12
Source Project: ghidra   Source File: EquateTablePluginTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testAddEquateReference() throws Exception {
	// add another equate reference; the reference count should update.

	Equate eq = equatesModel.getEquate(2);
	TableCellRenderer renderer = getRenderer(EquateTableModel.REFS_COL);
	String value = getRenderedValue(renderer, 2, EquateTableModel.REFS_COL);
	assertEquals("2", value);

	int transactionID = program.startTransaction("test");
	eq.addReference(getAddr(0x0100248c), 0);
	endTransaction(transactionID);

	value = getRenderedValue(renderer, 2, EquateTableModel.REFS_COL);
	assertEquals("3", value);

	undo();
	value = getRenderedValue(renderer, 2, EquateTableModel.REFS_COL);
	assertEquals("2", value);

	redo();
	value = getRenderedValue(renderer, 2, EquateTableModel.REFS_COL);
	assertEquals("3", value);
}
 
Example 13
Source Project: ghidra   Source File: EquateTablePluginTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoveEquateReference() throws Exception {
	Equate eq = equatesModel.getEquate(3);

	int transactionID = program.startTransaction("test");
	// remove an equate reference; the reference count should update.
	eq.removeReference(getAddr(0x0100621d), 1);
	endTransaction(transactionID);

	TableCellRenderer renderer = getRenderer(EquateTableModel.REFS_COL);
	String value = getRenderedValue(renderer, 3, EquateTableModel.REFS_COL);
	assertEquals("3", value);

	undo();
	value = getRenderedValue(renderer, 3, EquateTableModel.REFS_COL);
	assertEquals("4", value);

	redo();
	value = getRenderedValue(renderer, 3, EquateTableModel.REFS_COL);
	assertEquals("3", value);
}
 
Example 14
Source Project: jdk8u60   Source File: SwingUtilities2.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns true if the given point is outside the preferredSize of the
 * item at the given row of the table.  (Column must be 0).
 * Does not check the "Table.isFileList" property. That should be checked
 * before calling this method.
 * This is used to make WindowsL&F JFileChooser act like native dialogs.
 */
public static boolean pointOutsidePrefSize(JTable table, int row, int column, Point p) {
    if (table.convertColumnIndexToModel(column) != 0 || row == -1) {
        return true;
    }
    TableCellRenderer tcr = table.getCellRenderer(row, column);
    Object value = table.getValueAt(row, column);
    Component cell = tcr.getTableCellRendererComponent(table, value, false,
            false, row, column);
    Dimension itemSize = cell.getPreferredSize();
    Rectangle cellBounds = table.getCellRect(row, column, false);
    cellBounds.width = itemSize.width;
    cellBounds.height = itemSize.height;

    // See if coords are inside
    // ASSUME: mouse x,y will never be < cell's x,y
    assert (p.x >= cellBounds.x && p.y >= cellBounds.y);
    return p.x > cellBounds.x + cellBounds.width ||
            p.y > cellBounds.y + cellBounds.height;
}
 
Example 15
Source Project: openjdk-jdk8u   Source File: OldJTable.java    License: GNU General Public License v2.0 6 votes vote down vote up
public TableColumn addColumn(Object columnIdentifier, int width,
                             TableCellRenderer renderer,
                             TableCellEditor editor, List columnData) {
    checkDefaultTableModel();

    // Set up the model side first
    DefaultTableModel m = (DefaultTableModel)getModel();
    m.addColumn(columnIdentifier, columnData.toArray());

    // The column will have been added to the end, so the index of the
    // column in the model is the last element.
    TableColumn newColumn = new TableColumn(
            m.getColumnCount()-1, width, renderer, editor);
    super.addColumn(newColumn);
    return newColumn;
}
 
Example 16
Source Project: TencentKona-8   Source File: OldJTable.java    License: GNU General Public License v2.0 6 votes vote down vote up
public TableColumn addColumn(Object columnIdentifier, int width,
                             TableCellRenderer renderer,
                             TableCellEditor editor, List columnData) {
    checkDefaultTableModel();

    // Set up the model side first
    DefaultTableModel m = (DefaultTableModel)getModel();
    m.addColumn(columnIdentifier, columnData.toArray());

    // The column will have been added to the end, so the index of the
    // column in the model is the last element.
    TableColumn newColumn = new TableColumn(
            m.getColumnCount()-1, width, renderer, editor);
    super.addColumn(newColumn);
    return newColumn;
}
 
Example 17
Source Project: TencentKona-8   Source File: XTable.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public Component prepareRenderer(TableCellRenderer renderer,
                                 int row, int column) {
    Component comp = super.prepareRenderer(renderer, row, column);

    if (normalFont == null) {
        normalFont = comp.getFont();
        boldFont = normalFont.deriveFont(Font.BOLD);
    }

    if (column == VALUE_COLUMN && isAttributeViewable(row, VALUE_COLUMN)) {
        comp.setFont(boldFont);
    } else {
        comp.setFont(normalFont);
    }

    return comp;
}
 
Example 18
Source Project: jdk8u60   Source File: XTable.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public Component prepareRenderer(TableCellRenderer renderer,
                                 int row, int column) {
    Component comp = super.prepareRenderer(renderer, row, column);

    if (normalFont == null) {
        normalFont = comp.getFont();
        boldFont = normalFont.deriveFont(Font.BOLD);
    }

    if (column == VALUE_COLUMN && isAttributeViewable(row, VALUE_COLUMN)) {
        comp.setFont(boldFont);
    } else {
        comp.setFont(normalFont);
    }

    return comp;
}
 
Example 19
public static void resize(JTable table, int colViewIndex, boolean doFullScan) {
  int maxWidth = 0;

  // Get header width.
  TableColumn column = table.getColumnModel().getColumn(colViewIndex);
  TableCellRenderer headerRenderer = column.getHeaderRenderer();

  if (headerRenderer == null) {
    headerRenderer = table.getTableHeader().getDefaultRenderer();
  }

  Object headerValue = column.getHeaderValue();
  Component headerRendererComp =
      headerRenderer.getTableCellRendererComponent(table, headerValue, false, false, 0, colViewIndex);

  maxWidth = Math.max(maxWidth, headerRendererComp.getPreferredSize().width);


  // Get cell widths.
  if (doFullScan) {
    for (int row = 0; row < table.getRowCount(); ++row) {
      maxWidth = Math.max(maxWidth, getCellWidth(table, row, colViewIndex));
    }
  } else {
    maxWidth = Math.max(maxWidth, getCellWidth(table, 0, colViewIndex));
    maxWidth = Math.max(maxWidth, getCellWidth(table, table.getRowCount() / 2, colViewIndex));
    maxWidth = Math.max(maxWidth, getCellWidth(table, table.getRowCount() - 1, colViewIndex));
  }

  // For some reason, the calculation above gives a value that is 1 pixel too small.
  // Maybe that's because of the cell divider line?
  ++maxWidth;

  column.setPreferredWidth(maxWidth);
}
 
Example 20
Source Project: jdk8u60   Source File: bug7032791.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        UIManager.setLookAndFeel(new SynthLookAndFeel());

        Object value = "Test value";
        JTable table = new JTable(1, 1);
        TableCellRenderer renderer = table.getDefaultRenderer(Object.class);
        renderer.getTableCellRendererComponent(null, value, true, true, 0, 0);
        System.out.println("OK");
    }
 
Example 21
Source Project: openjdk-jdk8u   Source File: XTable.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method sets read write rows to be blue, and other rows to be their
 * default rendered colour.
 */
@Override
public TableCellRenderer getCellRenderer(int row, int column) {
    DefaultTableCellRenderer tcr =
        (DefaultTableCellRenderer) super.getCellRenderer(row,column);
    tcr.setToolTipText(getToolTip(row,column));
    if (defaultColor == null) {
        defaultColor = tcr.getForeground();
        editableColor = Color.blue;
        errorColor = Color.red;
        // this sometimes happens for some reason
        if (defaultColor == null) {
            return tcr;
        }
    }
    if (column != VALUE_COLUMN) {
        tcr.setForeground(defaultColor);
        return tcr;
    }
    if (isCellError(row,column)) {
        tcr.setForeground(errorColor);
    } else if (isCellEditable(row, column)) {
        tcr.setForeground(editableColor);
    } else {
        tcr.setForeground(defaultColor);
    }
    return tcr;
}
 
Example 22
Source Project: darklaf   Source File: DarkTableHeaderUI.java    License: MIT License 5 votes vote down vote up
protected Component getHeaderCellRenderer(final int columnIndex) {
    TableColumn aColumn = header.getColumnModel().getColumn(columnIndex);
    TableCellRenderer renderer = aColumn.getHeaderRenderer();
    if (renderer == null) {
        renderer = header.getDefaultRenderer();
    }

    boolean hasFocus = !header.isPaintingForPrint() && header.hasFocus();
    rendererDelegate.setDelegate(renderer);
    return rendererDelegate.getTableCellRendererComponent(header.getTable(),
                                                          aColumn.getHeaderValue(),
                                                          false, hasFocus,
                                                          -1, columnIndex);
}
 
Example 23
Source Project: openjdk-jdk8u   Source File: bug7032791.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        UIManager.setLookAndFeel(new SynthLookAndFeel());

        Object value = "Test value";
        JTable table = new JTable(1, 1);
        TableCellRenderer renderer = table.getDefaultRenderer(Object.class);
        renderer.getTableCellRendererComponent(null, value, true, true, 0, 0);
        System.out.println("OK");
    }
 
Example 24
Source Project: openAGV   Source File: OrdersTable.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public TableCellRenderer getCellRenderer(int row, int column) {
  TableModel tableModel = getModel();
  Object value = tableModel.getValueAt(row, column);
  TableCellRenderer renderer = getDefaultRenderer(value.getClass());

  return renderer;
}
 
Example 25
Source Project: JDKSourceCode1.8   Source File: SynthTableUI.java    License: MIT License 5 votes vote down vote up
private TableCellRenderer installRendererIfPossible(Class objectClass,
                                 TableCellRenderer renderer) {
    TableCellRenderer currentRenderer = table.getDefaultRenderer(
                             objectClass);
    if (currentRenderer instanceof UIResource) {
        table.setDefaultRenderer(objectClass, renderer);
    }
    return currentRenderer;
}
 
Example 26
@Override
public void uninstallUI(JComponent c) {
    for (int i = 0; i < header.getColumnModel().getColumnCount(); i++) {
        TableColumn aColumn = header.getColumnModel().getColumn(i);
        TableCellRenderer renderer = aColumn.getHeaderRenderer();
        if (renderer == null) {
            renderer = header.getDefaultRenderer();
        }
        Component rendComp = renderer.getTableCellRendererComponent(header.getTable(),
                aColumn.getHeaderValue(), false, false, -1, i);
        SwingUtilities.updateComponentTreeUI(rendComp);
    }
    super.uninstallUI(c);
}
 
Example 27
Source Project: FlatLaf   Source File: FlatTableHeaderUI.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void paint( Graphics g, JComponent c ) {
	// do not paint borders if JTableHeader.setDefaultRenderer() was used
	TableCellRenderer defaultRenderer = header.getDefaultRenderer();
	boolean paintBorders = isSystemDefaultRenderer( defaultRenderer );
	if( !paintBorders && header.getColumnModel().getColumnCount() > 0 ) {
		// check whether the renderer delegates to the system default renderer
		Component rendererComponent = defaultRenderer.getTableCellRendererComponent(
			header.getTable(), "", false, false, -1, 0 );
		paintBorders = isSystemDefaultRenderer( rendererComponent );
	}

	if( paintBorders )
		paintColumnBorders( g, c );

	// temporary use own default renderer if necessary
	FlatTableCellHeaderRenderer sortIconRenderer = null;
	if( sortIconPosition != SwingConstants.RIGHT ) {
		sortIconRenderer = new FlatTableCellHeaderRenderer( header.getDefaultRenderer() );
		header.setDefaultRenderer( sortIconRenderer );
	}

	// paint header
	super.paint( g, c );

	// restore default renderer
	if( sortIconRenderer != null ) {
		sortIconRenderer.reset();
		header.setDefaultRenderer( sortIconRenderer.delegate );
	}

	if( paintBorders )
		paintDraggedColumnBorders( g, c );
}
 
Example 28
Source Project: opt4j   Source File: Opt4JTasksPanel.java    License: MIT License 5 votes vote down vote up
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
	Component c = super.prepareRenderer(renderer, row, column);

	if (!(renderer instanceof ProgressRenderer)) {
		Task task = executionEnvironment.getTasks().get(row);
		if (task.getException() == null) {
			c.setForeground(Color.BLACK);
		} else {
			c.setForeground(Color.RED);
		}

		if (c instanceof JComponent) {
			JComponent jc = (JComponent) c;

			jc.setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 0));

			Object value = this.getValueAt(row, column);
			if (value != null) {
				char[] chars = value.toString().toCharArray();
				int length = jc.getFontMetrics(jc.getFont()).charsWidth(chars, 0, chars.length);
				if (this.getColumnModel().getColumn(column).getWidth() < length) {
					jc.setToolTipText(format.formatTooltip(value.toString()));
				} else {
					jc.setToolTipText(null);
				}
			}
		}
	}

	return c;
}
 
Example 29
Source Project: FlatLaf   Source File: FlatTableUI.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void installDefaults() {
	super.installDefaults();

	showHorizontalLines = UIManager.getBoolean( "Table.showHorizontalLines" );
	showVerticalLines = UIManager.getBoolean( "Table.showVerticalLines" );
	intercellSpacing = UIManager.getDimension( "Table.intercellSpacing" );

	selectionBackground = UIManager.getColor( "Table.selectionBackground" );
	selectionForeground = UIManager.getColor( "Table.selectionForeground" );
	selectionInactiveBackground = UIManager.getColor( "Table.selectionInactiveBackground" );
	selectionInactiveForeground = UIManager.getColor( "Table.selectionInactiveForeground" );

	toggleSelectionColors();

	int rowHeight = FlatUIUtils.getUIInt( "Table.rowHeight", 16 );
	if( rowHeight > 0 )
		LookAndFeel.installProperty( table, "rowHeight", UIScale.scale( rowHeight ) );

	if( !showHorizontalLines ) {
		oldShowHorizontalLines = table.getShowHorizontalLines();
		table.setShowHorizontalLines( false );
	}
	if( !showVerticalLines ) {
		oldShowVerticalLines = table.getShowVerticalLines();
		table.setShowVerticalLines( false );
	}

	if( intercellSpacing != null ) {
		oldIntercellSpacing = table.getIntercellSpacing();
		table.setIntercellSpacing( intercellSpacing );
	}

	// checkbox is non-opaque in FlatLaf and therefore would not paint selection
	// --> make checkbox renderer opaque (but opaque in Metal or Windows LaF)
	TableCellRenderer booleanRenderer = table.getDefaultRenderer( Boolean.class );
	if( booleanRenderer instanceof JCheckBox )
		((JCheckBox)booleanRenderer).setOpaque( true );
}
 
Example 30
private void paintCell(Graphics g, Rectangle cellRect, int columnIndex) {
  TableColumn aColumn = header.getColumnModel().getColumn(columnIndex);
  TableCellRenderer renderer = aColumn.getHeaderRenderer();
  if (renderer == null)
    renderer = header.getDefaultRenderer();
  Component component = renderer.getTableCellRendererComponent(header.getTable(),
      aColumn.getHeaderValue(), false, false, -1, columnIndex);
  rendererPane.add(component);
  rendererPane.paintComponent(g, component, header, cellRect.x, cellRect.y, cellRect.width,
      cellRect.height, true);
}