GUI IVOutlineEvent handling exampleModel-view-controllerJTableMVC in JTableMore TypicalTableModelDefaultTableModelAbstractTableModelWhy a separate class?Handling ChangeOther ModelsEditing and RenderingExampleJTreeTreeModelTreeNodeEventsHomework #3db packagecore packagecmd packageui packageui package continuedAssignmentCommand ClassNew I/O MethodsExamineAdviceGUI IVIS 3135.8.2003Outline Event handling example Model-view-controller JTable Homework #3Event handling exampleModel-view-controller Controller manipulate-able interface modality View observable interface display Model underlying data structureJTableMVC in JTableMore TypicalTableModel// Mandatoryint getColumnCount() int getRowCount() Object getValueAt(int rowIndex, int columnIndex) // For displayString getColumnName(int columnIndex) // For editingboolean isCellEditable(int rowIndex, int columnIndex) void setValueAt(Object aValue, int rowIndex, int columnIndex)// For renderingClass getColumnClass(int columnIndex) // For event handlingvoid addTableModelListener(TableModelListener l) void removeTableModelListener(TableModelListener l)DefaultTableModel Vector of Vectors To create DefaultTableModel(Object[ ][ ] data, Object[ ] columnNames) DefaultTableModel(Vector data, Vector columnNames) You can use the default table model implicitly new JTable(Object[ ][ ] data, Object[ ] columnNames) new JTable(Vector data, Vector columnNames)AbstractTableModel Supplies default methods for most of the methods Specializers need implement only int getColumnCount() int getRowCount() Object getValueAt(int rowIndex, int columnIndex)Why a separate class? Efficiency 1 no need to convert table to a different form Efficiency 2 only generate those items that the user wants to see Modularity Isolate the form of data display from the data itselfHandling Change The JTable needs to know when the model has changed TableModelListener If you have your own setValueAt method you must call fireTableCellUpdated (row, col) the JTable is a listenerOther Models TableColumnModel Separate from the table data Columns have their own display characteristics visible / hidden width ordering ListSelectionModel Selection is a set of intervals Issue of “what is selected” can be complex TreeSelectionModelEditing and Rendering Return a class with getColumnClass Some built-in Boolean = check-box ImageIcon = centered label containing the image Otherwise Register an editor and/or renderer for that class table.setCellEditor (Class, CellEditor) table.setDefaultRenderer (Class, Renderer)Example FlightTableModelJTreeTreeModel// essentialObject getRoot() Object getChild(Object parent, int index) int getChildCount(Object parent) int getIndexOfChild(Object parent, Object child) boolean isLeaf(Object node) // editabilityvoid valueForPathChanged(TreePath path, Object newValue) // event handlingvoid addTreeModelListener(TreeModelListener l) void removeTreeModelListener(TreeModelListener l)TreeNode// move downEnumeration children() TreeNode getChildAt(int childIndex) int getIndex(TreeNode node) int getChildCount() // move upTreeNode getParent() // statusboolean getAllowsChildren() boolean isLeaf()Events Tree expand/collapse TreeExpansionListener Node selection TreeSelectionListener Node/tree modification TreeModelListenerHomework #3 FFGUIMode Differences package structure dynamic command loading automatic path detection graphical interface in-memory database must use JDK 1.4db package Class related to data storage and retrieval FrequentFlyerDB main class for interacting with the stored data also MemberDB and FlightDB Member holds individual data has miles value Flight holds flight records MultiMap a utility class for a Map with multiple values for one key DBUtilities home of various utility functionscore package CommandMap maps names to commands loads commands at start up Command SimpleCommand ExitCommand HelpCommand MDatabaseCommand “M” indicates “memory” databasecmd package Adding/deleting commands MAddMemberCommand MAddFlightCommand MDeleteFlightCommand Reporting commands MShowMembersCommand MShowFlightsCommand Finding Commands MFindByNameCommand MFindByLevelCommand MFindFlightsByMemberCommand UnknownCommandui package FFTextMode FFGUIMode FFFrame menus CommandListener external listener class for commands PromptDialog default dialog for getting parameters for commands ResultDialog default dialog for displaying resultsui package continued FlightDisplayDialog JTable FlightTableModel EnterLevelDialog Combo box for benefit level EnterIdDialogAssignment Input dialog for “add member” use appropriate components Output dialog with a table of members to be used by “show members”, “find by name”, “find by level” must use a table model Help dialog But the set of commands is not fixed!Command ClassNew I/O Methods getParameters detects mode calls getParametersText or getParametersGUI default implementations provided subclasses must override getParametersGUI to display their own dialogs displayResults detects mode calls displayResultsText or displayResultsGUI default / must overrideExamine FFFrame CommandListener MShowFlightsCommand MDeleteFlightCommand EnterIdDialogAdvice Start early! Draw your dialogs before implementing Use existing classes as
View Full Document