/** * カラムの非表示設定を行う. * * @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); } } }
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); }
private void resizeColumns() { int columnCount = getColumnCount(); int rowCount = getRowCount(); TableColumnModel columnModel = getColumnModel(); for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) { TableColumn column = columnModel.getColumn(columnIndex); int preferredWidth = column.getMinWidth(); int maxWidth = column.getMaxWidth(); // Header value with TableCellRenderer cellRenderer = column.getHeaderRenderer(); if (cellRenderer == null) { cellRenderer = getTableHeader().getDefaultRenderer(); } Object headerValue = column.getHeaderValue(); Component comp = cellRenderer.getTableCellRendererComponent( this, headerValue, false, false, 0, columnIndex); int width = comp.getPreferredSize().width + getIntercellSpacing().width + 15; preferredWidth = Math.max(preferredWidth, width); // Row values' width for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { cellRenderer = getCellRenderer(rowIndex, columnIndex); comp = prepareRenderer(cellRenderer, rowIndex, columnIndex); width = comp.getPreferredSize().width + getIntercellSpacing().width + 15; preferredWidth = Math.max(preferredWidth, width); if (preferredWidth >= maxWidth) { preferredWidth = maxWidth; break; } } column.setPreferredWidth(preferredWidth); } }