// Initialize JTable private void initTable() { DialogTableModel tblModel = new DialogTableModel(students); studentsTbl.setModel(tblModel); studentsTbl.setAutoCreateRowSorter(true); studentsTbl.setRowSelectionAllowed(true); studentsTbl.getRowSorter().toggleSortOrder(1); studentsTbl.setGridColor(Color.gray); studentsTbl.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); studentsTbl.setRowHeight(22); TableColumn tc = studentsTbl.getColumnModel().getColumn(0); tc.setCellEditor(studentsTbl.getDefaultEditor(Boolean.class)); tc.setCellRenderer(studentsTbl.getDefaultRenderer(Boolean.class)); tc.setHeaderRenderer( new CheckBoxTableHeader( new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { Object source = e.getSource(); if (source instanceof AbstractButton == false) { return; } boolean checked = e.getStateChange() == ItemEvent.SELECTED; for (int x = 0, y = studentsTbl.getRowCount(); x < y; x++) { studentsTbl.setValueAt(new Boolean(checked), x, 0); } } })); }
public void setColumnWidths() { // See // "http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#custom" int n = getModel().getColumnCount(); for (int j = 0; j < n; ++j) { TableColumn column = getColumnModel().getColumn(j); TableCellRenderer headerRenderer = column.getHeaderRenderer(); if (headerRenderer == null) headerRenderer = getTableHeader().getDefaultRenderer(); // the new 1.3 way Component columnComponent = headerRenderer.getTableCellRendererComponent( this, column.getHeaderValue(), false, false, -1, j); Component cellComponent = getDefaultRenderer(getColumnClass(j)) .getTableCellRendererComponent(this, getModel().getValueAt(0, j), false, false, 0, j); int wantWidth = Math.max( columnComponent.getPreferredSize().width + 10, // fudge factor ... seems to always be too small otherwise (on x86 Linux) cellComponent.getPreferredSize().width + 10 // fudge factor ... seems to always be too small otherwise (on Mac OS X) ); column.setPreferredWidth(wantWidth); } }
public void setMetricsResults(MetricDisplaySpecification displaySpecification, MetricsRun run) { final MetricCategory[] categories = MetricCategory.values(); for (final MetricCategory category : categories) { final JTable table = tables.get(category); final String type = MetricsCategoryNameUtil.getShortNameForCategory(category); final MetricTableSpecification tableSpecification = displaySpecification.getSpecification(category); final MetricsResult results = run.getResultsForCategory(category); final MetricTableModel model = new MetricTableModel(results, type, tableSpecification); table.setModel(model); final Container tab = table.getParent().getParent(); if (model.getRowCount() == 0) { tabbedPane.remove(tab); continue; } final String longName = MetricsCategoryNameUtil.getLongNameForCategory(category); tabbedPane.add(tab, longName); final MyColumnListener columnListener = new MyColumnListener(tableSpecification, table); final TableColumnModel columnModel = table.getColumnModel(); columnModel.addColumnModelListener(columnListener); final int columnCount = columnModel.getColumnCount(); for (int i = 0; i < columnCount; i++) { final TableColumn column = columnModel.getColumn(i); column.addPropertyChangeListener(columnListener); } setRenderers(table, type); setColumnWidths(table, tableSpecification); } }
/** * This method implements the TableModelListener * * @param e The event to listen for. */ @Override public void tableChanged(TableModelEvent e) { if (!isColumnDataIncluded) { return; } // A cell has been updated if (e.getType() == TableModelEvent.UPDATE) { int column = table.convertColumnIndexToView(e.getColumn()); // Only need to worry about an increase in width for this cell if (isOnlyAdjustLarger) { int row = e.getFirstRow(); TableColumn tableColumn = table.getColumnModel().getColumn(column); if (tableColumn.getResizable()) { int width = getCellDataWidth(row, column); updateTableColumn(column, width); } } // Could be an increase of decrease so check all rows else { adjustColumn(column); } } // The update affected more than one column so adjust all columns else { adjustColumns(); } }
private void initializeFilteredPersonnelTableView() { personnelNameColumn.setCellValueFactory(new PropertyValueFactory<Official, String>("name")); personnelRankColumn.setCellValueFactory( new Callback<TableColumn.CellDataFeatures<Official, String>, ObservableValue<String>>() { @Override public ObservableValue<String> call(CellDataFeatures<Official, String> arg0) { final Official official = arg0.getValue(); final StringExpression concat = Bindings.selectString(official.rankProperty(), "designation"); return concat; } }); Bindings.bindContent(officialsFilteredTableView.getItems(), officialFilteredList); gameData .getOfficials() .addListener( new ListChangeListener<Official>() { @Override public void onChanged(Change<? extends Official> arg0) { updateOfficialFiltersResult(); } }); // TODO initialize assignOfficialMenuItem to be sure that it's not // enable if not correct values are selected }
/** * Create the priority column, which holds the priority of a filter and also creates a callback to * the integer property behind it. */ private void setUpPriorityColumn(TableView<FilterInput> tableView) { // Set up priority column final TableColumn<FilterInput, Integer> priorityColumn = new TableColumn<>("Priority"); priorityColumn.setMinWidth(50); priorityColumn.setPrefWidth(50); tableView.getColumns().add(priorityColumn); priorityColumn.setCellValueFactory(p -> p.getValue().getPriorityProperty().asObject()); }
/** * Create the legalality column, which holds the legality of a filter and also creates a callback * to the string property behind it. */ private void setUpLegalityColumn(TableView<FilterInput> tableView) { // Set up priority column final TableColumn<FilterInput, Boolean> legalColumn = new TableColumn<>("Authorized"); legalColumn.setMinWidth(70); legalColumn.setPrefWidth(70); tableView.getColumns().add(legalColumn); legalColumn.setCellValueFactory(p -> p.getValue().getLegalProperty()); }
/** * Create the origin column, which holds the type of the origin and also creates a callback to the * string property behind it. */ private void setUpOriginColumn(TableView<FilterInput> tableView) { // Set up origin column final TableColumn<FilterInput, String> originColumn = new TableColumn<>("Filtered By"); originColumn.setMinWidth(90); originColumn.setPrefWidth(90); tableView.getColumns().add(originColumn); originColumn.setCellValueFactory(p -> p.getValue().getFilterTypeProperty()); }
/** * Create the type column, which holds the type of the filter and also creates a callback to the * string property behind it. */ private void setUpTypeColumn(TableView<FilterInput> tableView) { // Set up type column final TableColumn<FilterInput, String> typeColumn = new TableColumn<>("Selection Model"); typeColumn.setMinWidth(90); typeColumn.setPrefWidth(90); tableView.getColumns().add(typeColumn); typeColumn.setCellValueFactory(p -> p.getValue().getSelectionModelProperty()); }
/** * Create the filter column, which holds the name of the filter and also creates a callback to the * string property behind it. */ private void setUpFilterColumn(TableView<FilterInput> tableView) { // Set up filter column final TableColumn<FilterInput, String> filterColumn = new TableColumn<>("Filter"); filterColumn.setMinWidth(120); filterColumn.setPrefWidth(120); tableView.getColumns().add(filterColumn); filterColumn.setCellValueFactory(p -> p.getValue().getNameProperty()); }
// Determines the width of each column in the table. protected void determineColumnWidth() { TableColumn column = null; for (int i = 0; i < 2; i++) { column = pinsTable.getColumnModel().getColumn(i); if (i == 0) column.setPreferredWidth(20); else if (i == 1) column.setPreferredWidth(20); else if (i == 2) column.setPreferredWidth(180); } }
/** * This method restores the width of the specified column to its previous width. * * @param column The column to restore. */ private void restoreColumn(int column) { TableColumn tableColumn = table.getColumnModel().getColumn(column); Integer width = columnSizes.get(tableColumn); if (width != null) { table.getTableHeader().setResizingColumn(tableColumn); tableColumn.setWidth(width.intValue()); } }
/** * Establishes combo box editor for 'from onramp' column. * * @param clmn */ private void setUpToOnrampColumn() { JComboBox combo = new JComboBox(); Vector<AbstractNetworkElement> nes = ((AbstractControllerComplex) controller).getMyMonitor().getSuccessors(); for (int i = 0; i < nes.size(); i++) if ((nes.get(i).getType() & TypesHWC.MASK_LINK) > 0) combo.addItem(nes.get(i)); TableColumn clmn = zonetab.getColumnModel().getColumn(3); clmn.setCellEditor(new DefaultCellEditor(combo)); clmn.setCellRenderer(new DefaultTableCellRenderer()); return; }
public Shell open(Display display) { shell = new Shell(display); shell.setLayout(new FillLayout()); shell.addShellListener( new ShellAdapter() { @Override public void shellClosed(ShellEvent e) { e.doit = closeAddressBook(); } }); createMenuBar(); searchDialog = new SearchDialog(shell); searchDialog.setSearchAreaNames(columnNames); searchDialog.setSearchAreaLabel(resAddressBook.getString("Column")); searchDialog.addFindListener( new FindListener() { public boolean find() { return findEntry(); } }); table = new Table(shell, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION); table.setHeaderVisible(true); table.setMenu(createPopUpMenu()); table.addSelectionListener( new SelectionAdapter() { @Override public void widgetDefaultSelected(SelectionEvent e) { TableItem[] items = table.getSelection(); if (items.length > 0) editEntry(items[0]); } }); for (int i = 0; i < columnNames.length; i++) { TableColumn column = new TableColumn(table, SWT.NONE); column.setText(columnNames[i]); column.setWidth(150); final int columnIndex = i; column.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { sort(columnIndex); } }); } newAddressBook(); shell.setSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300); shell.open(); return shell; }
/* * Adjust the width of the specified column in the table */ public void adjustColumn(final int column) { TableColumn tableColumn = table.getColumnModel().getColumn(column); if (!tableColumn.getResizable()) return; int columnHeaderWidth = getColumnHeaderWidth(column); int columnDataWidth = getColumnDataWidth(column); int preferredWidth = Math.max(columnHeaderWidth, columnDataWidth); updateTableColumn(column, preferredWidth); }
public void setFddObjectModel(FddObjectModel fddObjectModel) { logger.entry(); if (fddObjectModel != null) { fddObjectModel .getInteractionClasses() .values() .stream() .forEach( (value) -> { interactions.add(new InteractionState(value)); }); InteractionTableView.setItems(interactions); interactions.forEach( (interaction) -> { interaction .onProperty() .addListener( (observable, oldValue, newValue) -> { if (!newValue) { cb.setSelected(false); } else if (interactions.stream().allMatch(a -> a.isOn())) { cb.setSelected(true); } }); }); InteractionTableColumn.setCellValueFactory(new PropertyValueFactory<>("interactionName")); CheckTableColumn.setCellValueFactory( new Callback< TableColumn.CellDataFeatures<InteractionState, Boolean>, ObservableValue<Boolean>>() { @Override public ObservableValue<Boolean> call( TableColumn.CellDataFeatures<InteractionState, Boolean> param) { return param.getValue().onProperty(); } }); CheckTableColumn.setCellFactory(CheckBoxTableCell.forTableColumn(CheckTableColumn)); cb.setUserData(CheckTableColumn); cb.setOnAction( (ActionEvent event) -> { CheckBox cb1 = (CheckBox) event.getSource(); TableColumn tc = (TableColumn) cb1.getUserData(); InteractionTableView.getItems() .stream() .forEach( (item) -> { item.setOn(cb1.isSelected()); }); }); CheckTableColumn.setGraphic(cb); } logger.exit(); }
// Determines the width of each column in the table. protected void determineColumnWidth() { if (segmentTable.getColumnCount() == 2) { TableColumn column = null; for (int i = 0; i < 2; i++) { column = segmentTable.getColumnModel().getColumn(i); if (i == 0) { column.setPreferredWidth(30); } else { column.setPreferredWidth(100); } } } }
public MainPanel() { super(new BorderLayout()); table.setAutoCreateRowSorter(true); table.setSurrendersFocusOnKeystroke(true); table.setRowHeight(64); TableColumn c = table.getColumnModel().getColumn(1); c.setCellEditor(new TextAreaCellEditor()); c.setCellRenderer(new TextAreaCellRenderer()); add(new JScrollPane(table)); setPreferredSize(new Dimension(320, 240)); }
/* * Calculated the width based on the column name */ private int getColumnHeaderWidth(int column) { if (!isColumnHeaderIncluded) return 0; TableColumn tableColumn = table.getColumnModel().getColumn(column); Object value = tableColumn.getHeaderValue(); TableCellRenderer renderer = tableColumn.getHeaderRenderer(); if (renderer == null) { renderer = table.getTableHeader().getDefaultRenderer(); } Component c = renderer.getTableCellRendererComponent(table, value, false, false, -1, column); return c.getPreferredSize().width; }
private void saveColumnSpecification() { final TableColumnModel columnModel = table.getColumnModel(); final int numColumns = table.getColumnCount(); final List<String> columns = new ArrayList<String>(numColumns); final List<Integer> columnWidths = new ArrayList<Integer>(numColumns); for (int i = 0; i < numColumns; i++) { final String columnName = table.getColumnName(i); columns.add(columnName); final TableColumn column = columnModel.getColumn(i); final int columnWidth = column.getWidth(); columnWidths.add(Integer.valueOf(columnWidth)); } tableSpecification.setColumnOrder(columns); tableSpecification.setColumnWidths(columnWidths); MetricsProfileRepository.getInstance().persistCurrentProfile(); }
/* * Update the TableColumn with the newly calculated width */ private void updateTableColumn(int column, int width) { final TableColumn tableColumn = table.getColumnModel().getColumn(column); if (!tableColumn.getResizable()) return; width += spacing; // Don't shrink the column width if (isOnlyAdjustLarger) { width = Math.max(width, tableColumn.getPreferredWidth()); } columnSizes.put(tableColumn, new Integer(tableColumn.getWidth())); table.getTableHeader().setResizingColumn(tableColumn); tableColumn.setWidth(width); }
private void setupTracksView() { TableColumn titleColumn = new TableColumn("Title"); titleColumn.setSortable(false); titleColumn.setMinWidth(USE_COMPUTED_SIZE); titleColumn.setCellValueFactory(new PropertyValueFactory<>("title")); TableColumn durationColumn = new TableColumn("Duration"); durationColumn.setMinWidth(60); durationColumn.setMaxWidth(60); durationColumn.setSortable(false); durationColumn.setCellValueFactory(new PropertyValueFactory<>("durationFormatted")); durationColumn.getStyleClass().add("text-layout-center"); tracksView.getColumns().clear(); tracksView.getColumns().addAll(titleColumn, durationColumn); tracksView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); }
private void setupPlaylistsView() { TableColumn titleColumn = new TableColumn("Title"); titleColumn.setMinWidth(USE_COMPUTED_SIZE); titleColumn.setSortable(false); titleColumn.setCellValueFactory(new PropertyValueFactory<>("title")); TableColumn countColumn = new TableColumn("Tracks"); countColumn.setMinWidth(60); countColumn.setMaxWidth(60); countColumn.setSortable(false); countColumn.setCellValueFactory(new PropertyValueFactory<>("count")); countColumn.getStyleClass().add("text-layout-right"); playlistsView.getColumns().clear(); playlistsView.getColumns().addAll(titleColumn, countColumn); playlistsView.setContextMenu(playlistsContextMenu); playlistsView.setPlaceholder(playlistPlaceholder); playlistsView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); }
public void mouseClicked(MouseEvent e) { TableColumnModel colModel = table.getColumnModel(); int columnModelIndex = colModel.getColumnIndexAtX(e.getX()); int modelIndex = colModel.getColumn(columnModelIndex).getModelIndex(); if (modelIndex < 0) return; if (sortCol == modelIndex) isSortAsc = !isSortAsc; else sortCol = modelIndex; for (int i = 0; i < colNames.length; i++) { TableColumn column = colModel.getColumn(i); column.setHeaderValue(getColumnName(column.getModelIndex())); } table.getTableHeader().repaint(); Collections.sort(rowData, new MyComparator(isSortAsc)); table.tableChanged(new TableModelEvent(MyTableModel.this)); table.repaint(); }
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); final Table table = new Table(shell, SWT.BORDER); table.setHeaderVisible(true); final TableColumn column1 = new TableColumn(table, SWT.NONE); column1.setText("Column 1"); final TableColumn column2 = new TableColumn(table, SWT.NONE); column2.setText("Column 2"); TableItem item = new TableItem(table, SWT.NONE); item.setText(new String[] {"a", "3"}); item = new TableItem(table, SWT.NONE); item.setText(new String[] {"b", "2"}); item = new TableItem(table, SWT.NONE); item.setText(new String[] {"c", "1"}); column1.setWidth(100); column2.setWidth(100); Listener sortListener = e -> { TableItem[] items = table.getItems(); Collator collator = Collator.getInstance(Locale.getDefault()); TableColumn column = (TableColumn) e.widget; int index = column == column1 ? 0 : 1; for (int i = 1; i < items.length; i++) { String value1 = items[i].getText(index); for (int j = 0; j < i; j++) { String value2 = items[j].getText(index); if (collator.compare(value1, value2) < 0) { String[] values = {items[i].getText(0), items[i].getText(1)}; items[i].dispose(); TableItem item1 = new TableItem(table, SWT.NONE, j); item1.setText(values); items = table.getItems(); break; } } } table.setSortColumn(column); }; column1.addListener(SWT.Selection, sortListener); column2.addListener(SWT.Selection, sortListener); table.setSortColumn(column1); table.setSortDirection(SWT.UP); shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
private static void setColumnWidths(JTable table, MetricTableSpecification tableSpecification) { final TableModel model = table.getModel(); final TableColumnModel columnModel = table.getColumnModel(); final List<Integer> columnWidths = tableSpecification.getColumnWidths(); final List<String> columnOrder = tableSpecification.getColumnOrder(); if (columnWidths != null && !columnWidths.isEmpty()) { final int columnCount = model.getColumnCount(); for (int i = 0; i < columnCount; i++) { final String columnName = model.getColumnName(i); final int index = columnOrder.indexOf(columnName); if (index != -1) { final Integer width = columnWidths.get(index); final TableColumn column = columnModel.getColumn(i); column.setPreferredWidth(width.intValue()); } } } else { final Graphics graphics = table.getGraphics(); final Font font = table.getFont(); final FontMetrics fontMetrics = table.getFontMetrics(font); final int rowCount = model.getRowCount(); int maxFirstColumnWidth = 100; for (int i = 0; i < rowCount; i++) { final String name = (String) model.getValueAt(i, 0); if (name != null) { final Rectangle2D stringBounds = fontMetrics.getStringBounds(name, graphics); final double stringWidth = stringBounds.getWidth(); if (stringWidth > maxFirstColumnWidth) { maxFirstColumnWidth = (int) stringWidth; } } } final int allocatedFirstColumnWidth = Math.min(300, maxFirstColumnWidth + 5); final TableColumn column = columnModel.getColumn(0); column.setPreferredWidth(allocatedFirstColumnWidth); } }
private JTable makeTable() { String empty = ""; String[] columnNames = {"String", "Button"}; Object[][] data = {{"AAA", empty}, {"CCC", empty}, {"BBB", empty}, {"ZZZ", empty}}; DefaultTableModel model = new DefaultTableModel(data, columnNames) { @Override public Class<?> getColumnClass(int column) { return getValueAt(0, column).getClass(); } }; final JTable table = new JTable(model); table.setRowHeight(36); table.setAutoCreateRowSorter(true); // table.addMouseListener(new CellButtonsMouseListener()); // ButtonsEditorRenderer er = new ButtonsEditorRenderer(table); TableColumn column = table.getColumnModel().getColumn(1); column.setCellRenderer(new ButtonsRenderer()); column.setCellEditor(new ButtonsEditor(table)); return table; }
private static void setRenderers(JTable table, String type) { final MetricTableModel model = (MetricTableModel) table.getModel(); final MetricInstance[] metrics = model.getMetricsInstances(); Arrays.sort(metrics, new MetricInstanceAbbreviationComparator()); final TableColumnModel columnModel = table.getColumnModel(); for (int i = 0; i < model.getColumnCount(); i++) { final String columnName = model.getColumnName(i); final TableColumn column = columnModel.getColumn(i); if (columnName.equals(type)) { column.setCellRenderer(new MetricCellRenderer(null)); column.setHeaderRenderer(new HeaderRenderer(null, model, SwingConstants.LEFT)); } else { final MetricInstance metricInstance = model.getMetricForColumn(i); final TableCellRenderer renderer = new MetricCellRenderer(metricInstance); column.setCellRenderer(renderer); final Metric metric = metricInstance.getMetric(); final String displayName = metric.getDisplayName(); column.setHeaderRenderer(new HeaderRenderer(displayName, model, SwingConstants.RIGHT)); } } }
/** * Create the active column, which holds the activity state and also creates a callback to the * string property behind it. */ private void setUpActiveColumn(TableView<FilterInput> tableView) { // Set up active column final TableColumn<FilterInput, Boolean> activeColumn = new TableColumn<>("Active"); activeColumn.setMinWidth(50); activeColumn.setPrefWidth(50); tableView.getColumns().add(activeColumn); activeColumn.setSortable(false); activeColumn.setCellFactory( CheckBoxTableCell.forTableColumn( (Callback<Integer, ObservableValue<Boolean>>) param -> { final FilterInput input = tableView.getItems().get(param); input .getActiveProperty() .addListener( l -> { notifyUpdateCommand(input); }); return input.getActiveProperty(); })); }
public void updateColumnSizes() { final JTableHeader header = getTableHeader(); final TableCellRenderer defaultRenderer = header == null ? null : header.getDefaultRenderer(); final RowSorter<? extends TableModel> sorter = getRowSorter(); final List<? extends RowSorter.SortKey> current = sorter == null ? null : sorter.getSortKeys(); ColumnInfo[] columns = getListTableModel().getColumnInfos(); for (int i = 0; i < columns.length; i++) { final ColumnInfo columnInfo = columns[i]; final TableColumn column = getColumnModel().getColumn(i); // hack to get sort arrow included into the renderer component if (sorter != null && columnInfo.isSortable()) { sorter.setSortKeys( Collections.singletonList(new RowSorter.SortKey(i, SortOrder.ASCENDING))); } final Component headerComponent = defaultRenderer == null ? null : defaultRenderer.getTableCellRendererComponent( this, column.getHeaderValue(), false, false, 0, 0); if (sorter != null && columnInfo.isSortable()) { sorter.setSortKeys(current); } final Dimension headerSize = headerComponent == null ? new Dimension(0, 0) : headerComponent.getPreferredSize(); final String maxStringValue; final String preferredValue; if (columnInfo.getWidth(this) > 0) { int width = columnInfo.getWidth(this); column.setMaxWidth(width); column.setPreferredWidth(width); column.setMinWidth(width); } else if ((maxStringValue = columnInfo.getMaxStringValue()) != null) { int width = getFontMetrics(getFont()).stringWidth(maxStringValue) + columnInfo.getAdditionalWidth(); width = Math.max(width, headerSize.width); column.setPreferredWidth(width); column.setMaxWidth(width); } else if ((preferredValue = columnInfo.getPreferredStringValue()) != null) { int width = getFontMetrics(getFont()).stringWidth(preferredValue) + columnInfo.getAdditionalWidth(); width = Math.max(width, headerSize.width); column.setPreferredWidth(width); } } }