@Override public void actionPerformed(ActionEvent e) { JCheckBoxMenuItem check = (JCheckBoxMenuItem) e.getSource(); // Do not allow the last column to be hidden if (!check.isSelected() && tcm.getColumnCount(true) == 1) { return; } tcm.setColumnVisible(tc, check.isSelected()); }
protected void showTableHeaderPopup(MouseEvent e) { JPopupMenu popupMenu = new JPopupMenu(); for (int i = 0; i < columnModel.getColumnCount(false); i++) { TableColumn tc = columnModel.getColumnByModelIndex(i); JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem( dataTable.getModel().getColumnName(i), columnModel.isColumnVisible(tc)); menuItem.addActionListener(new headerActionListener(tc)); popupMenu.add(menuItem); } popupMenu.show(e.getComponent(), e.getX(), e.getY()); }
protected void showTableHeaderPopup(MouseEvent e, JTable table) { JPopupMenu popupMenu = new JPopupMenu(); XTableColumnModel tcm = (XTableColumnModel) table.getColumnModel(); for (int i = 0; i < tcm.getColumnCount(false); i++) { TableColumn tc = tcm.getColumnByModelIndex(i); String columnName = table.getModel().getColumnName(i); if (columnName != null && !columnName.equals("")) { JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(table.getModel().getColumnName(i), tcm.isColumnVisible(tc)); menuItem.addActionListener(new headerActionListener(tc, tcm)); popupMenu.add(menuItem); } } popupMenu.show(e.getComponent(), e.getX(), e.getY()); }
public final void resetColumnWidths() { Enumeration<TableColumn> en = columnModel.getColumns(false); while (en.hasMoreElements()) { TableColumn tc = en.nextElement(); int width = dataModel.getPreferredWidth(tc.getModelIndex()); tc.setPreferredWidth(width); } dataTable.sizeColumnsToFit(-1); }
@Override public String getColumnName(int col) { switch (col) { case IDCOL: return Bundle.getMessage("FieldID"); case ADDRESSCOL: return Bundle.getMessage("FieldDCCAddress"); case DECODERCOL: return Bundle.getMessage("FieldDecoderModel"); case MODELCOL: return Bundle.getMessage("FieldModel"); case ROADNAMECOL: return Bundle.getMessage("FieldRoadName"); case ROADNUMBERCOL: return Bundle.getMessage("FieldRoadNumber"); case MFGCOL: return Bundle.getMessage("FieldManufacturer"); case ICONCOL: return Bundle.getMessage("FieldIcon"); case OWNERCOL: return Bundle.getMessage("FieldOwner"); case DATEUPDATECOL: return Bundle.getMessage("FieldDateUpdated"); case PROTOCOL: return Bundle.getMessage("FieldProtocol"); default: break; } if (_tcm != null) { TableColumn tc = _tcm.getColumnByModelIndex(col); if (tc != null) { return (String) tc.getHeaderValue(); } } return "<UNKNOWN>"; // NOI18N }
public RosterTable(boolean editable, int selectionMode) { super(); dataModel = new RosterTableModel(editable); sorter = new TableSorter(dataModel); dataTable = new JTable(sorter); sorter.setTableHeader(dataTable.getTableHeader()); dataScroll = new JScrollPane(dataTable); // set initial sort TableSorter tmodel = ((TableSorter) dataTable.getModel()); tmodel.setSortingStatus(RosterTableModel.ADDRESSCOL, TableSorter.ASCENDING); // Use a "Numeric, if not, Alphanumeric" comparator tmodel.setColumnComparator(String.class, new jmri.util.PreferNumericComparator()); // allow reordering of the columns dataTable.getTableHeader().setReorderingAllowed(true); // have to shut off autoResizeMode to get horizontal scroll to work (JavaSwing p 541) dataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); dataTable.setColumnModel(columnModel); dataModel.setColumnModel(columnModel); dataTable.createDefaultColumnsFromModel(); dataTable.setAutoCreateColumnsFromModel(false); TableColumn tc = columnModel.getColumnByModelIndex(RosterTableModel.PROTOCOL); columnModel.setColumnVisible(tc, false); for (String s : Roster.instance().getAllAttributeKeys()) { if (!s.contains("RosterGroup") && !s.toLowerCase().startsWith("sys") && !s.toUpperCase().startsWith("VSD")) { // NOI18N String[] r = s.split("(?=\\p{Lu})"); // NOI18N StringBuilder sb = new StringBuilder(); sb.append(r[0].trim()); // System.out.println("'"+r[0]+","); for (int j = 1; j < r.length; j++) { // System.out.println("'"+r[j]+","); sb.append(" "); sb.append(r[j].trim()); } TableColumn c = new TableColumn(dataModel.getColumnCount()); c.setHeaderValue((sb.toString()).trim()); dataTable.addColumn(c); dataModel.addColumn(c.getHeaderValue()); columnModel.setColumnVisible(c, false); } } // resize columns as requested resetColumnWidths(); // general GUI config setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); // install items in GUI add(dataScroll); // set Viewport preferred size from size of table java.awt.Dimension dataTableSize = dataTable.getPreferredSize(); // width is right, but if table is empty, it's not high // enough to reserve much space. dataTableSize.height = Math.max(dataTableSize.height, 400); dataTableSize.width = Math.max(dataTableSize.width, 400); dataScroll.getViewport().setPreferredSize(dataTableSize); dataTable.setSelectionMode(selectionMode); MouseListener mouseHeaderListener = new tableHeaderListener(); dataTable.getTableHeader().addMouseListener(mouseHeaderListener); dataTable.setDefaultEditor(Object.class, new RosterCellEditor()); dataTable .getSelectionModel() .addListSelectionListener( tableSelectionListener = new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { if (!e.getValueIsAdjusting()) { selectedRosterEntries = null; // clear cached list of selections if (dataTable.getSelectedRowCount() == 1) { re = Roster.instance() .getEntryForId( sorter .getValueAt( dataTable.getSelectedRow(), RosterTableModel.IDCOL) .toString()); } else if (dataTable.getSelectedRowCount() > 1) { re = null; } // leave last selected item visible if no selection } else if (e.getFirstIndex() == -1) { // A reorder of the table might of occured therefore we are going to make sure // that the selected item is still in view moveTableViewToSelected(); } } }); }