use of javax.swing.table.TableCellRenderer in project antlrworks by antlr.
the class XJTableView method resizeColumnToFitContent.
public static void resizeColumnToFitContent(JTable table, int columnIndex, int margin) {
TableColumn column = table.getColumnModel().getColumn(columnIndex);
TableCellRenderer renderer = column.getHeaderRenderer();
if (renderer == null) {
renderer = table.getTableHeader().getDefaultRenderer();
}
Component c = renderer.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, 0, 0);
int maxWidth = c.getPreferredSize().width;
for (int row = 0; row < table.getRowCount(); row++) {
renderer = table.getCellRenderer(row, columnIndex);
c = renderer.getTableCellRendererComponent(table, table.getValueAt(row, columnIndex), false, false, row, columnIndex);
maxWidth = Math.max(maxWidth, c.getPreferredSize().width);
}
column.setPreferredWidth(maxWidth + margin);
}
use of javax.swing.table.TableCellRenderer in project druid by alibaba.
the class GroupableTableHeaderUI method paintCell.
private void paintCell(Graphics g, Rectangle cellRect, ColumnGroup cGroup) {
TableCellRenderer renderer = cGroup.getHeaderRenderer();
//
if (renderer == null) {
renderer = new DefaultTableCellRenderer() {
private static final long serialVersionUID = 1L;
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JTableHeader header = table.getTableHeader();
if (header != null) {
setForeground(header.getForeground());
setBackground(header.getBackground());
setFont(header.getFont());
}
setHorizontalAlignment(JLabel.CENTER);
setText((value == null) ? "" : value.toString());
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
return this;
}
};
}
String headerValue = cGroup.getHeaderValue().toString();
Component component = renderer.getTableCellRendererComponent(header.getTable(), headerValue, false, false, -1, -1);
rendererPane.add(component);
rendererPane.paintComponent(g, component, header, cellRect.x, cellRect.y, cellRect.width, cellRect.height, true);
}
use of javax.swing.table.TableCellRenderer in project druid by alibaba.
the class GroupableTableHeaderUI method paintCell.
private void paintCell(Graphics g, Rectangle cellRect, int columnIndex) {
TableColumn aColumn = header.getColumnModel().getColumn(columnIndex);
TableCellRenderer renderer = aColumn.getHeaderRenderer();
//
if (renderer == null) {
renderer = new DefaultTableCellRenderer() {
private static final long serialVersionUID = 1L;
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JTableHeader header = table.getTableHeader();
if (header != null) {
setForeground(header.getForeground());
setBackground(header.getBackground());
setFont(header.getFont());
}
setHorizontalAlignment(JLabel.CENTER);
setText((value == null) ? "" : value.toString());
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
return this;
}
};
}
String headerValue = aColumn.getHeaderValue().toString();
Component component = renderer.getTableCellRendererComponent(header.getTable(), headerValue, false, false, -1, columnIndex);
rendererPane.add(component);
rendererPane.paintComponent(g, component, header, cellRect.x, cellRect.y, cellRect.width, cellRect.height, true);
}
use of javax.swing.table.TableCellRenderer in project gitblit by gitblit.
the class Utils method newTable.
public static JTable newTable(TableModel model, String datePattern, final RowRenderer rowRenderer) {
JTable table;
if (rowRenderer == null) {
table = new JTable(model);
} else {
table = new JTable(model) {
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
boolean isSelected = isCellSelected(row, column);
rowRenderer.prepareRow(c, isSelected, row, column);
return c;
}
};
}
table.setRowHeight(table.getFont().getSize() + 8);
table.setCellSelectionEnabled(false);
table.setRowSelectionAllowed(true);
table.getTableHeader().setReorderingAllowed(false);
table.setGridColor(new Color(0xd9d9d9));
table.setBackground(Color.white);
table.setDefaultRenderer(Date.class, new DateCellRenderer(datePattern, Color.orange.darker()));
return table;
}
use of javax.swing.table.TableCellRenderer in project gitblit by gitblit.
the class Utils method packColumn.
// Sets the preferred width of the visible column specified by vColIndex.
// The column will be just wide enough to show the column head and the
// widest cell in the column. margin pixels are added to the left and right
// (resulting in an additional width of 2*margin pixels).
private static void packColumn(JTable table, int vColIndex, int margin) {
DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
TableColumn col = colModel.getColumn(vColIndex);
int width = 0;
// Get width of column header
TableCellRenderer renderer = col.getHeaderRenderer();
if (renderer == null) {
renderer = table.getTableHeader().getDefaultRenderer();
}
Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
width = comp.getPreferredSize().width;
// Get maximum width of column data
for (int r = 0; r < table.getRowCount(); r++) {
renderer = table.getCellRenderer(r, vColIndex);
comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false, r, vColIndex);
width = Math.max(width, comp.getPreferredSize().width);
}
// Add margin
width += 2 * margin;
// Set the width
col.setPreferredWidth(width);
}
Aggregations