SR-JRC | a java richclient framework |
| create a tableview for entities Strictly speaking, the list view is not just a view, but a view controller, but we don't want to be ultramontane - after all, the "big" user actions stay in the controller (our application class). Our list view will be derived from public class AddressTableView extends AbstractTableView<Addresses> { protected static final String PREFIX = AddressesTableView.class.getSimpleName(); protected static RendererFactory rf; public AddressTableView(List<Addresses> list, int selectionMode, List<Addresses> initialSelection) { super(list, selectionMode, initialSelection); setTableFormat(new AddressTableFormat()); setSelectionMode(selectionMode); } @Override protected JComponent buildPanel() { return createTable(); } } Simple list views might return If you're looking for higher sofisticated samples, take a look at the project VdrAssistant - the mother of SRJRCFrames. There you'll find more list views and complex filtered lists (i.e. EpgManager). As indicated by the constructor above, even a simple list view needs a protected class AddressTableFormat extends AbstractTableFormat { private final String[] columnNames = { "type", "city", "street", "zip", "state", "country" }; private final int[] width = { 20, 200, 200, 90, 150, 150 }; public AddressTableFormat() { super(PREFIX); } @Override protected String[] getColumnNames() { return columnNames; } @Override protected int getColumnWidth(int idx) { if (idx < width.length) return width[idx]; return 0; } @Override protected int getMaxColWidth(int idx) { if (idx < width.length) return width[idx]; return 0; } @Override protected void setColumnRenderer(TableColumn tc, int colIndex) { if (colIndex == 0) tc.setCellRenderer(rf.getEnumRenderer(JLabel.RIGHT)); } @Override protected void setTypeRenderers(JTable table) { // ask for factory service here rf = (RendererFactory) ApplicationServiceProvider.getService(RendererFactory.class); } @Override public Object getColumnValue(Addresses instance, int idx) { switch (idx) { case 0: return instance.getType(); case 1: return instance.getCity(); case 2: return instance.getStreet(); case 3: return instance.getZip(); case 4: return instance.getState(); case 5: return instance.getCountry(); } return null; } }
Filters are used to reduce the number of displayed list entries, without database access. Glazedlists provides this functionality out of the box so the effort of high sofisticated list views is negligible. A filter consists of an form component, where the user may change the filtercriteria (of cause, we will use JGoodies for that). Then you'll need a class, that performs the list filtering and last not least, your filter needs to get registered. Let's start with the form component: public class AddressesFilter implements ListFilterComponent<Addresses> { private JTextField filter; private TextComponentMatcherEditor<Addresses> me; public RecordingFilter() { filter = new JTextField(20); me = new TextComponentMatcherEditor<Addresses>( filter, new AddressesFilterator()); } public JComponent getFilterPane() { FormLayout layout = new FormLayout("right:max(25dlu;pref), 3dlu, default:grow"); DefaultFormBuilder builder = new DefaultFormBuilder(layout); MessageSource msgSource = (MessageSource) ApplicationServiceProvider.getService(MessageSource.class); builder.setDefaultDialogBorder(); builder.append(msgSource.getMessage(PREFIX + "filter", null, PREFIX + "filter", null), filter, true); return builder.getPanel(); } public MatcherEditor<Addresses> getMatcherEditor() { return me; } } Next we'll add a class, that does the list filtering. Using the Superclass protected class AddressesFilterator implements TextFilterator<Addresses> { public void getFilterStrings(List Finally we have to register our filter at the views constructor and extend the pane, that shows up the list: public class AddressTableView extends AbstractTableView<Addresses> { protected static final String PREFIX = AddressesTableView.class.getSimpleName(); protected static RendererFactory rf; private AddressesFilter filter; public AddressTableView(List Once again the complete list view class: public class AddressTableView extends AbstractTableView<Addresses> { public class AddressesFilter implements ListFilterComponent<Addresses> { private JTextField filter; private TextComponentMatcherEditor<Addresses> me; public RecordingFilter() { filter = new JTextField(20); me = new TextComponentMatcherEditor<Addresses>( filter, new AddressesFilterator()); } public JComponent getFilterPane() { FormLayout layout = new FormLayout("right:max(25dlu;pref), 3dlu, default:grow"); DefaultFormBuilder builder = new DefaultFormBuilder(layout); MessageSource msgSource = (MessageSource) ApplicationServiceProvider.getService(MessageSource.class); builder.setDefaultDialogBorder(); builder.append(msgSource.getMessage(PREFIX + "filter", null, PREFIX + "filter", null), filter, true); return builder.getPanel(); } public MatcherEditor<Addresses> getMatcherEditor() { return me; } } protected class AddressesFilterator implements TextFilterator<Addresses> { public void getFilterStrings(List |