// 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); } } })); }
/** * 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; }
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)); } } }
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)); }
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; }
public ProcessedModulesTable(final Project project) { super(new BorderLayout()); myTableModel = new MyTableModel(project); myTable = new JBTable(myTableModel); myTable.getEmptyText().setText("No modules configured"); // myTable.setShowGrid(false); myTable.setIntercellSpacing(JBUI.emptySize()); myTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); myTable.setColumnSelectionAllowed(false); final TableColumnModel columnModel = myTable.getColumnModel(); final TableColumn dirNameColumn = columnModel.getColumn(myTableModel.DIRNAME_COLUMN_INDEX); final String title = "Generated Sources Directory Name"; dirNameColumn.setHeaderValue(title); final JTableHeader tableHeader = myTable.getTableHeader(); final FontMetrics metrics = tableHeader.getFontMetrics(tableHeader.getFont()); final int preferredWidth = metrics.stringWidth(title) + 12; dirNameColumn.setPreferredWidth(preferredWidth); dirNameColumn.setMaxWidth(preferredWidth + 20); dirNameColumn.setCellRenderer(new MyElementColumnCellRenderer()); final TableColumn moduleColumn = columnModel.getColumn(myTableModel.ELEMENT_COLUMN_INDEX); moduleColumn.setHeaderValue("Module"); moduleColumn.setCellRenderer(new MyElementColumnCellRenderer()); final JPanel panel = ToolbarDecorator.createDecorator(myTable) .disableUpDownActions() .setPreferredSize(JBUI.size(100, 155)) .createPanel(); add(panel, BorderLayout.CENTER); final SpeedSearchBase<JBTable> speedSearch = new SpeedSearchBase<JBTable>(myTable) { public int getSelectedIndex() { return myTable.getSelectedRow(); } @Override protected int convertIndexToModel(int viewIndex) { return myTable.convertRowIndexToModel(viewIndex); } public Object[] getAllElements() { final int count = myTableModel.getRowCount(); Object[] elements = new Object[count]; for (int idx = 0; idx < count; idx++) { elements[idx] = myTableModel.getModuleAt(idx); } return elements; } public String getElementText(Object element) { return ((Module) element).getName() + " (" + FileUtil.toSystemDependentName(((Module) element).getModuleFilePath()) + ")"; } public void selectElement(Object element, String selectedText) { final int count = myTableModel.getRowCount(); for (int row = 0; row < count; row++) { if (element.equals(myTableModel.getModuleAt(row))) { final int viewRow = myTable.convertRowIndexToView(row); myTable.getSelectionModel().setSelectionInterval(viewRow, viewRow); TableUtil.scrollSelectionToVisible(myTable); break; } } } }; speedSearch.setComparator(new SpeedSearchComparator(false)); }
private ElementsChooser(@Nullable List<T> elements, boolean marked, boolean elementsCanBeMarked) { super(new BorderLayout()); myTableModel = new MyTableModel(elementsCanBeMarked); myTable = new Table(myTableModel); myTable.setShowGrid(false); myTable.setIntercellSpacing(new Dimension(0, 0)); myTable.setTableHeader(null); myTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); myTable.setColumnSelectionAllowed(false); JScrollPane pane = ScrollPaneFactory.createScrollPane(myTable); pane.setPreferredSize(new Dimension(100, 155)); int width = new JCheckBox().getPreferredSize().width; TableColumnModel columnModel = myTable.getColumnModel(); if (elementsCanBeMarked) { TableColumn checkMarkColumn = columnModel.getColumn(myTableModel.CHECK_MARK_COLUM_INDEX); checkMarkColumn.setPreferredWidth(width); checkMarkColumn.setMaxWidth(width); checkMarkColumn.setCellRenderer( new CheckMarkColumnCellRenderer(myTable.getDefaultRenderer(Boolean.class))); } columnModel .getColumn(myTableModel.ELEMENT_COLUMN_INDEX) .setCellRenderer(new MyElementColumnCellRenderer()); add(pane, BorderLayout.CENTER); myTable.registerKeyboardAction( new ActionListener() { public void actionPerformed(ActionEvent e) { final int[] selectedRows = myTable.getSelectedRows(); boolean currentlyMarked = true; for (int selectedRow : selectedRows) { currentlyMarked = myTableModel.isElementMarked(selectedRow); if (!currentlyMarked) { break; } } myTableModel.setMarked(selectedRows, !currentlyMarked); } }, KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), JComponent.WHEN_FOCUSED); final SpeedSearchBase<JBTable> speedSearch = new SpeedSearchBase<JBTable>(myTable) { public int getSelectedIndex() { return myTable.getSelectedRow(); } @Override protected int convertIndexToModel(int viewIndex) { return myTable.convertRowIndexToModel(viewIndex); } public Object[] getAllElements() { final int count = myTableModel.getRowCount(); Object[] elements = new Object[count]; for (int idx = 0; idx < count; idx++) { elements[idx] = myTableModel.getElementAt(idx); } return elements; } public String getElementText(Object element) { return getItemText((T) element); } public void selectElement(Object element, String selectedText) { final int count = myTableModel.getRowCount(); for (int row = 0; row < count; row++) { if (element.equals(myTableModel.getElementAt(row))) { final int viewRow = myTable.convertRowIndexToView(row); myTable.getSelectionModel().setSelectionInterval(viewRow, viewRow); TableUtil.scrollSelectionToVisible(myTable); break; } } } }; speedSearch.setComparator(new SpeedSearchBase.SpeedSearchComparator(false)); setElements(elements, marked); installActions(myTable); }