DOC PREVIEW
USC CSCI 571 - 05Advanced-Swing

This preview shows page 1-2-20-21 out of 21 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Advanced SwingAgendaMVC ArchitectureJList with Fixed Set of ChoicesSimple JList: Example CodeSimple JList: Example Code (Continued)Simple JList: Example OutputJList with Changeable ChoicesChangeable JList: Example CodeChangeable JList: Example Code (Continued)Changeable JList: Example OutputJList with Custom Data ModelCustom Model: Example CodeActual DataJList with Custom Model: Example CodeJList with Custom Model: Example OutputJList with Custom Cell RendererCustom Renderer: Example CodeCustom Renderer: Example OutputSummaryQuestions?1© 2001-2002 Marty Hall, Larry Brown http://www.corewebprogramming.comWebcoreprogrammingAdvanced Swing Custom Data Models and Cell RenderersAdvanced Swing2www.corewebprogramming.comAgenda•Building a simple static JList•Adding and removing entries from a JList at runtime•Making a custom data model–Telling JList how to extract data from existing objects•Making a custom cell renderer–Telling JList what GUI component to use for each of the data cellsAdvanced Swing3www.corewebprogramming.comMVC Architecture•Custom data models–Changing the way the GUI control obtains the data. Instead of copying data from an existing object into a GUI control, simply tell the GUI control how to get at the existing data.•Custom cell renderers–Changing the way the GUI control displays data values. Instead of changing the data values, simply tell the GUI control how to build a Swing component that represents each data value.•Main applicable components–JList–JTable–JTreeAdvanced Swing4www.corewebprogramming.comJList with Fixed Set of Choices•Build JList: pass strings to constructor–The simplest way to use a JList is to supply an array of strings to the JList constructor. Cannot add or remove elements once the JList is created. String options = { "Option 1", ... , "Option N"};JList optionList = new JList(options);•Set visible rows–Call setVisibleRowCount and drop JList into JScrollPaneoptionList.setVisibleRowCount(4);JScrollPane optionPane = new JScrollPane(optionList);someContainer.add(optionPane);•Handle events–Attach ListSelectionListener and use valueChangedAdvanced Swing5www.corewebprogramming.comSimple JList: Example Codepublic class JListSimpleExample extends JFrame {... public JListSimpleExample() { super("Creating a Simple JList"); WindowUtilities.setNativeLookAndFeel(); addWindowListener(new ExitListener()); Container content = getContentPane(); String[] entries = { "Entry 1", "Entry 2", "Entry 3", "Entry 4", "Entry 5", "Entry 6"}; sampleJList = new JList(entries); sampleJList.setVisibleRowCount(4); sampleJList.addListSelectionListener (new ValueReporter()); JScrollPane listPane = new JScrollPane(sampleJList); ... }Advanced Swing6www.corewebprogramming.comSimple JList: Example Code (Continued) private class ValueReporter implements ListSelectionListener { /** You get three events in many cases -- one for the * deselection of the originally selected entry, one * indicating the selection is moving, and one for the * selection of the new entry. In the first two cases, * getValueIsAdjusting returns true; thus, the test * below since only the third case is of interest. */ public void valueChanged(ListSelectionEvent event) { if (!event.getValueIsAdjusting()) { Object value = sampleJList.getSelectedValue(); if (value != null) { valueField.setText(value.toString()); } } } }}Advanced Swing7www.corewebprogramming.comSimple JList: Example OutputAdvanced Swing8www.corewebprogramming.comJList with Changeable Choices•Build JList: –Create a DefaultListModel, add data, pass to constructorString choices = { "Choice 1", ... , "Choice N"};DefaultListModel sampleModel = new DefaultListModel();for(int i=0; i<choices.length; i++) { sampleModel.addElement(choices[i]);}JList optionList = new JList(sampleModel);•Set visible rows–Same: Use setVisibleRowCount and a JScrollPane•Handle events–Same: attach ListSelectionListener and use valueChanged•Add/remove elements–Use the model, not the JList directlyAdvanced Swing9www.corewebprogramming.comChangeable JList: Example Code String[] entries = { "Entry 1", "Entry 2", "Entry 3", "Entry 4", "Entry 5", "Entry 6"}; sampleModel = new DefaultListModel(); for(int i=0; i<entries.length; i++) { sampleModel.addElement(entries[i]); } sampleJList = new JList(sampleModel); sampleJList.setVisibleRowCount(4); Font displayFont = new Font("Serif", Font.BOLD, 18); sampleJList.setFont(displayFont); JScrollPane listPane = new JScrollPane(sampleJList);Advanced Swing10www.corewebprogramming.comChangeable JList: Example Code (Continued) private class ItemAdder implements ActionListener { /** Add an entry to the ListModel whenever the user * presses the button. Note that since the new entries * may be wider than the old ones (e.g., "Entry 10" vs. * "Entry 9"), you need to rerun the layout manager. * You need to do this <I>before</I> trying to scroll * to make the index visible. */ public void actionPerformed(ActionEvent event) { int index = sampleModel.getSize(); sampleModel.addElement("Entry " + (index+1)); ((JComponent)getContentPane()).revalidate(); sampleJList.setSelectedIndex(index); sampleJList.ensureIndexIsVisible(index); } }}Advanced Swing11www.corewebprogramming.comChangeable JList: Example OutputAdvanced Swing12www.corewebprogramming.comJList with Custom Data Model•Build JList –Have existing data implement ListModel interface•getElementAt–Given an index, returns data element•getSize–Tells JList how many entries are in list•addListDataListener–Lets user add listeners that should be notified when an item is selected or deselected.•removeListDataListener–Pass model to JList constructor•Set visible rows & handle events: as before•Add/remove items: use the modelAdvanced Swing13www.corewebprogramming.comCustom Model: Example Codepublic class JavaLocationListModel implements ListModel { private JavaLocationCollection collection; public JavaLocationListModel (JavaLocationCollection collection) { this.collection = collection; } public Object getElementAt(int index) { return(collection.getLocations()[index]); } public int getSize() { return(collection.getLocations().length); } public void addListDataListener(ListDataListener l)


View Full Document

USC CSCI 571 - 05Advanced-Swing

Download 05Advanced-Swing
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view 05Advanced-Swing and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view 05Advanced-Swing 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?