Computer/Human InteractionLecture 16Overview:More input elements, listenersMessage dialogsMenusFile I/OReferences:JSWI, JTUT, JAPIPurple Pizza Parlor, v2Download from web or copy files /home/hwang/cs350/lecture16/*.*All Linux boxen have updated Java compiler and runtimes, so don’t need to change pathTo use Eclipse: create directory for files of a project. Import directory as project.Writes to file “output/orders.txt”, need to create output subdirectory first.Combo BoxesJComboBox – construct with array of string itemsUses ActionListener interface, dynamically cast the source of the event object into a JComboBox and get the selected itemList BoxesJList – construct with array of string itemsUses ListSelectionListener interfaceDefined in javax.swing.event.*Handler is valueChangedMULTIPLE_INTERVAL_SELECTION mode is default, also SINGLE_SELECTION and SINGLE_INTERVAL_SELECTION modesgetSelectedIndices returns int array of indices of selected itemsMessage DialogsSimple OK message dialogs can be created using JOptionPane.showMessageDialogArguments are the top-level frame, dialog message, window title, and message type (determines the icon); can also add icon image argumentDefined message types are INFORMATION_MESSAGE, WARNING_MESSAGE, ERROR_MESSAGE, QUESTION_MESSAGE, PLAIN_MESSAGE (no icon)MenuBars and MenusJMenuBar is added specially to top-level frameDefault placement is platform-dependentAdd JMenu objects to menubarAdd JMenuItem, JMenu objects to menusUses ActionListener interfacePPP Action menu uses same listener as the buttons, so define ButtonListener, a named inner class that both can useMiscellaneousSize and toppings are not in same panel; their panels have labelsAdded keyboard mnemonics to buttonssubmit.setMnemonic (KeyEvent.VK_S);Underlines the letter in the labelMenubar creation is a call to application object method, so the application object is created first, then added to outer frame.File I/OFile streams defined in java.io.*General paradigm is to wrap a buffered stream around an unbuffered stream around a file.PrintWriter implements print and println PrintWriter outputStream = null;try { outputStream = new PrintWriter( new FileWriter (“output/orders.txt”)); outputStream.println (“Text with newline”);} catch (IOException ex) { // error code} finally { // clean up code }File I/O 2BufferedReader implements readLine that returns a line as a String, or null at end of fileBufferedReader inStream = null;try { inStream = new BufferedReader( new FileReader (“input.txt”)); String line; while ((line = inStream.readLine()) != null) { // do something with line }} catch ...ExerciseTry the followingAdd a phone field, add error checking to make sure it’s filled in, add code to write the data to the output fileSeparate the toppings into meats and vegetables. Create a list box for each. Create a named inner class that implements the ListSelectionListener interface so they can share the same
View Full Document