use of javax.swing.table.DefaultTableCellRenderer in project adempiere by adempiere.
the class CTable method packColumn.
// autoSize
public void packColumn(TableColumn column) {
int width = 0;
// Header
TableCellRenderer renderer = column.getHeaderRenderer();
if (renderer == null)
renderer = new DefaultTableCellRenderer();
Component comp = null;
if (renderer != null)
comp = renderer.getTableCellRendererComponent(this, column.getHeaderValue(), false, false, 0, 0);
//
if (comp != null) {
width = comp.getPreferredSize().width;
width = Math.max(width, comp.getWidth());
// Cells
int col = column.getModelIndex();
int maxRow = Math.min(20, getRowCount());
try {
for (int row = 0; row < maxRow; row++) {
renderer = getCellRenderer(row, col);
comp = renderer.getTableCellRendererComponent(this, getValueAt(row, col), false, false, row, col);
if (comp != null) {
int rowWidth = comp.getPreferredSize().width;
width = Math.max(width, rowWidth);
}
}
} catch (Exception e) {
log.log(Level.SEVERE, column.getIdentifier().toString(), e);
}
// Width not greater than 250
width = Math.min(MAXSIZE, width + SLACK);
}
//
column.setPreferredWidth(width);
}
use of javax.swing.table.DefaultTableCellRenderer in project adempiere by adempiere.
the class CTable method autoSize.
// stopEditor
/**************************************************************************
* Size Columns.
* @param useColumnIdentifier if false uses plain content -
* otherwise uses Column Identifier to indicate displayed columns
*/
public void autoSize(boolean useColumnIdentifier) {
TableModel model = this.getModel();
int size = model.getColumnCount();
// for all columns
for (int c = 0; c < size; c++) {
TableColumn column = getColumnModel().getColumn(c);
// Not displayed columns
if (useColumnIdentifier && (column.getIdentifier() == null || column.getMaxWidth() == 0 || column.getIdentifier().toString().length() == 0))
continue;
int width = 0;
// Header
TableCellRenderer renderer = column.getHeaderRenderer();
if (renderer == null)
renderer = new DefaultTableCellRenderer();
Component comp = null;
if (renderer != null)
comp = renderer.getTableCellRendererComponent(this, column.getHeaderValue(), false, false, 0, 0);
//
if (comp != null) {
width = comp.getPreferredSize().width;
width = Math.max(width, comp.getWidth());
// Cells
int col = column.getModelIndex();
int maxRow = Math.min(20, getRowCount());
try {
for (int row = 0; row < maxRow; row++) {
renderer = getCellRenderer(row, col);
comp = renderer.getTableCellRendererComponent(this, getValueAt(row, col), false, false, row, col);
if (comp != null) {
int rowWidth = comp.getPreferredSize().width;
width = Math.max(width, rowWidth);
}
}
} catch (Exception e) {
log.log(Level.SEVERE, column.getIdentifier().toString(), e);
}
// Width not greater than 250
width = Math.min(MAXSIZE, width + SLACK);
}
//
column.setPreferredWidth(width);
}
// for all columns
}
use of javax.swing.table.DefaultTableCellRenderer in project adempiere by adempiere.
the class VBrowserTable method autoSize.
/**
* Size Columns.
* Uses Mimimun Column Size
*/
public void autoSize() {
if (!autoResize)
return;
long start = System.currentTimeMillis();
//
// making sure it fits in a column
final int SLACK = 8;
// max size of a column
final int MAXSIZE = 300;
//
TableModel model = this.getModel();
int size = model.getColumnCount();
// for all columns
for (int col = 0; col < size; col++) {
// Column & minimum width
TableColumn tc = this.getColumnModel().getColumn(col);
int width = 0;
if (m_minWidth.size() > col)
width = ((Integer) m_minWidth.get(col)).intValue();
// log.config( "Column=" + col + " " + column.getHeaderValue());
// Header
TableCellRenderer renderer = tc.getHeaderRenderer();
if (renderer == null)
renderer = new DefaultTableCellRenderer();
Component comp = renderer.getTableCellRendererComponent(this, tc.getHeaderValue(), false, false, 0, 0);
// log.fine( "Hdr - preferred=" + comp.getPreferredSize().width + ", width=" + comp.getWidth());
width = Math.max(width, comp.getPreferredSize().width + SLACK);
// Cells
// first 30 rows
int maxRow = Math.min(30, getRowCount());
for (int row = 0; row < maxRow; row++) {
renderer = getCellRenderer(row, col);
comp = renderer.getTableCellRendererComponent(this, getValueAt(row, col), false, false, row, col);
if (comp != null) {
int rowWidth = comp.getPreferredSize().width + SLACK;
width = Math.max(width, rowWidth);
}
}
// Width not greater ..
width = Math.min(MAXSIZE, width);
tc.setPreferredWidth(width);
// log.fine( "width=" + width);
}
// for all columns
log.finer("Cols=" + size + " - " + (System.currentTimeMillis() - start) + "ms");
}
use of javax.swing.table.DefaultTableCellRenderer in project jmeter by apache.
the class HeaderAsPropertyRendererWrapper method getTableCellRendererComponent.
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (delegate instanceof DefaultTableCellRenderer) {
DefaultTableCellRenderer tr = (DefaultTableCellRenderer) delegate;
if (table != null) {
JTableHeader header = table.getTableHeader();
if (header != null) {
tr.setForeground(header.getForeground());
tr.setBackground(header.getBackground());
tr.setFont(header.getFont());
}
}
tr.setBorder(UIManager.getBorder("TableHeader.cellBorder"));
tr.setHorizontalAlignment(SwingConstants.CENTER);
}
return delegate.getTableCellRendererComponent(table, HeaderAsPropertyRenderer.getText(value, row, column, columnsMsgParameters), isSelected, hasFocus, row, column);
}
use of javax.swing.table.DefaultTableCellRenderer in project intellij-community by JetBrains.
the class ShowUIDefaultsAction method actionPerformed.
@Override
public void actionPerformed(AnActionEvent e) {
final UIDefaults defaults = UIManager.getDefaults();
Enumeration keys = defaults.keys();
final Object[][] data = new Object[defaults.size()][2];
int i = 0;
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
data[i][0] = key;
data[i][1] = defaults.get(key);
i++;
}
Arrays.sort(data, (o1, o2) -> StringUtil.naturalCompare(o1[0].toString(), o2[0].toString()));
final Project project = getEventProject(e);
new DialogWrapper(project) {
{
setTitle("Edit LaF Defaults");
setModal(false);
init();
}
public JBTable myTable;
@Nullable
@Override
public JComponent getPreferredFocusedComponent() {
return myTable;
}
@Nullable
@Override
protected String getDimensionServiceKey() {
return project == null ? null : "UI.Defaults.Dialog";
}
@Override
protected JComponent createCenterPanel() {
final JBTable table = new JBTable(new DefaultTableModel(data, new Object[] { "Name", "Value" }) {
@Override
public boolean isCellEditable(int row, int column) {
return column == 1 && getValueAt(row, column) instanceof Color;
}
}) {
@Override
public boolean editCellAt(int row, int column, EventObject e) {
if (isCellEditable(row, column) && e instanceof MouseEvent) {
final Object color = getValueAt(row, column);
final Color newColor = ColorPicker.showDialog(this, "Choose Color", (Color) color, true, null, true);
if (newColor != null) {
final ColorUIResource colorUIResource = new ColorUIResource(newColor);
final Object key = getValueAt(row, 0);
UIManager.put(key, colorUIResource);
setValueAt(colorUIResource, row, column);
}
}
return false;
}
};
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
final JPanel panel = new JPanel(new BorderLayout());
final JLabel label = new JLabel(value == null ? "" : value.toString());
panel.add(label, BorderLayout.CENTER);
if (value instanceof Color) {
final Color c = (Color) value;
label.setText(String.format("[r=%d,g=%d,b=%d] hex=0x%s", c.getRed(), c.getGreen(), c.getBlue(), ColorUtil.toHex(c)));
label.setForeground(ColorUtil.isDark(c) ? Color.white : Color.black);
panel.setBackground(c);
return panel;
} else if (value instanceof Icon) {
try {
final Icon icon = new IconWrap((Icon) value);
if (icon.getIconHeight() <= 20) {
label.setIcon(icon);
}
label.setText(String.format("(%dx%d) %s)", icon.getIconWidth(), icon.getIconHeight(), label.getText()));
} catch (Throwable e1) {
//
}
return panel;
} else if (value instanceof Border) {
try {
final Insets i = ((Border) value).getBorderInsets(null);
label.setText(String.format("[%d, %d, %d, %d] %s", i.top, i.left, i.bottom, i.right, label.getText()));
return panel;
} catch (Exception ignore) {
}
}
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
});
final JBScrollPane pane = new JBScrollPane(table);
new TableSpeedSearch(table, (o, cell) -> cell.column == 1 ? null : String.valueOf(o));
table.setShowGrid(false);
final JPanel panel = new JPanel(new BorderLayout());
panel.add(pane, BorderLayout.CENTER);
myTable = table;
TableUtil.ensureSelectionExists(myTable);
return panel;
}
}.show();
}
Aggregations