public static int getMaxColumnWidth(JTable table, int column) { // Set first to column header width as a minimum Font font = table.getFont(); FontMetrics metrics = table.getGraphics().getFontMetrics(font); String header = table.getColumnName(column) + " "; // Whitespace buffer as a crude way (hack) // to account for the rendering context Test.output("Column name: " + header); // Convert from string length in characters to pixels int widest = metrics.stringWidth(header) + (2 * table.getColumnModel().getColumnMargin()); Test.output("Starting widest value: " + header + " (" + widest + " pixels)"); // Now go through each row to see if there is a longer value in the column int rows = table.getRowCount(); Test.output("Row count: " + rows); for (int index = 0; index < rows; index++) { String cellValue = table.getValueAt(index, column).toString(); int cellWidth = metrics.stringWidth(cellValue) + (2 * table.getColumnModel().getColumnMargin()); if (cellWidth > widest) { widest = cellWidth; Test.output("New widest value: " + widest + " pixels"); } } return widest; }
public String getColumnName(final int _i) { if (column_ == null) { return t_.getColumnName(_i); } return t_.getColumnName(column_[_i]); }
public Object getElementAt(final int _index) { return table_.getColumnName(_index); }
private int getColumnByName(JTable table, String name) { for (int i = 0; i < table.getColumnCount(); ++i) if (table.getColumnName(i).equals(name)) return i; return -1; }
private int getColumnByName(JTable table, String name) { for (int i = 0; i < table.getColumnCount(); ++i) if (table.getColumnName(i).equals(name)) return i; return -1; }
public int getColumnIndex(String columnName) { for (int columnIndex = 0; columnIndex < jTable.getColumnCount(); columnIndex++) { if (jTable.getColumnName(columnIndex).equalsIgnoreCase(columnName)) { return columnIndex; } } java.util.List<String> names = new ArrayList<String>(); for (int columnIndex = 0; columnIndex < jTable.getColumnCount(); columnIndex++) { names.add(jTable.getColumnName(columnIndex)); } AssertAdapter.fail("Column '" + columnName + "' not found - actual names: " + names); return -1; }
String[] columnNames = {"First Name", "Last Name","Sport","# of Years"}; Object[][] data = { {"Kathy", "Smith", "Snowboarding", "5"}, {"John", "Doe", "Rowing", "2"}, {"Sue", "Black", "Knitting", "8"}, {"Jane", "White", "Speed reading", "10"}, {"Joe", "Brown", "Pool", "20"} }; JTable table = new JTable(data, columnNames); for(int row = 0; row < table.getRowCount(); row++) { for(int column = 0; column < table.getColumnCount(); column++) { System.out.print(table.getColumnName(column) + ": "); System.out.println(table.getValueAt(row, column)); } System.out.println(""); // Add line space }
/** * Set Column size */ private void setColumnWidths() { int margin = 10; int minwidth = 150; JTableHeader tableHeader = paramTable.getTableHeader(); FontMetrics headerFontMetrics = tableHeader.getFontMetrics(tableHeader.getFont()); for (int i = 0; i < tableModel.getColumnCount(); i++) { int headerWidth = headerFontMetrics.stringWidth(paramTable.getColumnName(i)); int maxWidth = getMaximalRequiredColumnWidth(i, headerWidth); paramTable.getColumnModel().getColumn(i).setPreferredWidth(Math.max(maxWidth + margin, minwidth)); } }
public String[] getColumnNames() { String[] columnNames = new String[jTable.getColumnCount()]; for (int columnIndex = 0; columnIndex < jTable.getColumnCount(); columnIndex++) { columnNames[columnIndex] = jTable.getColumnName(columnIndex); } return columnNames; }
@Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { columnName = table.getColumnName(column); if (value != null) { Blob blob = (Blob) value; dialog.setBytes(blob.getBytes()); button.setText(dialog.getContentDescription()); } else { button.setText(null); } return button; }
final JTable table = new JTable(tableModel); table.getTableHeader().addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent mouseEvent) { int selectedHeader = table.convertColumnIndexToModel(table .columnAtPoint(mouseEvent.getPoint())); table.getColumn(table.getColumnName(selectedHeader)) .setCellRenderer(new DefaultTableCellRenderer() { public void setBackground(Color c) { super.setBackground(Color.blue); } }); }; });
public int computePreferredWidth(final int _indexInView){ final JTable t = getTable(); final TableColumn c = t.getColumn(t.getColumnName(_indexInView)); return computePreferredWidth(t, c, _indexInView); }
@Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasfocus, int row, int column) { if (column > keys.length) { throw new IndexOutOfBoundsException("colum can not be greater than " + keys.length); } TableColumn col = table.getColumn(table.getColumnName(column)); int index = col.getModelIndex(); value = _(keys[index]); JComponent rendererComponent = (JComponent) delegate.getTableCellRendererComponent(table, value, isSelected, hasfocus, row, column); rendererComponent.setToolTipText(_(tips[index])); return rendererComponent; }
int nameColNo = getColumnIndex(table, "Name"); String name = (String)table.getValueAt(rowNo, nameColNo); private int getColumnIndex (JTable table, String header) { for (int i=0; i < table.getColumnCount(); i++) { if (table.getColumnName(i).equals(header)) return i; } return -1; }
public String getAccessibleName() { AccessibleContext ac = getCurrentAccessibleContext(); if (ac != null) { String name = ac.getAccessibleName(); if ((name != null) && (name != "")) { //$NON-NLS-1$ return ac.getAccessibleName(); } } if ((accessibleName != null) && (accessibleName != "")) { //$NON-NLS-1$ return accessibleName; } else { return table.getColumnName(column); } }
@Test public void testBucketEditorColumnHeaders() { initEditorWithBlobs(directoryBlob); JTable bucketTable = editorPanel.getBucketContentTable(); assertThat(bucketTable.getColumnCount()).isEqualTo(4); IntStream.range(0, bucketTable.getColumnCount()) .forEach( colIdx -> assertThat(INDEX_TO_COL_NAME.get(colIdx)) .isEqualTo(bucketTable.getColumnName(colIdx))); }
public void check() { checkHeader(); try { AssertAdapter.assertEquals(expectedHeaders.length, jTable.getColumnCount()); for (int i = 0; i < expectedHeaders.length; i++) { AssertAdapter.assertEquals(expectedHeaders[i], jTable.getColumnName(i)); } } catch (Error e) { AssertAdapter.assertEquals(ArrayUtils.toString(expectedHeaders), ArrayUtils.toString(getColumnNames())); throw e; } } };
public void check() { checkHeader(); try { AssertAdapter.assertTrue(countHeaderToCheck <= jTable.getColumnCount()); for (int i = 0; i < expectedHeaders.length; i++) { AssertAdapter.assertEquals(expectedHeaders[i], jTable.getColumnName(i)); } } catch (Error e) { AssertAdapter.assertEquals(ArrayUtils.toString(expectedHeaders), ArrayUtils.toString(getColumnNames())); throw e; } } };
private void selectVisibleColumns() { setCurrentWidths(); String viewName = null; if ( getParent() != null ) viewName = getParent().getName(); if ( tableModel.selectVisibleColumns( viewName, treeTable.getColumnName(0), getSortedNodeTreeModel ().getRootDescription() ) ) { if ( tableModel.getVisibleSortingColumn() == -1 ) getSortedNodeTreeModel ().setSortedByProperty( null ); setTreePreferredWidth( treeColumnWidth ); for (int i=0; i < tableModel.getColumnCount(); i++) { setTableColumnPreferredWidth( tableModel.getArrayIndex( i ), tableModel.getVisibleColumnWidth( i ) ); } } }
private void selectVisibleColumns() { setCurrentWidths(); String viewName = null; if ( getParent() != null ) viewName = getParent().getName(); if ( tableModel.selectVisibleColumns( viewName, treeTable.getColumnName(0), getSortedNodeTreeModel ().getRootDescription() ) ) { if ( tableModel.getVisibleSortingColumn() == -1 ) getSortedNodeTreeModel ().setSortedByProperty( null ); setTreePreferredWidth( treeColumnWidth ); for (int i=0; i < tableModel.getColumnCount(); i++) { setTableColumnPreferredWidth( tableModel.getArrayIndex( i ), tableModel.getVisibleColumnWidth( i ) ); } } }