protected JTable createDefaultsTable() { JTable table = new JTable(new UIDefaultsTableModel()); table.setRowHeight(rowHeight); table.setShowHorizontalLines(false); table.setShowVerticalLines(false); table.setIntercellSpacing(new Dimension(0, 0)); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); initFilters(table); DefaultTableColumnModel columnModel = new DefaultTableColumnModel(); Color rowColors[] = new Color[2]; rowColors[0] = UIManager.getColor("Table.background"); rowColors[1] = new Color( (int) (rowColors[0].getRed() * .90), (int) (rowColors[0].getGreen() * .95), (int) (rowColors[0].getBlue() * .95)); int width = 0; TableColumn column = new TableColumn(); column.setCellRenderer(new KeyRenderer(rowColors)); column.setModelIndex(UIDefaultsTableModel.KEY_COLUMN); column.setHeaderValue("Key"); column.setPreferredWidth(250); columnModel.addColumn(column); width += column.getPreferredWidth(); column = new TableColumn(); column.setCellRenderer(new RowRenderer(rowColors)); column.setModelIndex(UIDefaultsTableModel.TYPE_COLUMN); column.setHeaderValue("Type"); column.setPreferredWidth(250); columnModel.addColumn(column); width += column.getPreferredWidth(); column = new TableColumn(); column.setCellRenderer(new ValueRenderer(rowColors)); column.setModelIndex(UIDefaultsTableModel.VALUE_COLUMN); column.setHeaderValue("Value"); column.setPreferredWidth(300); columnModel.addColumn(column); width += column.getPreferredWidth(); table.setColumnModel(columnModel); table.setPreferredScrollableViewportSize(new Dimension(width, 12 * rowHeight)); return table; }
/** * カラムの非表示設定を行う. * * @param col 対象カラム番号 * @param hide true:表示 false:非表示 */ public void setColumnHide(int col, boolean hide) { TableColumn tc = getColumnModel().getColumn(convertColumnIndexToView(col)); int[] setting = new int[4]; setting[0] = tc.getWidth(); setting[1] = tc.getPreferredWidth(); setting[2] = tc.getMinWidth(); setting[3] = tc.getMaxWidth(); if (hide) { if ((setting[0] == 0) && (setting[1] == 0) && (setting[2] == 0) && (setting[3] == 0)) { int[] baseSetting = columnSizeSetting.get(col); // setMaxWidthを実行する順番が大事 tc.setMaxWidth(baseSetting[3]); tc.setMinWidth(baseSetting[2]); tc.setPreferredWidth(baseSetting[1]); tc.setWidth(baseSetting[0]); } } else { if ((setting[0] != 0) || (setting[1] != 0) || (setting[2] != 0) || (setting[3] != 0)) { columnSizeSetting.put(col, setting); // setMaxWidthを実行する順番が大事 tc.setWidth(0); tc.setPreferredWidth(0); tc.setMinWidth(0); tc.setMaxWidth(0); } } }
@Override public void actionPerformed(ActionEvent e) { // JCheckBoxMenuItem item = (JCheckBoxMenuItem) e.getSource(); TableColumnModel model = table.getColumnModel(); if (colData.isVisible() == false) { colData.setVisible(true); model.addColumn(column); // column is added at right end of model // move column to its default place int lastPos = model.getColumnCount() - 1; int pos = data.getColumnNumber(colData); if (pos >= 0 && pos < lastPos) model.moveColumn(lastPos, pos); scrollPane.setSize( scrollPane.getWidth() + column.getPreferredWidth(), scrollPane.getHeight()); // show breakPointColumn => show all lines if (isBreakPointColumn) { kernel.getConstruction().setShowOnlyBreakpoints(false); // cbShowOnlyBreakpoints.setSelected(false); } } else { colData.setVisible(false); model.removeColumn(column); // setSize(getWidth() - column.getWidth(), getHeight()); // setSize(view.getWidth(), getHeight()); } table.tableChanged(new TableModelEvent(((GAbstractTableModelD) data.getImpl()).getImpl())); // reinit view to update possible breakpoint changes ((ConstructionTableData) data).initView(); SwingUtilities.updateComponentTreeUI(view.scrollPane); }
/** Adjust the preferred size of the <code>Table</code>. */ private void adjustPreferredSize() { // Refresh the model model.refresh(); int preferredWidth = 20; TableColumn col = null; for (Enumeration<TableColumn> e = table.getColumnModel().getColumns(); e.hasMoreElements(); ) { col = e.nextElement(); // Calculs & sets the preferred width of the cells. int colPreferredWidth = col.getPreferredWidth(); for (int i = 0; i < table.getRowCount(); i++) { int cellWidth = table .getTableHeader() .getFontMetrics(table.getTableHeader().getFont()) .stringWidth(model.getValueAt(i, col.getModelIndex()).toString()) + 5; if (cellWidth > colPreferredWidth) colPreferredWidth = cellWidth; } // Increments the table preferred width. col.setPreferredWidth(colPreferredWidth); // Increments the table preferred width. preferredWidth += colPreferredWidth; } // Set the preferred size. setPreferredSize(new Dimension(preferredWidth, (int) getPreferredSize().getHeight())); }
/** @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent) */ public void mouseReleased(MouseEvent e) { if (columnBeingResized == null) return; ColumnSettingParameter<CommonColumnType> csPar = parameters.getParameter(PeakListTableParameters.commonColumns); ColumnSettingParameter<DataFileColumnType> dfPar = parameters.getParameter(PeakListTableParameters.dataFileColumns); final int modelIndex = columnBeingResized.getModelIndex(); final int newWidth = columnBeingResized.getPreferredWidth(); final int numOfCommonColumns = CommonColumnType.values().length; final int numOfDataFileColumns = DataFileColumnType.values().length; if (modelIndex < numOfCommonColumns) { csPar.setColumnWidth(modelIndex, newWidth); } else { int dataFileColumnIndex = (modelIndex - numOfCommonColumns) % numOfDataFileColumns; dfPar.setColumnWidth(dataFileColumnIndex, newWidth); // set same width to other data file columns of this type for (int dataFileIndex = peakList.getNumberOfRawDataFiles() - 1; dataFileIndex >= 0; dataFileIndex--) { int columnIndex = numOfCommonColumns + (dataFileIndex * numOfDataFileColumns) + dataFileColumnIndex; TableColumn col = this.getColumnByModelIndex(columnIndex); int currentWidth = col.getPreferredWidth(); if (currentWidth != newWidth) { col.setPreferredWidth(newWidth); } } } }
/** * Return the preferred size of the header. The preferred height is the maximum of the preferred * heights of all of the components provided by the header renderers. The preferred width is the * sum of the preferred widths of each column (plus inter-cell spacing). */ public Dimension getPreferredSize(JComponent c) { if ((header == null) || (header.getTable() == null)) { return new Dimension(0, 0); } long width = 0; Enumeration enumeration = header.getColumnModel().getColumns(); while (enumeration.hasMoreElements()) { TableColumn aColumn = (TableColumn) enumeration.nextElement(); width = width + aColumn.getPreferredWidth(); } if (width > Integer.MAX_VALUE) { width = Integer.MAX_VALUE; } return new Dimension((int) width, getHeaderHeight()); }
/* * Update the TableColumn with the newly calculated width */ private void updateTableColumn(int column, int width) { final TableColumn tableColumn = table.getColumnModel().getColumn(column); if (!tableColumn.getResizable()) return; width += spacing; // Don't shrink the column width if (isOnlyAdjustLarger) { width = Math.max(width, tableColumn.getPreferredWidth()); } columnSizes.put(tableColumn, new Integer(tableColumn.getWidth())); table.getTableHeader().setResizingColumn(tableColumn); tableColumn.setWidth(width); }
private static int calcMaxWidth(JTable table) { int colsNum = table.getColumnModel().getColumnCount(); int totalWidth = 0; for (int col = 0; col < colsNum - 1; col++) { TableColumn column = table.getColumnModel().getColumn(col); int preferred = column.getPreferredWidth(); int width = Math.max(preferred, columnMaxWidth(table, col)); totalWidth += width; column.setMinWidth(width); column.setMaxWidth(width); column.setWidth(width); column.setPreferredWidth(width); } totalWidth += columnMaxWidth(table, colsNum - 1); return totalWidth; }
private void strechShareWithRestColumns(int[] columnWidths) { if (columnWidths == null) return; List<TableColumn> columnsToStrech = new ArrayList<TableColumn>(); int alreadyDefinedSize = 0; for (int i = 1; i < columnWidths.length - 1; i++) { TableColumn tc = table.getColumnModel().getColumn(i); if (columnWidths[i] == -1 && tc.getMaxWidth() > 0) columnsToStrech.add(tc); else alreadyDefinedSize += tc.getPreferredWidth(); } if (columnsToStrech.size() == 0) return; int sharedTotal = table.getColumnModel().getTotalColumnWidth() - alreadyDefinedSize; for (TableColumn tc : columnsToStrech) { int newSize = sharedTotal / columnsToStrech.size(); tc.setPreferredWidth(newSize); tc.setWidth(newSize); } }
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 tc = null; for (int i = 0; i < 3; i++) { tc = table.getColumnModel().getColumn(i); if (tc.getModelIndex() == 0) tc.setResizable(false); if (i == 2) { tc.setPreferredWidth(100); // sport column is bigger } else { tc.setPreferredWidth(50); } System.out.printf( "column [%s] header=[%s] modelIndex=[%d] resizable=[%b] minWidth=[%s] maxWidth=[%d] preferredWidth=[%d]\n", tc.getIdentifier(), tc.getHeaderValue(), tc.getModelIndex(), tc.getResizable(), tc.getMinWidth(), tc.getMaxWidth(), tc.getPreferredWidth()); } frame.setSize(300, 150); frame.setVisible(true); }
/** * This method picks good column sizes. If all column heads are wider than the column's cells' * contents, then you can just use column.sizeWidthToFit(). */ private void initColumnSizes(JTable table) { TableModel model = table.getModel(); if (model.getRowCount() > 0) { TableColumn column = null; Component comp = null; int cellWidth = 0; TableColumnModel columnModel = table.getColumnModel(); for (int i = 0; i < columnModel.getColumnCount(); i++) { column = columnModel.getColumn(i); for (int j = 0; j < table.getRowCount(); j++) { TableCellRenderer cellRenderer = table.getCellRenderer(j, i); comp = cellRenderer.getTableCellRendererComponent( table, model.getValueAt(j, i), false, false, j, i); cellWidth = comp.getPreferredSize().width; column.setPreferredWidth(Math.max(column.getPreferredWidth(), cellWidth)); } } } }
public int calcColumnWidths(JTableIbp table) { int panelsWidth = 0; JTableHeader header = table.getTableHeader(); TableCellRenderer defaultHeaderRenderer = null; if (header != null) defaultHeaderRenderer = header.getDefaultRenderer(); TableColumnModel columns = table.getColumnModel(); // tableModel = table.getModel(); int margin = columns.getColumnMargin(); // only JDK1.3 int rowCount = table.getModel().getRowCount(); for (int i = columns.getColumnCount() - 1; i >= 0; --i) { TableColumn column = columns.getColumn(i); int columnIndex = column.getModelIndex(); int width = -1; TableCellRenderer h = column.getHeaderRenderer(); if (h == null) h = defaultHeaderRenderer; if (h != null) // Not explicitly impossible { Component c = h.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1, i); width = c.getPreferredSize().width; } for (int row = rowCount - 1; row >= 0; --row) { TableCellRenderer r = table.getCellRenderer(row, i); Component c = r.getTableCellRendererComponent( table, table.getModel().getValueAt(row, columnIndex), false, false, row, i); width = Math.max(width, c.getPreferredSize().width); } if (width >= 0) column.setPreferredWidth(width + margin); // <1.3: without // margin else ; // ??? panelsWidth += column.getPreferredWidth(); } panelsWidth = panelsWidth > (screenWidth - 30) ? screenWidth : panelsWidth; frameWidth = panelsWidth > (frameWidth - 30) ? (panelsWidth - 30) : frameWidth; return panelsWidth; }
private void setFixedColumnWidth(final int columnIndex) { final TableColumn column = myEntryTable.getTableHeader().getColumnModel().getColumn(columnIndex); column.setResizable(false); column.setMaxWidth(column.getPreferredWidth()); }
package com.l2fprod.common.swing.table;