List of usage examples for javax.swing.table TableColumn setPreferredWidth
@BeanProperty(description = "The preferred width of the column.") public void setPreferredWidth(int preferredWidth)
preferredWidth
. From source file:Main.java
public static void main(String[] argv) { int rows = 3; int cols = 3; JTable table = new JTable(rows, cols); // //from w w w . j av a2 s. co m table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); TableColumn col = table.getColumnModel().getColumn(0); int width = 100; col.setPreferredWidth(width); }
From source file:Main.java
public static void main(String[] argv) throws Exception { int rows = 3; int cols = 3; JTable table = new JTable(rows, cols); TableColumn col = table.getColumnModel().getColumn(0); col.setMinWidth(100);//www . ja v a 2 s .c om col.setMaxWidth(100); col.setPreferredWidth(100); }
From source file:ResizeColumnWidth.java
public static void main(String args[]) { final Object rowData[][] = { { "1", "one", "I" }, { "2", "two", "II" }, { "3", "three", "III" } }; final String columnNames[] = { "#", "English", "Roman" }; final JTable table = new JTable(rowData, columnNames); JScrollPane scrollPane = new JScrollPane(table); JFrame frame = new JFrame("Resizing Table"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(scrollPane, BorderLayout.CENTER); TableColumn column = null; for (int i = 0; i < 3; i++) { column = table.getColumnModel().getColumn(i); if (i == 2) { column.setPreferredWidth(100); //sport column is bigger } else {//w w w. j av a2 s.c o m column.setPreferredWidth(50); } } frame.setSize(300, 150); frame.setVisible(true); }
From source file:Main.java
/** * Resizes the columns to match content while keeping the table the same * size. This means that the last column may be larger than the content. *///from www .j a v a2 s . c o m public static void resizeTableColumns(JTable table) { table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); int totalWidth = 0; for (int i = 0; i < table.getColumnCount(); i++) { DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); TableColumn col = colModel.getColumn(i); if (i == table.getColumnCount() - 1) { col.setPreferredWidth(table.getWidth() - totalWidth); table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); return; } int width = 0; TableCellRenderer renderer = col.getHeaderRenderer(); if (renderer == null) { renderer = table.getTableHeader().getDefaultRenderer(); } Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, -1, i); width = Math.max(width, comp.getPreferredSize().width); for (int r = 0; r < table.getRowCount(); r++) { renderer = table.getCellRenderer(r, i); comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, i), false, false, r, i); width = Math.max(width, comp.getPreferredSize().width); } totalWidth += width + 2; col.setPreferredWidth(width + 2); } }
From source file:Main.java
/** * Setups the given table for usage as row-header. This method setups the background color to * the same one than the column headers. * * {@note In a previous version, we were assigning to the row headers the same cell renderer than * the one created by <cite>Swing</cite> for the column headers. But it produced strange * effects when the L&F uses a vertical grandiant instead than a uniform color.} * * @param table The table to setup as row headers. * @return The renderer which has been assigned to the table. *//* w ww. j a v a 2 s . co m*/ public static TableCellRenderer setupAsRowHeader(final JTable table) { final JTableHeader header = table.getTableHeader(); Color background = header.getBackground(); Color foreground = header.getForeground(); if (background == null || background.equals(table.getBackground())) { if (!SystemColor.control.equals(background)) { background = SystemColor.control; foreground = SystemColor.controlText; } else { final Locale locale = table.getLocale(); background = UIManager.getColor("Label.background", locale); foreground = UIManager.getColor("Label.foreground", locale); } } final DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(); renderer.setBackground(background); renderer.setForeground(foreground); renderer.setHorizontalAlignment(DefaultTableCellRenderer.RIGHT); final TableColumn column = table.getColumnModel().getColumn(0); column.setCellRenderer(renderer); column.setPreferredWidth(60); table.setPreferredScrollableViewportSize(table.getPreferredSize()); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.setCellSelectionEnabled(false); return renderer; }
From source file:org.drugis.mtc.gui.results.ResultsComponentFactory.java
private static JTable createTableWithoutHeaders(NetworkRelativeEffectTableModel dm) { final JTable table = new JTable(dm); table.setTableHeader(null);/*w w w .j av a 2 s . com*/ table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); for (final TableColumn c : Collections.list(table.getColumnModel().getColumns())) { c.setMinWidth(170); c.setPreferredWidth(170); } TableCopyHandler.registerCopyAction(table); return table; }
From source file:Main.java
public static void sizeWidthToFitData(JTable table, int vc, int buf) { TableColumn tc = table.getColumnModel().getColumn(vc); int max = table.getTableHeader().getDefaultRenderer() .getTableCellRendererComponent(table, tc.getHeaderValue(), false, false, 0, vc) .getPreferredSize().width;//from ww w . j av a2s . com int vrows = table.getRowCount(); for (int i = 0; i < vrows; i++) { TableCellRenderer r = table.getCellRenderer(i, vc); Object value = table.getValueAt(i, vc); Component c = r.getTableCellRendererComponent(table, value, false, false, i, vc); int w = c.getPreferredSize().width; if (max < w) { max = w; } } tc.setPreferredWidth(max + buf); }
From source file:Main.java
/** * Auto fit the column of a table.//from ww w . ja v a 2 s .c o m * @param table the table for which to auto fit the columns. * @param columnIndex the index of the column to auto fit. * @param maxWidth the maximum width that a column can take (like Integer.MAX_WIDTH). */ public static void autoFitTableColumn(JTable table, int columnIndex, int maxWidth) { TableModel model = table.getModel(); TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer(); int rowCount = table.getRowCount(); for (int i = columnIndex >= 0 ? columnIndex : model.getColumnCount() - 1; i >= 0; i--) { TableColumn column = table.getColumnModel().getColumn(i); int headerWidth = headerRenderer .getTableCellRendererComponent(table, column.getHeaderValue(), false, false, 0, 0) .getPreferredSize().width; int cellWidth = 0; for (int j = 0; j < rowCount; j++) { Component comp = table.getDefaultRenderer(model.getColumnClass(i)) .getTableCellRendererComponent(table, table.getValueAt(j, i), false, false, 0, i); int preferredWidth = comp.getPreferredSize().width; // Artificial space to look nicer. preferredWidth += 10; cellWidth = Math.max(cellWidth, preferredWidth); } // Artificial space for the sort icon. headerWidth += 20; column.setPreferredWidth(Math.min(Math.max(headerWidth, cellWidth) + table.getRowMargin(), maxWidth)); if (columnIndex >= 0) { break; } } }
From source file:Main.java
/** * Adjusts the widths and heights of the cells of the supplied table to fit their contents. *///from ww w . j av a 2 s . c o m public static void sizeToContents(JTable table) { TableModel model = table.getModel(); TableColumn column = null; Component comp = null; int ccount = table.getColumnModel().getColumnCount(), rcount = model.getRowCount(), cellHeight = 0; for (int cc = 0; cc < ccount; cc++) { int headerWidth = 0, cellWidth = 0; column = table.getColumnModel().getColumn(cc); try { comp = column.getHeaderRenderer().getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0); headerWidth = comp.getPreferredSize().width; } catch (NullPointerException e) { // getHeaderRenderer() this doesn't work in 1.3 } for (int rr = 0; rr < rcount; rr++) { Object cellValue = model.getValueAt(rr, cc); comp = table.getDefaultRenderer(model.getColumnClass(cc)).getTableCellRendererComponent(table, cellValue, false, false, 0, cc); Dimension psize = comp.getPreferredSize(); cellWidth = Math.max(psize.width, cellWidth); cellHeight = Math.max(psize.height, cellHeight); } column.setPreferredWidth(Math.max(headerWidth, cellWidth)); } if (cellHeight > 0) { table.setRowHeight(cellHeight); } }
From source file:ThreadViewer.java
public ThreadViewer() { JTable table = new JTable(tableModel); table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); TableColumnModel colModel = table.getColumnModel(); int numColumns = colModel.getColumnCount(); for (int i = 0; i < numColumns - 1; i++) { TableColumn col = colModel.getColumn(i); col.sizeWidthToFit();/* w ww. ja va 2s .com*/ col.setPreferredWidth(col.getWidth() + 5); col.setMaxWidth(col.getWidth() + 5); } JScrollPane sp = new JScrollPane(table); setLayout(new BorderLayout()); add(sp, BorderLayout.CENTER); }