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 void dispose() { this.setRosterGroupSource(null); if (dataModel != null) { dataModel.dispose(); } dataModel = null; dataTable = null; dataScroll = null; super.dispose(); }
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(); } } }); }
@Override public String getSelectedRosterGroup() { return dataModel.getRosterGroup(); }