@Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { textLabel.setFont(table.getFont()); textLabel.setText(Objects.toString(value, "")); textLabel.setBorder(hasFocus ? focusCellHighlightBorder : noFocusBorder); FontMetrics fm = table.getFontMetrics(table.getFont()); Insets i = textLabel.getInsets(); int swidth = iconLabel.getPreferredSize().width + fm.stringWidth(textLabel.getText()) + i.left + i.right; int cwidth = table.getColumnModel().getColumn(column).getWidth(); dim.width = swidth > cwidth ? cwidth : swidth; if (isSelected) { textLabel.setOpaque(true); textLabel.setForeground(table.getSelectionForeground()); textLabel.setBackground(table.getSelectionBackground()); iconLabel.setIcon(sicon); } else { textLabel.setOpaque(false); textLabel.setForeground(table.getForeground()); textLabel.setBackground(table.getBackground()); iconLabel.setIcon(nicon); } return panel; }
/** * Returns the default table cell renderer. * * @param table the <code>JTable</code> * @param value the value to assign to the cell at <code>[row, column]</code> * @param isSelected true if cell is selected * @param hasFocus true if cell has focus * @param row the row of the cell to render * @param column the column of the cell to render * @return the default table cell renderer */ @Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (isSelected) { this.setBackground(table.getSelectionBackground()); this.setForeground(table.getSelectionForeground()); } else { this.setBackground(table.getBackground()); this.setForeground(table.getForeground()); } this.setEnabled(table.isEnabled()); this.setFont(table.getFont()); if (value instanceof CEMSystemActivity) { CEMSystemActivity activity = (CEMSystemActivity) value; int state = activity.getState(); this.setText(activity.getText()); if (state == CEMEntry.STATUS_ACCEPTED_INT || state == CEMEntry.STATUS_PENDING_INT) { Color backgroundColor = table.getBackground(); if (state == CEMEntry.STATUS_ACCEPTED_INT) { backgroundColor = this.colorAccepted; } else if (state == CEMEntry.STATUS_PENDING_INT) { backgroundColor = this.colorPending; } if (isSelected) { this.setBackground(backgroundColor.darker()); } else { this.setBackground(backgroundColor); } } } return (this); }
@Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (isSelected) { setForeground(table.getSelectionForeground()); setBackground(table.getSelectionBackground()); } else { setForeground(table.getForeground()); setBackground(table.getBackground()); } setFont(table.getFont()); setText(Objects.toString(value, "")); return this; }
@Override public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column) { System.out.println("getTableCellEditorComponent"); setFont(table.getFont()); setText(Objects.toString(value, "")); EventQueue.invokeLater( new Runnable() { @Override public void run() { setCaretPosition(getText().length()); requestFocusInWindow(); System.out.println("invokeLater: getTableCellEditorComponent"); } }); return scroll; }
private static void setColumnWidths(JTable table, MetricTableSpecification tableSpecification) { final TableModel model = table.getModel(); final TableColumnModel columnModel = table.getColumnModel(); final List<Integer> columnWidths = tableSpecification.getColumnWidths(); final List<String> columnOrder = tableSpecification.getColumnOrder(); if (columnWidths != null && !columnWidths.isEmpty()) { final int columnCount = model.getColumnCount(); for (int i = 0; i < columnCount; i++) { final String columnName = model.getColumnName(i); final int index = columnOrder.indexOf(columnName); if (index != -1) { final Integer width = columnWidths.get(index); final TableColumn column = columnModel.getColumn(i); column.setPreferredWidth(width.intValue()); } } } else { final Graphics graphics = table.getGraphics(); final Font font = table.getFont(); final FontMetrics fontMetrics = table.getFontMetrics(font); final int rowCount = model.getRowCount(); int maxFirstColumnWidth = 100; for (int i = 0; i < rowCount; i++) { final String name = (String) model.getValueAt(i, 0); if (name != null) { final Rectangle2D stringBounds = fontMetrics.getStringBounds(name, graphics); final double stringWidth = stringBounds.getWidth(); if (stringWidth > maxFirstColumnWidth) { maxFirstColumnWidth = (int) stringWidth; } } } final int allocatedFirstColumnWidth = Math.min(300, maxFirstColumnWidth + 5); final TableColumn column = columnModel.getColumn(0); column.setPreferredWidth(allocatedFirstColumnWidth); } }
public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Color foreground = null; Color background = null; Font font = null; TableModel model = table.getModel(); if (model instanceof AttributiveCellTableModel) { CellAttribute cellAtt = ((AttributiveCellTableModel) model).getCellAttribute(); if (cellAtt instanceof ColoredCell) { foreground = ((ColoredCell) cellAtt).getForeground(row, column); background = ((ColoredCell) cellAtt).getBackground(row, column); } if (cellAtt instanceof CellFont) { font = ((CellFont) cellAtt).getFont(row, column); } } if (isSelected) { setForeground((foreground != null) ? foreground : table.getSelectionForeground()); setBackground(table.getSelectionBackground()); } else { setForeground((foreground != null) ? foreground : table.getForeground()); setBackground((background != null) ? background : table.getBackground()); } setFont((font != null) ? font : table.getFont()); if (hasFocus) { setBorder(UIManager.getBorder("Table.focusCellHighlightBorder")); if (table.isCellEditable(row, column)) { setForeground( (foreground != null) ? foreground : UIManager.getColor("Table.focusCellForeground")); setBackground(UIManager.getColor("Table.focusCellBackground")); } } else { setBorder(noFocusBorder); } setValue(value); return this; }