Java Code Examples for javax.swing.JTable#getDefaultRenderer()

The following examples show how to use javax.swing.JTable#getDefaultRenderer() . 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
Source File: CompositeGhidraTableCellRenderer.java    From ghidra with 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 2
Source File: PriceEditComponent.java    From ramus with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Component getTableCellRendererComponent(JTable table,
                                               Object value, boolean isSelected, boolean hasFocus, int row,
                                               int column) {
    Component component = super.getTableCellRendererComponent(table,
            value, isSelected, hasFocus, row, column);
    if (value != null) {
        if (component instanceof JLabel)
            ((JLabel) component)
                    .setText(DateAttributePlugin.DATE_INSTANCE
                            .format(value));
        else {
            TableCellRenderer renderer = table
                    .getDefaultRenderer(Date.class);
            return renderer.getTableCellRendererComponent(table, value,
                    isSelected, hasFocus, row, column);
        }
    }
    return component;
}
 
Example 3
Source File: DateAttributePlugin.java    From ramus with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Component getTableCellRendererComponent(JTable table,
                                               Object value, boolean isSelected, boolean hasFocus, int row,
                                               int column) {
    Component component = super.getTableCellRendererComponent(table,
            value, isSelected, hasFocus, row, column);
    if (value != null) {
        if (component instanceof JLabel)
            ((JLabel) component).setText(DATE_INSTANCE.format(value));
        else {
            TableCellRenderer renderer = table
                    .getDefaultRenderer(Date.class);
            return renderer.getTableCellRendererComponent(table, value,
                    isSelected, hasFocus, row, column);
        }
    }
    return component;
}
 
Example 4
Source File: PropertyColorCellRender.java    From jdal with Apache License 2.0 6 votes vote down vote up
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
		boolean isSelected, boolean hasFocus, int row, int column) {
	
	TableCellRenderer renderer = table.getDefaultRenderer(table.getColumnClass(column));
	Component c = renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus,
			row, column);

	if (!isSelected) {
		ListTableModel tableModel = (ListTableModel) table.getModel();
		Object model = tableModel.getList().get(row);
		Color color = (Color) BeanUtils.getProperty(model, colorPropertyName);
		
		if (color != null)
			c.setBackground(color);
	}
	
	return c;
}
 
Example 5
Source File: DisablingCellRenderer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Applies DisablingCellRenderer to all cell renderers in the table.
 * @param t the table
 * @param disabled if the table content should be disabled
 */
public static void apply(JTable t, Color background) {
    int nc = t.getColumnModel().getColumnCount();
    for (int i = 0; i < nc; i++) {
        TableCellRenderer columnRenderer = t.getColumnModel().getColumn(i).getCellRenderer();
        if (columnRenderer == null) columnRenderer = t.getDefaultRenderer(t.getColumnClass(i));
        t.getColumnModel().getColumn(i).setCellRenderer(
            new DisablingCellRenderer(columnRenderer, t, background));
    }
}
 
Example 6
Source File: ClassNameList.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    TableCellRenderer def = table.getDefaultRenderer(table.getColumnClass(column));
    if (!table.isEnabled()) {
        isSelected = hasFocus = false;
    }
    JComponent c = (JComponent)def.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    c.setEnabled(table.isEnabled());
    
    return c;
}
 
Example 7
Source File: DetailsTableCellRenderer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public Component getTableCellRendererComponent(JTable table, Object value,
                                               boolean isSelected, boolean hasFocus,
                                               int row, int column) {

    if (impl == null) impl = table.getDefaultRenderer(table.getColumnClass(column));
    
    value = formatValue(table, value, isSelected, hasFocus, row, column);
    Component c = impl.getTableCellRendererComponent(table, value, isSelected,
                                                     hasFocus, row, column);
    updateRenderer(c, table, value, isSelected, hasFocus, row, column);

    return c;
}
 
Example 8
Source File: AttrTable.java    From Logisim with GNU General Public License v3.0 5 votes vote down vote up
public AttrTable(Window parent) {
	super(new BorderLayout());
	this.parent = parent;

	titleEnabled = true;
	title = new TitleLabel();
	title.setHorizontalAlignment(SwingConstants.CENTER);
	title.setVerticalAlignment(SwingConstants.CENTER);
	tableModel = new TableModelAdapter(parent, NULL_ATTR_MODEL);
	table = new JTable(tableModel);
	table.setDefaultEditor(Object.class, editor);
	table.setTableHeader(null);
	table.setRowHeight(20);

	Font baseFont = title.getFont();
	int titleSize = Math.round(baseFont.getSize() * 1.2f);
	Font titleFont = baseFont.deriveFont((float) titleSize).deriveFont(Font.BOLD);
	title.setFont(titleFont);
	Color bgColor = new Color(240, 240, 240);
	setBackground(bgColor);
	table.setBackground(bgColor);
	Object renderer = table.getDefaultRenderer(String.class);
	if (renderer instanceof JComponent) {
		((JComponent) renderer).setBackground(Color.WHITE);
	}

	JScrollPane tableScroll = new JScrollPane(table);

	this.add(title, BorderLayout.PAGE_START);
	this.add(tableScroll, BorderLayout.CENTER);
	LocaleManager.addLocaleListener(this);
	localeChanged();
}
 
Example 9
Source File: DetailsTableCellRenderer.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public Component getTableCellRendererComponent(JTable table, Object value,
                                               boolean isSelected, boolean hasFocus,
                                               int row, int column) {

    if (impl == null) impl = table.getDefaultRenderer(table.getColumnClass(column));
    
    value = formatValue(table, value, isSelected, hasFocus, row, column);
    Component c = impl.getTableCellRendererComponent(table, value, isSelected,
                                                     hasFocus, row, column);
    updateRenderer(c, table, value, isSelected, hasFocus, row, column);

    return c;
}
 
Example 10
Source File: ValuesCustomizer.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public Component getTableCellRendererComponent(JTable table, Object value,
                                               boolean isSelected, boolean hasFocus,
                                               int row, int column) {

    if (impl == null) impl = table.getDefaultRenderer(table.getColumnClass(column));

    value = formatValue(table, value, isSelected, false, row, column);
    Component c = impl.getTableCellRendererComponent(table, value, isSelected,
                                                     false, row, column);
    updateRenderer(c, table, value, isSelected, false, row, column);

    return c;
}
 
Example 11
Source File: DetailsTableCellRenderer.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public Component getTableCellRendererComponent(JTable table, Object value,
                                               boolean isSelected, boolean hasFocus,
                                               int row, int column) {

    if (impl == null) impl = table.getDefaultRenderer(table.getColumnClass(column));
    
    value = formatValue(table, value, isSelected, hasFocus, row, column);
    Component c = impl.getTableCellRendererComponent(table, value, isSelected,
                                                     hasFocus, row, column);
    updateRenderer(c, table, value, isSelected, hasFocus, row, column);

    return c;
}
 
Example 12
Source File: AnnotationFormatTableCellRenderer.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
		int row, int column) {
	
	Class<?> clazz = table.getColumnClass(column);
	if (Number.class.isAssignableFrom(clazz)) {
		TableModel tableModel = table.getModel();
		if (tableModel instanceof ListTableModel) {
			ListTableModel listTableModel = (ListTableModel) tableModel;
			Class<?> modelClass = ((ListTableModel) tableModel).getModelClass();
			String propertyName = listTableModel.getPropertyName(column);
			Printer<Object> printer = (Printer<Object>) FormatUtils.getPrinter(modelClass, propertyName);
			if (printer != null) {
				value = printer.print(value, Locale.getDefault());
				JLabel label  = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
				label.setHorizontalAlignment(JLabel.RIGHT);
				return label;
			}
		}
	}
	TableCellRenderer renderer = table.getDefaultRenderer(clazz);
	
	return renderer != null ? 
			renderer.getTableCellRendererComponent(
					table, value, isSelected, hasFocus, row, column) :
				null;
}
 
Example 13
Source File: PeakSummaryComponent.java    From mzmine2 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param peaksInfoList
 * @return
 */
private Dimension calculatedTableDimension(JTable peaksInfoList) {

  int numRows = peaksInfoList.getRowCount();
  int numCols = peaksInfoList.getColumnCount();
  int maxWidth = 0, compWidth, totalWidth = 0, totalHeight = 0;
  TableCellRenderer renderer = peaksInfoList.getDefaultRenderer(Object.class);
  TableCellRenderer headerRenderer = peaksInfoList.getTableHeader().getDefaultRenderer();
  TableModel model = peaksInfoList.getModel();
  Component comp;
  TableColumn column;

  for (int c = 0; c < numCols; c++) {
    for (int r = 0; r < numRows; r++) {

      if (r == 0) {
        comp = headerRenderer.getTableCellRendererComponent(peaksInfoList, model.getColumnName(c),
            false, false, r, c);
        compWidth = comp.getPreferredSize().width + 10;
        maxWidth = Math.max(maxWidth, compWidth);

      }

      comp = renderer.getTableCellRendererComponent(peaksInfoList, model.getValueAt(r, c), false,
          false, r, c);

      compWidth = comp.getPreferredSize().width + 10;
      maxWidth = Math.max(maxWidth, compWidth);

      if (c == 0) {
        totalHeight += comp.getPreferredSize().height;
      }

      // Consider max 10 rows
      if (r == 8) {
        break;
      }

    }
    totalWidth += maxWidth;
    column = peaksInfoList.getColumnModel().getColumn(c);
    column.setPreferredWidth(maxWidth);
    maxWidth = 0;
  }

  // add 30x10 px for a scrollbar
  totalWidth += 30;
  totalHeight += 10;

  comp = headerRenderer.getTableCellRendererComponent(peaksInfoList, model.getColumnName(0),
      false, false, 0, 0);
  totalHeight += comp.getPreferredSize().height;

  return new Dimension(totalWidth, totalHeight);

}
 
Example 14
Source File: MetadataOutlineCellRenderer.java    From snap-desktop with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Highlight the non editable cells making the foreground lighter.
 */

@Override
@SuppressWarnings("unchecked")
public Component getTableCellRendererComponent(final JTable table,
                                               final Object value,
                                               final boolean isSelected,
                                               final boolean hasFocus,
                                               final int row,
                                               final int column) {
    Component cell = null;
    Object valueToDisplay = value;
    if (value instanceof Property) {
        try {
            valueToDisplay = ((Property) value).getValue();
        } catch (IllegalAccessException | InvocationTargetException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
    if (valueToDisplay != null) {
        TableCellRenderer renderer = table.getDefaultRenderer(valueToDisplay.getClass());
        if (renderer != null) {
            cell = renderer.getTableCellRendererComponent(table, valueToDisplay, isSelected,
                                                          hasFocus, row, column);
        }
    } else {
        cell = super.getTableCellRendererComponent(table, valueToDisplay, isSelected, hasFocus, row, column);
    }
    if (cell != null) {
        if (cell instanceof HtmlRenderer.Renderer) {
            ((HtmlRenderer.Renderer) cell).setIndent(5);
        } else if (cell instanceof DefaultTableCellRenderer.UIResource) {
            ((UIResource) cell).setHorizontalAlignment(JLabel.LEFT);
        }
        Color foregroundColor = table.getForeground();
        cell.setForeground(foregroundColor);
        cell.setBackground(row % 2 == 0 ? Color.WHITE : VERY_LIGHT_GRAY);
        if (isSelected) {
            cell.setBackground(table.getSelectionBackground());
        }
    }
    return cell;
}