List of usage examples for javax.swing JTable getTableHeader
public JTableHeader getTableHeader()
tableHeader
used by this JTable
. From source file:be.ugent.maf.cellmissy.gui.controller.analysis.singlecell.SingleCellStatisticsController.java
/** * Show p-values in correspondent table//from ww w .j ava2 s .c o m * * @param analysisGroup */ private void showPValues(SingleCellAnalysisGroup singleCellAnalysisGroup, boolean isAdjusted) { String statisticalTestName = singleCellAnalysisController.getAnalysisPanel().getStatTestComboBox() .getSelectedItem().toString(); SingleCellPValuesTableModel pValuesTableModel = new SingleCellPValuesTableModel(singleCellAnalysisGroup, isAdjusted); JTable pValuesTable = singleCellAnalysisController.getAnalysisPanel().getComparisonTable(); pValuesTable.setModel(pValuesTableModel); Double selectedSignLevel = (Double) singleCellAnalysisController.getAnalysisPanel().getSignLevelComboBox() .getSelectedItem(); // detect significances with selected alpha level singleCellStatisticsAnalyzer.detectSignificance(singleCellAnalysisGroup, statisticalTestName, selectedSignLevel, isAdjusted); boolean[][] significances = singleCellAnalysisGroup.getSignificances(); for (int i = 1; i < pValuesTable.getColumnCount(); i++) { pValuesTable.getColumnModel().getColumn(i) .setCellRenderer(new PValuesTableRenderer(new DecimalFormat("#.####"), significances)); } pValuesTable.getTableHeader().setDefaultRenderer(new TableHeaderRenderer(SwingConstants.RIGHT)); }
From source file:net.sf.taverna.t2.activities.spreadsheet.views.SpreadsheetImportConfigView.java
private void enableTable(JTable table, boolean enabled) { table.setEnabled(enabled);//ww w . j av a 2 s. c om Component editor = table.getEditorComponent(); if (editor != null) { editor.setEnabled(enabled); } if (enabled) { table.setForeground(Color.BLACK); table.getTableHeader().setForeground(Color.BLACK); } else { table.setForeground(Color.LIGHT_GRAY); table.getTableHeader().setForeground(Color.LIGHT_GRAY); } }
From source file:modnlp.capte.AlignmentInterfaceWS.java
public JTable autoResizeColWidth(JTable table, DefaultTableModel model) { table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.setModel(model);/*from w ww . j a v a 2 s . c om*/ int margin = 5; for (int i = 0; i < table.getColumnCount(); i++) { int vColIndex = i; 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); } ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()) .setHorizontalAlignment(SwingConstants.LEFT); // table.setAutoCreateRowSorter(true); table.getTableHeader().setReorderingAllowed(false); return table; }
From source file:RowHeaderTable.java
public RowHeaderTable() { super("Row Header Test"); setSize(300, 200);/*from w ww. j a v a 2s . com*/ setDefaultCloseOperation(EXIT_ON_CLOSE); TableModel tm = new AbstractTableModel() { String data[] = { "", "a", "b", "c", "d", "e" }; String headers[] = { "Row #", "Column 1", "Column 2", "Column 3", "Column 4", "Column 5" }; public int getColumnCount() { return data.length; } public int getRowCount() { return 1000; } public String getColumnName(int col) { return headers[col]; } // Synthesize some entries using the data values & the row # public Object getValueAt(int row, int col) { return data[col] + row; } }; // Create a column model for the main table. This model ignores the // first // column added, and sets a minimum width of 150 pixels for all others. TableColumnModel cm = new DefaultTableColumnModel() { boolean first = true; public void addColumn(TableColumn tc) { // Drop the first column . . . that'll be the row header if (first) { first = false; return; } tc.setMinWidth(150); // just for looks, really... super.addColumn(tc); } }; // Create a column model that will serve as our row header table. This // model picks a maximum width and only stores the first column. TableColumnModel rowHeaderModel = new DefaultTableColumnModel() { boolean first = true; public void addColumn(TableColumn tc) { if (first) { tc.setMaxWidth(tc.getPreferredWidth()); super.addColumn(tc); first = false; } // Drop the rest of the columns . . . this is the header column // only } }; JTable jt = new JTable(tm, cm); // Set up the header column and get it hooked up to everything JTable headerColumn = new JTable(tm, rowHeaderModel); jt.createDefaultColumnsFromModel(); headerColumn.createDefaultColumnsFromModel(); // Make sure that selections between the main table and the header stay // in sync (by sharing the same model) jt.setSelectionModel(headerColumn.getSelectionModel()); // Make the header column look pretty //headerColumn.setBorder(BorderFactory.createEtchedBorder()); headerColumn.setBackground(Color.lightGray); headerColumn.setColumnSelectionAllowed(false); headerColumn.setCellSelectionEnabled(false); // Put it in a viewport that we can control a bit JViewport jv = new JViewport(); jv.setView(headerColumn); jv.setPreferredSize(headerColumn.getMaximumSize()); // With out shutting off autoResizeMode, our tables won't scroll // correctly (horizontally, anyway) jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // We have to manually attach the row headers, but after that, the // scroll // pane keeps them in sync JScrollPane jsp = new JScrollPane(jt); jsp.setRowHeader(jv); jsp.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, headerColumn.getTableHeader()); getContentPane().add(jsp, BorderLayout.CENTER); }
From source file:SortableTable.java
/** * Returns the renderer component./*from w ww .j av a2 s . c o m*/ * * @param table the table. * @param value the value. * @param isSelected selected? * @param hasFocus focussed? * @param row the row. * @param column the column. * @return the renderer. */ public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) { if (table == null) { throw new NullPointerException("Table must not be null."); } final JComponent component; final SortableTableModel model = (SortableTableModel) table.getModel(); final int cc = table.convertColumnIndexToModel(column); final boolean isSorting = (model.getSortingColumn() == cc); final boolean isAscending = model.isAscending(); final JTableHeader header = table.getTableHeader(); final boolean isPressed = (cc == this.pressedColumn); if (this.useLabels) { final JLabel label = getRendererLabel(isSorting, isAscending); label.setText((value == null) ? "" : value.toString()); component = label; } else { final JButton button = getRendererButton(isSorting, isAscending); button.setText((value == null) ? "" : value.toString()); button.getModel().setPressed(isPressed); button.getModel().setArmed(isPressed); component = button; } if (header != null) { component.setForeground(header.getForeground()); component.setBackground(header.getBackground()); component.setFont(header.getFont()); } return component; }
From source file:com.aw.swing.mvp.binding.component.BndSJTable.java
public void resetHeader() { TableColumnModel colModel = jTable.getColumnModel(); int colsNumber = getColumnsInfo().length; int colsModel = colModel.getColumnCount() + fixedColumns; if (colsNumber == colsModel) { if (getSelectorColumn() != null) { SwingUtilities.invokeLater(new Runnable() { public void run() { JTable fixedJTable = (JTable) jTable.getClientProperty(CP_FIXED_TABLE); if (fixedJTable != null) { fixedJTable.getTableHeader().repaint(); } else { jTable.getTableHeader().repaint(); }/* w w w. j a v a 2 s. c o m*/ } }); } return; } if (colsNumber > colsModel) { int delta = colsNumber - colModel.getColumnCount(); for (int i = 0; i < delta; i++) { colModel.addColumn(new TableColumn()); } } for (int i = 0; i < colsNumber; i++) { TableColumn column = colModel.getColumn(i); column.setHeaderValue(getColumnsInfo()[i].getColumnHeader()); } SwingUtilities.invokeLater(new Runnable() { public void run() { JTable fixedJTable = (JTable) jTable.getClientProperty(CP_FIXED_TABLE); if (fixedJTable != null) { fixedJTable.getTableHeader().repaint(); } jTable.getTableHeader().repaint(); } }); }
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; };/*from w w w .j av a2 s . c om*/ }; 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 *//*w w w . jav a 2 s .c om*/ 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.specify.tasks.subpane.wb.DataImportDialog.java
/** * Parses the given import xls file according to the users selection and creates/updates the * Preview table, showing the user how the import options effect the way the data will be * imported into the spreadsheet./*from w ww . jav a 2 s .c o m*/ * * @param table - the table to display the data * @return JTable - the table to display the data */ private JTable setXLSTableData(final JTable table) { int numRows = 0; int numCols = 0; String[] headers = {}; Vector<Vector<String>> tableDataVector = new Vector<Vector<String>>(); Vector<String> rowData = new Vector<String>(); Vector<String> headerVector = new Vector<String>(); DateWrapper scrDateFormat = AppPrefsCache.getDateWrapper("ui", "formatting", "scrdateformat"); try { log.debug("setXLSTableData - file - " + configXLS.getFile().toString()); InputStream input = new FileInputStream(configXLS.getFile()); POIFSFileSystem fs = new POIFSFileSystem(input); HSSFWorkbook workBook = new HSSFWorkbook(fs); HSSFSheet sheet = workBook.getSheetAt(0); Vector<Integer> badHeads = new Vector<Integer>(); Vector<Integer> emptyCols = new Vector<Integer>(); ((ConfigureXLS) config).checkHeadsAndCols(sheet, badHeads, emptyCols); if (badHeads.size() > 0 && doesFirstRowHaveHeaders) { if (table != null) { ((ConfigureXLS) config).showBadHeadingsMsg(badHeads, emptyCols, getTitle()); } this.doesFirstRowHaveHeaders = false; try { ignoreActions = true; this.containsHeaders.setSelected(false); } finally { ignoreActions = false; } if (table != null) { return table; } } boolean firstRow = true; //quick fix to prevent ".0" at end of catalog numbers etc NumberFormat nf = NumberFormat.getInstance(); nf.setMinimumFractionDigits(0); nf.setMaximumFractionDigits(20); nf.setGroupingUsed(false); //gets rid of commas int maxCols = 0; // Iterate over each row in the sheet Iterator<?> rows = sheet.rowIterator(); while (rows.hasNext()) { numCols = 0; rowData = new Vector<String>(); HSSFRow row = (HSSFRow) rows.next(); //log.debug(row.getLastCellNum()+" "+row.getPhysicalNumberOfCells()); int maxSize = Math.max(row.getPhysicalNumberOfCells(), row.getLastCellNum()); if (maxSize > maxCols) { maxCols = maxSize; } while (numCols < maxSize) { if (emptyCols.indexOf(new Integer(numCols)) == -1) { HSSFCell cell = row.getCell(numCols); String value = null; // if cell is blank, set value to "" if (cell == null) { value = ""; } else { int type = cell.getCellType(); switch (type) { case HSSFCell.CELL_TYPE_NUMERIC: // The best I can do at this point in the app is to guess if a // cell is a date. // Handle dates carefully while using HSSF. Excel stores all // dates as numbers, internally. // The only way to distinguish a date is by the formatting of // the cell. (If you // have ever formatted a cell containing a date in Excel, you // will know what I mean.) // Therefore, for a cell containing a date, cell.getCellType() // will return // HSSFCell.CELL_TYPE_NUMERIC. However, you can use a utility // function, // HSSFDateUtil.isCellDateFormatted(cell), to check if the cell // can be a date. // This function checks the format against a few internal // formats to decide the issue, // but by its very nature it is prone to false negatives. if (HSSFDateUtil.isCellDateFormatted(cell)) { value = scrDateFormat.getSimpleDateFormat().format(cell.getDateCellValue()); //value = scrDateFormat.getSimpleDateFormat().format(cell.getDateCellValue()); } else { double numeric = cell.getNumericCellValue(); value = nf.format(numeric); } break; case HSSFCell.CELL_TYPE_STRING: value = cell.getRichStringCellValue().getString(); break; case HSSFCell.CELL_TYPE_BLANK: value = ""; break; case HSSFCell.CELL_TYPE_BOOLEAN: value = Boolean.toString(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_FORMULA: value = UIRegistry.getResourceString("WB_FORMULA_IMPORT_NO_PREVIEW"); break; default: value = ""; log.error("unsuported cell type"); break; } } if (firstRow && doesFirstRowHaveHeaders) { checkUserColInfo(value, numCols); } if (isUserCol(numCols)) { rowData.add(value.toString()); } } numCols++; } if (doesFirstRowHaveHeaders && firstRow) { headerVector = rowData; headers = new String[rowData.size()]; } else if (!doesFirstRowHaveHeaders && firstRow) { //headers = createDummyHeaders(rowData.size()); tableDataVector.add(rowData); } else { tableDataVector.add(rowData); } firstRow = false; numRows++; } maxCols -= emptyCols.size(); if (!doesFirstRowHaveHeaders) { headerVector = createDummyHeadersAsVector(maxCols); headers = new String[maxCols]; } for (int i = 0; i < headerVector.size(); i++) { headers[i] = headerVector.elementAt(i); } printArray(headers); String[][] tableData = new String[tableDataVector.size()][maxCols]; for (int i = 0; i < tableDataVector.size(); i++) { Vector<String> v = tableDataVector.get(i); for (int j = 0; j < v.size(); j++) { tableData[i][j] = v.get(j).toString(); } } if (checkForErrors(headers, tableData)) { errorPanel.showDataImportStatusPanel(true); } else { errorPanel.showDataImportStatusPanel(false); } if ((doesFirstRowHaveHeaders ? numRows - 1 : numRows) > WorkbenchTask.MAX_ROWS) { hasTooManyRows = true; showTooManyRowsErrorDialog(); } else { hasTooManyRows = false; } log.debug(headers); log.debug(tableData); model = new PreviewTableModel(headers, tableData); JTable result = null; if (table == null) { result = new JTable(); result.setColumnSelectionAllowed(false); result.setRowSelectionAllowed(false); result.setCellSelectionEnabled(false); result.getTableHeader().setReorderingAllowed(false); result.setPreferredScrollableViewportSize(new Dimension(500, 100)); result.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); } else { result = table; } result.setModel(model); result.setDefaultRenderer(String.class, new BiColorTableCellRenderer(false)); model.fireTableDataChanged(); model.fireTableStructureChanged(); return result; } catch (Exception ex) { UIRegistry.displayErrorDlgLocalized(UIRegistry.getResourceString("WB_ERROR_READING_IMPORT_FILE")); if (table != null) { String[] columnNames = {}; String[][] blankData = { {} }; model = new PreviewTableModel(columnNames, blankData); table.setModel(model); table.setColumnSelectionAllowed(false); table.setRowSelectionAllowed(false); table.setCellSelectionEnabled(false); table.getTableHeader().setReorderingAllowed(false); table.setPreferredScrollableViewportSize(new Dimension(500, 100)); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.setDefaultRenderer(String.class, new BiColorTableCellRenderer(false)); model.fireTableDataChanged(); model.fireTableStructureChanged(); return table; } //log.error("Error attempting to parse input xls file:" + ex); //ex.printStackTrace(); } return null; }
From source file:org.zaproxy.zap.extension.multiFuzz.impl.http.HttpFuzzResultDialog.java
@Override public JXTreeTable getTable() { if (table == null) { if (model == null) { model = new HttpFuzzTableModel(); }//ww w .j a v a2 s. co m table = new JXTreeTable(model); table.setDoubleBuffered(true); table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION); table.setName("HttpFuzzResultTable"); table.setFont(new java.awt.Font("Default", java.awt.Font.PLAIN, 12)); table.setDefaultRenderer(Pair.class, new IconTableCellRenderer()); int[] widths = { 10, 25, 550, 30, 85, 55, 40, 70 }; for (int i = 0, count = widths.length; i < count; i++) { TableColumn column = table.getColumnModel().getColumn(i); column.setPreferredWidth(widths[i]); } table.addMouseListener(new java.awt.event.MouseAdapter() { @Override public void mousePressed(java.awt.event.MouseEvent e) { showPopupMenuIfTriggered(e); } @Override public void mouseReleased(java.awt.event.MouseEvent e) { showPopupMenuIfTriggered(e); } private void showPopupMenuIfTriggered(java.awt.event.MouseEvent e) { if (e.isPopupTrigger()) { if (e.isPopupTrigger()) { // Select list item on right click JTable table = (JTable) e.getSource(); int row = table.rowAtPoint(e.getPoint()); if (!table.isRowSelected(row)) { table.changeSelection(row, 0, false, false); } View.getSingleton().getPopupMenu().show(e.getComponent(), e.getX(), e.getY()); } } } }); table.getSelectionModel().addListSelectionListener(new javax.swing.event.ListSelectionListener() { @Override public void valueChanged(javax.swing.event.ListSelectionEvent e) { if (!e.getValueIsAdjusting()) { if (table.getSelectedRowCount() == 0) { return; } final int row = table.getSelectedRow(); if (getEntry(row) instanceof HttpFuzzRequestRecord) { final HistoryReference historyReference = ((HttpFuzzRequestRecord) getEntry(row)) .getHistory(); try { getMessageInspection().setMessage(historyReference.getHttpMessage()); } catch (HttpMalformedHeaderException | SQLException ex) { logger.error(ex.getMessage(), ex); } } updateValues(); redrawDiagrams(); } } }); } table.getTableHeader().addMouseListener(new MouseListener() { int sortedOn = -1; @Override public void mouseReleased(MouseEvent arg0) { } @Override public void mousePressed(MouseEvent arg0) { } @Override public void mouseExited(MouseEvent arg0) { } @Override public void mouseEntered(MouseEvent arg0) { } @Override public void mouseClicked(MouseEvent e) { int index = table.columnAtPoint(e.getPoint()); List<HttpFuzzRecord> list = model.getEntries(); if (list.size() == 0) { return; } HttpFuzzRecordComparator comp = new HttpFuzzRecordComparator(); comp.setFeature(index); if (index == sortedOn) { Collections.sort(list, comp); Collections.reverse(list); sortedOn = -1; } else { Collections.sort(list, comp); sortedOn = index; } table.updateUI(); } }); table.setRootVisible(false); table.setVisible(true); return table; }