public static int getMaxColumnWidth(JTable table, int column) { // Set first to column header width as a minimum Font font = table.getFont(); FontMetrics metrics = table.getGraphics().getFontMetrics(font); String header = table.getColumnName(column) + " "; // Whitespace buffer as a crude way (hack) // to account for the rendering context Test.output("Column name: " + header); // Convert from string length in characters to pixels int widest = metrics.stringWidth(header) + (2 * table.getColumnModel().getColumnMargin()); Test.output("Starting widest value: " + header + " (" + widest + " pixels)"); // Now go through each row to see if there is a longer value in the column int rows = table.getRowCount(); Test.output("Row count: " + rows); for (int index = 0; index < rows; index++) { String cellValue = table.getValueAt(index, column).toString(); int cellWidth = metrics.stringWidth(cellValue) + (2 * table.getColumnModel().getColumnMargin()); if (cellWidth > widest) { widest = cellWidth; Test.output("New widest value: " + widest + " pixels"); } } return widest; }
private int strWidth(String str) { return table.getFontMetrics(table.getFont()) .charsWidth(str.toCharArray(), 0, str.length()); }
table.setFont(table.getFont().deriveFont(Font.BOLD)); add(new JScrollPane(table));
private int calculateColumnWidth(String txt) { Font font = getTargetList().getFont(); FontMetrics fontMetrics = getTargetList().getFontMetrics(font); int width = fontMetrics.stringWidth(txt); return width; }
public TRTablePane(TableModelListener listener, StringIntPair[] tags) { setLayout(new BorderLayout()); j_table = new JTable(); j_table.setModel(new DefaultTableModel(new Object[][]{{0,"","",0,""}}, t_columns)); j_table.getModel().addTableModelListener(listener); TableColumnModel columnModel = j_table.getColumnModel(); for (StringIntPair tag : tags) setColumnEditor(columnModel, tag.s, tag.i); add(j_table.getTableHeader(), BorderLayout.NORTH); add(j_table, BorderLayout.CENTER); t_height = j_table.getFontMetrics(j_table.getFont()).getHeight(); }
protected void setValue(JTable table, Object value, int row, int column) { if (table != null) { setFont(table.getFont()); } label.setText((value == null) ? "" : value.toString()); //NOI18N } }
public TRTablePane(TableModelListener listener, StringIntPair[] tags) { setLayout(new BorderLayout()); j_table = new JTable(); j_table.setModel(new DefaultTableModel(new Object[][]{{0,"","",0,""}}, t_columns)); j_table.getModel().addTableModelListener(listener); TableColumnModel columnModel = j_table.getColumnModel(); for (StringIntPair tag : tags) setColumnEditor(columnModel, tag.s, tag.i); add(j_table.getTableHeader(), BorderLayout.NORTH); add(j_table, BorderLayout.CENTER); t_height = j_table.getFontMetrics(j_table.getFont()).getHeight(); }
@Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { JButton cmb = (JButton) value; cmb.setFont(table.getFont()); if (isSelected){ } return cmb; } }
protected void setValue(JTable table, Object value, int row, int column) { if (table != null) { setFont(table.getFont()); } if (value == null) { label.setText(""); // NOI18N } else { int index = ((Integer) value).intValue(); label.setText(viewManager.getThreadName(index)); } } }
public void componentResized(ComponentEvent e) { // The new size of the table final double w = e.getComponent().getSize().getWidth(); final double h = e.getComponent().getSize().getHeight(); // Set the size of the font as a fraction of the width or the height, whichever is smallest final float sw = (float) Math.floor(w / 16); final float sh = (float) Math.floor(h / 8); dayTable.setFont(dayTable.getFont().deriveFont(Math.min(sw, sh))); // Set the row height as a fraction of the height final int r = (int) Math.floor(h / 6); dayTable.setRowHeight(r); }
public void installDefaults() { super.installDefaults(); // Setup the rowheight. The font may change if UI switches FontMetrics fm = JTattooUtilities.getFontMetrics(table, null, table.getFont()); table.setRowHeight(fm.getHeight() + (fm.getHeight() / 4)); }
private JTable createTable(ValueTable valueTable) { JTable table = new JTable(new ValueTableModel(valueTable)); table.setDefaultRenderer(Value.class, new ValueRenderer()); table.setDefaultRenderer(Integer.class, new NumberRenderer()); final Font font = table.getFont(); table.getColumnModel().getColumn(0).setMaxWidth(font.getSize() * 4); table.setRowHeight(font.getSize() * 6 / 5); return table; }
@Override public void paint(Graphics g) { super.paint(g); setRowHeight(g.getFontMetrics(existingTreesTable.getFont()).getHeight()); } };
private static int getTableBaseline(JTable table, int height) { if (TABLE_LABEL == null) { TABLE_LABEL = new JLabel(""); TABLE_LABEL.setBorder(new EmptyBorder(1, 1, 1, 1)); } JLabel label = TABLE_LABEL; label.setFont(table.getFont()); int rowMargin = table.getRowMargin(); int baseline = getLabelBaseline(label, table.getRowHeight() - rowMargin); return baseline += rowMargin / 2; }
protected void setValue(JTable table, Object value, int row, int column) { if (table != null) { setFont(table.getFont()); } label.setText((value == null) ? "" : value.toString()); //NOI18N }
private int getTableBaseline(JTable table, int height) { if (TABLE_LABEL == null) { TABLE_LABEL = new JLabel(""); TABLE_LABEL.setBorder(new EmptyBorder(1, 1, 1, 1)); } JLabel label = TABLE_LABEL; label.setFont(table.getFont()); int rowMargin = table.getRowMargin(); int baseline = getLabelBaseline(label, table.getRowHeight() - rowMargin); return baseline += rowMargin / 2; }
public TextRenderer(String countMessage) { setFont(table.getFont()); emptyIcon = new EmptyIcon(); elementsMessage = countMessage; }
@Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { TableModel tableModel = table.getModel(); if (tableModel instanceof MultiTagFieldModel) { if (((MultiTagFieldModel) tableModel).getTrackInfoItems().get(row).isMultiple()) { value = ""; } } JTextField c = (JTextField) super.getTableCellEditorComponent(table, value, isSelected, row, column); c.setBorder(BorderFactory.createEmptyBorder()); c.setFont(table.getFont()); c.selectAll(); return c; }
@Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { setText((String) value); setBackground(isSelected && !hasFocus ? table.getSelectionBackground() : table.getBackground()); setForeground(isSelected && !hasFocus ? table.getSelectionForeground() : table.getForeground()); setFont(table.getFont()); return this; } }