private void packColumn(JTable table, int vColIndex, int margin) { // table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); TableColumn col = colModel.getColumn(vColIndex); int width = 0; // obtiene la anchura de la cabecera de la columna TableCellRenderer renderer = col.getHeaderRenderer(); if (renderer == null) { renderer = table.getTableHeader().getDefaultRenderer(); } Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0); // width = comp.getPreferredSize().width; width = 5; // Obtine la anchura maxima de la coluna de for (int r = 0; r < table.getRowCount(); r++) { renderer = table.getCellRenderer(r, vColIndex); comp = renderer.getTableCellRendererComponent( table, table.getValueAt(r, vColIndex), false, false, r, vColIndex); width = Math.max(width, 0); } width += 2 * margin; // Configura el ancho col.setPreferredWidth(width); }
public void packColumn(JTable table, int vColIndex, int margin) { DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); TableColumn col = colModel.getColumn(vColIndex); int width = 0; // Get width of column header TableCellRenderer renderer = col.getHeaderRenderer(); if (renderer == null) { renderer = table.getTableHeader().getDefaultRenderer(); } Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0); width = comp.getPreferredSize().width; // Get maximum width of column data for (int r = 0; r < table.getRowCount(); r++) { renderer = table.getCellRenderer(r, vColIndex); comp = renderer.getTableCellRendererComponent( table, table.getValueAt(r, vColIndex), false, false, r, vColIndex); width = Math.max(width, comp.getPreferredSize().width); } // Add margin width += 2 * margin; // Set the width col.setPreferredWidth(width); }
/** Creates new form ValuesDialog */ public StylesDialog(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); applyI18n(); this.setSize(490, 250); // DefaultTableColumnModel dtcm = (DefaultTableColumnModel)jTableParameters.getColumnModel(); // DefaultTableCellRenderer tcr = // (DefaultTableCellRenderer)dtcm.getColumn(0).getHeaderRenderer(); // new DefaultTableCellRenderer(); // tcr.setFont(jTableParameters.getFont()); // tcr.setBackground(this.getBackground()); // tcr.setBorder( new javax.swing.border.BevelBorder( javax.swing.border.BevelBorder.RAISED)); // dtcm.getColumn(0).setHeaderRenderer(tcr); DefaultListSelectionModel dlsm = (DefaultListSelectionModel) this.jTableParameters.getSelectionModel(); dlsm.addListSelectionListener( new javax.swing.event.ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { jTableParametersListSelectionValueChanged(e); } }); // Open in center... it.businesslogic.ireport.util.Misc.centerFrame(this); DefaultTableModel dtm = (DefaultTableModel) jTableParameters.getModel(); it.businesslogic.ireport.gui.MainFrame.getMainInstance() .getActiveReportFrame() .getReport() .addStyleChangedListener(this); Enumeration e = it.businesslogic.ireport.gui.MainFrame.getMainInstance() .getActiveReportFrame() .getReport() .getStyles() .elements(); while (e.hasMoreElements()) { Style style = (Style) e.nextElement(); addStyle(style); } DefaultTableColumnModel dtcm = (DefaultTableColumnModel) jTableParameters.getColumnModel(); dtcm.getColumn(0).setPreferredWidth(350); dtcm.getColumn(1).setPreferredWidth(100); javax.swing.KeyStroke escape = javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0, false); javax.swing.Action escapeAction = new javax.swing.AbstractAction() { public void actionPerformed(java.awt.event.ActionEvent e) { setVisible(false); } }; getRootPane().getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, "ESCAPE"); getRootPane().getActionMap().put("ESCAPE", escapeAction); }
private void packColumns(int inMargin) { TableModel model = this.table.getModel(); DefaultTableColumnModel columnModel = (DefaultTableColumnModel) table.getColumnModel(); for (int col = 0; col < this.table.getColumnCount(); col++) { TableColumn column = columnModel.getColumn(col); int width = 0; // determine widest row in column for (int row = 0; row < this.table.getRowCount(); row++) { TableCellRenderer renderer = this.table.getCellRenderer(row, col); Component component = renderer.getTableCellRendererComponent( this.table, this.table.getValueAt(row, col), false, false, row, col); width = Math.max(width, component.getPreferredSize().width); } // add the margin width += 2 * inMargin; column.setPreferredWidth(width); } }
// 返回列的名称 private String getSelectColumnIdentifier() { // 得到选中列索引 int selectIndex = this.table.getSelectColumn(); if (selectIndex == -1) return null; DefaultTableColumnModel colModel = (DefaultTableColumnModel) this.table.getColumnModel(); return (String) colModel.getColumn(selectIndex).getIdentifier(); }
/** * Constructs a <code>VariabilityRecordTable</code> with a list of variability records. * * @param record_list the list of variability records. * @param desktop the parent desktop. */ public VariabilityRecordTable(Vector record_list, net.aerith.misao.gui.Desktop desktop) { this.record_list = record_list; this.desktop = desktop; index = new ArrayIndex(record_list.size()); model = new DefaultTableModel(column_names, 0); Object[] objects = new Object[column_names.length]; objects[0] = new Boolean(true); for (int i = 1; i < column_names.length; i++) objects[i] = ""; for (int i = 0; i < record_list.size(); i++) model.addRow(objects); setModel(model); column_model = (DefaultTableColumnModel) getColumnModel(); for (int i = 1; i < column_names.length; i++) column_model .getColumn(i) .setCellRenderer( new StringRenderer(column_names[i], LabelTableCellRenderer.MODE_MULTIPLE_SELECTION)); initializeCheckColumn(); setTableHeader(new TableHeader(column_model)); setAutoResizeMode(JTable.AUTO_RESIZE_OFF); initializeColumnWidth(); pane = this; initPopupMenu(); }
// 设置每列的宽 private void setTableColumn(JTable table) { DefaultTableColumnModel model = (DefaultTableColumnModel) table.getColumnModel(); for (int i = 0; i < table.getColumnCount(); i++) { // 得到列对象 TableColumn column = model.getColumn(i); // 得到列名 String columnName = (String) column.getIdentifier(); // 设置最小宽度 int width = columnName.length() * 10; // 设置列宽最小为150 int prefectWidth = (width < 150) ? 150 : width; column.setMinWidth(prefectWidth); } }
public void autoResizeColWidth() { int margin = 5; for (int i = 0; i < tabel.getColumnCount(); i++) { int vColIndex = i; DefaultTableColumnModel colModel = (DefaultTableColumnModel) tabel.getColumnModel(); TableColumn col = colModel.getColumn(vColIndex); int width = 0; // Get width of column header TableCellRenderer renderer = col.getHeaderRenderer(); if (renderer == null) { renderer = tabel.getTableHeader().getDefaultRenderer(); } Component comp = renderer.getTableCellRendererComponent(tabel, col.getHeaderValue(), false, false, 0, 0); width = comp.getPreferredSize().width; // Get maximum width of column data for (int r = 0; r < tabel.getRowCount(); r++) { renderer = tabel.getCellRenderer(r, vColIndex); comp = renderer.getTableCellRendererComponent( tabel, tabel.getValueAt(r, vColIndex), false, false, r, vColIndex); width = Math.max(width, comp.getPreferredSize().width); } // Add margin width += 2 * margin; // Set the width col.setPreferredWidth(width); } ((DefaultTableCellRenderer) tabel.getTableHeader().getDefaultRenderer()) .setHorizontalAlignment(SwingConstants.LEFT); // table.setAutoCreateRowSorter(true); tabel.getTableHeader().setReorderingAllowed(false); }
/** Initializes the column width. */ protected void initializeColumnWidth() { column_model.getColumn(0).setPreferredWidth(20); column_model.getColumn(1).setPreferredWidth(100); column_model.getColumn(2).setPreferredWidth(100); column_model.getColumn(3).setPreferredWidth(100); column_model.getColumn(4).setPreferredWidth(60); column_model.getColumn(5).setPreferredWidth(60); column_model.getColumn(6).setPreferredWidth(60); column_model.getColumn(7).setPreferredWidth(40); column_model.getColumn(8).setPreferredWidth(40); column_model.getColumn(9).setPreferredWidth(40); column_model.getColumn(10).setPreferredWidth(160); column_model.getColumn(11).setPreferredWidth(160); }
public void packColumn(JTable table, int vColIndex, int margin) { TableModel model = table.getModel(); DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); TableColumn col = colModel.getColumn(vColIndex); int width = 0; // Obtém a largura do cabeçalho da coluna TableCellRenderer renderer = col.getHeaderRenderer(); if (renderer == null) { renderer = table.getTableHeader().getDefaultRenderer(); } Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0); width = comp.getPreferredSize().width; // Obtém a largura maxima da coluna de dados for (int r = 0; r < table.getRowCount(); r++) { renderer = table.getCellRenderer(r, vColIndex); comp = renderer.getTableCellRendererComponent( table, table.getValueAt(r, vColIndex), false, false, r, vColIndex); width = Math.max(width, comp.getPreferredSize().width); } width += 2 * margin; // Configura a largura col.setPreferredWidth(width); }