List of usage examples for javax.swing.table JTableHeader getDefaultRenderer
@Transient
public TableCellRenderer getDefaultRenderer()
headerRenderer
is defined by a TableColumn
. From source file:Main.java
/** * Calculates the optimal width for the header of the given table. The * calculation is based on the preferred width of the header renderer. * * @param table the table to calculate the column width * @param col the column to calculate the widths * @return the width, -1 if error *//*from w w w.j a v a2s .com*/ public static int calcHeaderWidth(JTable table, int col) { if (table == null) return -1; if (col < 0 || col > table.getColumnCount()) { System.out.println("invalid col " + col); return -1; } JTableHeader header = table.getTableHeader(); TableCellRenderer defaultHeaderRenderer = null; if (header != null) defaultHeaderRenderer = header.getDefaultRenderer(); TableColumnModel columns = table.getColumnModel(); TableModel data = table.getModel(); TableColumn column = columns.getColumn(col); 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, col); width = c.getPreferredSize().width + 5; } return width; }
From source file:edu.ku.brc.stats.StatGroupTable.java
/** * Calculates and sets the each column to it preferred size * @param table the table to fix ups//from w ww . j a va 2s . c o m */ public static void calcColumnWidths(JTable table) { JTableHeader header = table.getTableHeader(); TableCellRenderer defaultHeaderRenderer = null; if (header != null) { defaultHeaderRenderer = header.getDefaultRenderer(); } TableColumnModel columns = table.getColumnModel(); TableModel data = table.getModel(); int margin = columns.getColumnMargin(); // only JDK1.3 int rowCount = data.getRowCount(); int totalWidth = 0; 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, data.getValueAt(row, columnIndex), false, false, row, i); width = Math.max(width, c.getPreferredSize().width + 10); // adding an arbitray 10 pixels to make it look nicer } if (width >= 0) { column.setPreferredWidth(width + margin); // <1.3: without margin } totalWidth += column.getPreferredWidth(); } // If you like; This does not make sense for two many columns! Dimension size = table.getPreferredScrollableViewportSize(); //if (totalWidth > size.width) { size.height = Math.min(size.height, table.getRowHeight() * visibleRows); size.width = totalWidth; table.setPreferredScrollableViewportSize(size); } }
From source file:edu.ku.brc.specify.utilapps.SetUpBuildDlg.java
/** * @param table// ww w. j a v a 2s . c o m */ public static void calcColumnWidths(JTable table) { JTableHeader header = table.getTableHeader(); TableCellRenderer defaultHeaderRenderer = null; if (header != null) { defaultHeaderRenderer = header.getDefaultRenderer(); } TableColumnModel columns = table.getColumnModel(); TableModel data = table.getModel(); int margin = columns.getColumnMargin(); // only JDK1.3 int rowCount = data.getRowCount(); int totalWidth = 0; 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, data.getValueAt(row, columnIndex), false, false, row, i); width = Math.max(width, c.getPreferredSize().width + 10); // adding an arbitray 10 pixels to make it look nicer } if (width >= 0) { column.setPreferredWidth(width + margin); // <1.3: without margin } else { // ??? } totalWidth += column.getPreferredWidth(); } }
From source file:com.mirth.connect.client.ui.components.MirthTreeTable.java
public MirthTreeTable(String prefix, Set<String> defaultVisibleColumns) { customHiddenColumnMap = new HashMap<String, Set<String>>(); this.prefix = prefix; this.defaultVisibleColumns = defaultVisibleColumns; columnOrderMap = new HashMap<String, Integer>(); sortOrderColumn = -1;//from w w w . j ava 2 s . c om sortOrder = null; if (StringUtils.isNotEmpty(prefix)) { try { userPreferences = Preferences.userNodeForPackage(Mirth.class); String columns = userPreferences.get(prefix + "ColumnOrderMap", ""); if (StringUtils.isNotEmpty(columns)) { columnOrderMap = (Map<String, Integer>) ObjectXMLSerializer.getInstance().deserialize(columns, Map.class); } } catch (Exception e) { } try { String order = userPreferences.get(prefix + "SortOrder", ""); if (StringUtils.isNotEmpty(order)) { sortOrder = ObjectXMLSerializer.getInstance().deserialize(order, SortOrder.class); sortOrderColumn = userPreferences.getInt(prefix + "SortOrderColumn", -1); } } catch (Exception e) { } } addKeyListener(new KeyListener() { public void keyPressed(KeyEvent e) { boolean isAccelerated = (((e.getModifiers() & Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) > 0) || ((e.getModifiers() & InputEvent.CTRL_MASK) > 0)); if ((e.getKeyCode() == KeyEvent.VK_S) && isAccelerated) { PlatformUI.MIRTH_FRAME.doContextSensitiveSave(); } } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } }); /* * Swingx 1.0 has this set to true by default, which doesn't allow dragging and dropping * into tables. Swingx 0.8 had this set to false. Tables that want it set to true can * override it. */ putClientProperty("terminateEditOnFocusLost", Boolean.FALSE); JTableHeader header = getTableHeader(); header.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) { saveColumnOrder(); } }); header.setDefaultRenderer(new SortableHeaderCellRenderer(header.getDefaultRenderer())); final JButton columnControlButton = new JButton(new ColumnControlButton(this).getIcon()); columnControlButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JPopupMenu columnMenu = getColumnMenu(); Dimension buttonSize = columnControlButton.getSize(); int xPos = columnControlButton.getComponentOrientation().isLeftToRight() ? buttonSize.width - columnMenu.getPreferredSize().width : 0; columnMenu.show(columnControlButton, xPos, columnControlButton.getHeight()); } }); setColumnControl(columnControlButton); }
From source file:com.vgi.mafscaling.ClosedLoop.java
private JTable createAfrDataTable(JPanel panel, String tableName, int gridy) { final JTable afrTable = new JTable() { private static final long serialVersionUID = 6526901361175099297L; public boolean isCellEditable(int row, int column) { return false; };// w ww. j a v a2s .c o m }; DefaultTableColumnModel afrModel = new DefaultTableColumnModel(); final TableColumn afrColumn = new TableColumn(0, 250); afrColumn.setHeaderValue(tableName); afrModel.addColumn(afrColumn); JTableHeader lblAfrTableName = afrTable.getTableHeader(); lblAfrTableName.setColumnModel(afrModel); lblAfrTableName.setReorderingAllowed(false); DefaultTableCellRenderer headerRenderer = (DefaultTableCellRenderer) lblAfrTableName.getDefaultRenderer(); headerRenderer.setHorizontalAlignment(SwingConstants.LEFT); GridBagConstraints gbc_lblAfrTableName = new GridBagConstraints(); gbc_lblAfrTableName.insets = new Insets((gridy == 0 ? 0 : 5), 0, 0, 0); gbc_lblAfrTableName.anchor = GridBagConstraints.PAGE_START; gbc_lblAfrTableName.fill = GridBagConstraints.HORIZONTAL; gbc_lblAfrTableName.gridx = 0; gbc_lblAfrTableName.gridy = gridy; panel.add(lblAfrTableName, gbc_lblAfrTableName); afrTable.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { afrColumn.setWidth(afrTable.getWidth()); } }); afrTable.getTableHeader().setReorderingAllowed(false); afrTable.setColumnSelectionAllowed(true); afrTable.setCellSelectionEnabled(true); afrTable.setBorder(new LineBorder(new Color(0, 0, 0))); afrTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); afrTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); afrTable.setModel(new DefaultTableModel(AfrTableRowCount, AfrTableColumnCount)); Utils.initializeTable(afrTable, ColumnWidth); if (tableName.equals(Afr1TableName)) { Format[][] formatMatrix = { { new DecimalFormat("#"), new DecimalFormat("0.00") } }; NumberFormatRenderer renderer = (NumberFormatRenderer) afrTable.getDefaultRenderer(Object.class); renderer.setFormats(formatMatrix); } else if (tableName.equals(Afr2TableName)) { Format[][] formatMatrix = { { new DecimalFormat("#"), new DecimalFormat("0.00") }, { new DecimalFormat("#"), new DecimalFormat("#") } }; NumberFormatRenderer renderer = (NumberFormatRenderer) afrTable.getDefaultRenderer(Object.class); renderer.setFormats(formatMatrix); } GridBagConstraints gbc_afrTable = new GridBagConstraints(); gbc_afrTable.insets = new Insets(0, 0, 0, 0); gbc_afrTable.anchor = GridBagConstraints.PAGE_START; gbc_afrTable.gridx = 0; gbc_afrTable.gridy = gridy + 1; panel.add(afrTable, gbc_afrTable); excelAdapter.addTable(afrTable, true, false); return afrTable; }
From source file:com.aw.swing.mvp.binding.component.BndSJTable.java
/** * Configure the grid columns based on the ColumnInfo information *///from w w w . j ava 2 s . com protected void configureColumns() { //jcv:2.0.1 // TableCellRenderer tableCellRenderer = createHeaderRenderer(); JTableHeader jTableHeader = jTable.getTableHeader(); TableCellRenderer tableCellRenderer = jTableHeader.getDefaultRenderer(); for (int i = 0; i < columnsInfo.length; i++) { TableColumn col = jTable.getColumnModel().getColumn(i); if (columnsInfo[i] instanceof SelectorColumn) { final SelectorColumn selectorColumn = (SelectorColumn) columnsInfo[i]; // Set special renderer for the column's header col.setHeaderRenderer(getSelectorHeaderRenderer(selectorColumn)); } else { // Set special renderer for the column's header col.setHeaderRenderer(tableCellRenderer); } // set column width if (columnsInfo[i].getWidth() > 0) { col.setPreferredWidth(columnsInfo[i].getWidth()); } if (columnsInfo[i].isEditable() || StringUtils.hasText(columnsInfo[i].getValidationRule())) { columnsInfo[i].initializeValidator(); } // set column alignment if the cell is not editable if (!columnsInfo[i].isEditable() && !(columnsInfo[i] instanceof ButtonColumn)) { configureReadOnlyCell(col, columnsInfo[i]); } else { configureEditableCell(col, columnsInfo[i]); } columnsInfo[i].setIdx(i); } if (fixedColumns > 0) { fixedColumnTableMgr = FixedColumnTableDecorator.decorate(getJTable(), fixedColumns); JTable fixedTable = fixedColumnTableMgr.getFixedTable(); JTableMouseProcessor jTableMouseProcessor = new JTableMouseProcessor(presenter, this, gridIndex); jTableMouseProcessor.setJTable(fixedTable); jTableMouseProcessor.setInFixedTable(true); fixedTable.addMouseListener(getJTableMouseListener(jTableMouseProcessor)); fixedTable.getTableHeader().addMouseListener(getJTableHeaderMouseListener(jTableMouseProcessor)); } }
From source file:edu.ku.brc.ui.UIHelper.java
/** * Calculates and sets the each column to it preferred size. NOTE: This * method also sets the table height to 10 rows. * //from w w w . jav a 2 s.co m * @param table the table to fix up * @param numRowsHeight the number of rows to make the table height (or null not to set it) */ public static void calcColumnWidths(final JTable table, final Integer numRowsHeight, final Integer maxWidth) { if (table != null) { JTableHeader header = table.getTableHeader(); TableCellRenderer defaultHeaderRenderer = null; if (header != null) { defaultHeaderRenderer = header.getDefaultRenderer(); } TableColumnModel columns = table.getColumnModel(); TableModel data = table.getModel(); int margin = columns.getColumnMargin(); // only JDK1.3 int rowCount = data.getRowCount(); int totalWidth = 0; 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, data.getValueAt(row, columnIndex), false, false, row, i); width = Math.max(width, c.getPreferredSize().width + 10); // adding an arbitray 10 pixels to make it look nicer if (maxWidth != null) { width = Math.min(width, maxWidth); } } if (width >= 0) { column.setPreferredWidth(width + margin); // <1.3: without margin } else { // ??? } totalWidth += column.getPreferredWidth(); } // If you like; This does not make sense for two many columns! Dimension size = table.getPreferredScrollableViewportSize(); //if (totalWidth > size.width) { if (numRowsHeight != null) { size.height = Math.min(size.height, table.getRowHeight() * numRowsHeight); } size.width = totalWidth; table.setPreferredScrollableViewportSize(size); } } }
From source file:org.nuclos.client.genericobject.GenericObjectCollectController.java
/** * sets up a modified renderer for the result table header that uses the HTML formatted toString() value of the * column's entity field rather than its unformatted getLabel() value. *//*from w w w .ja v a 2 s .co m*/ private void setupResultTableHeaderRenderer() { final JTableHeader header = getResultTable().getTableHeader(); final TableCellRenderer headerrendererDefault = header.getDefaultRenderer(); header.setDefaultRenderer(new TableCellRenderer() { @Override public Component getTableCellRendererComponent(JTable tbl, Object oValue, boolean bSelected, boolean bHasFocus, int iRow, int iColumn) { final CollectableEntityField f = GenericObjectCollectController.this.getResultTableModel() .getCollectableEntityField(tbl.convertColumnIndexToModel(iColumn)); final String value; if (f instanceof CollectableEOEntityField) { // In the pivot case, we want to separate the column name // from the tool top. final CollectableEOEntityField field = (CollectableEOEntityField) f; final EntityFieldMetaDataVO ef = field.getMeta(); if (ef.getPivotInfo() != null) { value = ef.getField() + ":" + ef.getPivotInfo().getValueField(); } else { value = f.getLabel(); } } else { value = f.getLabel(); } return headerrendererDefault.getTableCellRendererComponent(tbl, value, bSelected, bHasFocus, iRow, iColumn); } }); }