gui programming: components
TRANSCRIPT
![Page 1: GUI Programming: Components](https://reader030.vdocuments.mx/reader030/viewer/2022020701/61f8f7123d297461b96a84b5/html5/thumbnails/1.jpg)
GUI Programming: Components
Slides derived from the work of
Dr. Amy McGovern and Dr. Deborah Trytten
Andrew H. Fagg: CS 2334: GUI Components 1
![Page 2: GUI Programming: Components](https://reader030.vdocuments.mx/reader030/viewer/2022020701/61f8f7123d297461b96a84b5/html5/thumbnails/2.jpg)
Basic JComponents
•JRadioButton: Radio buttons to select one item from a set of possibilities
•JCheckBox: Check box to select an item. Can have multiple checked at the same time
•JLabel: Display text
•JTextField: Enter/display a single line of text
Andrew H. Fagg: CS 2334: GUI Components 2
![Page 3: GUI Programming: Components](https://reader030.vdocuments.mx/reader030/viewer/2022020701/61f8f7123d297461b96a84b5/html5/thumbnails/3.jpg)
Other GUI Components
•JTextArea:•Multiple lines of text•Can embed within JScrollPane to enable scrolling
•JComboBox•Dropdown list of items to choose from•Pre-validates input!•ActionEvent for new selections• ItemEvent for unselect/new select
Andrew H. Fagg: CS 2334: GUI Components 3
![Page 4: GUI Programming: Components](https://reader030.vdocuments.mx/reader030/viewer/2022020701/61f8f7123d297461b96a84b5/html5/thumbnails/4.jpg)
Administrivia
•Lab 10 due Tuesday @7pm
•Project 3•Code reviews this week
•Project 4 is out & is due in 2 weeks
•Exam 2: next Monday
Andrew H. Fagg: CS 2334: GUI Components 4
![Page 5: GUI Programming: Components](https://reader030.vdocuments.mx/reader030/viewer/2022020701/61f8f7123d297461b96a84b5/html5/thumbnails/5.jpg)
JList
•Like a combo box but enables multiple selection•Single item•Single interval selection•Multiple interval selection•Also combines with JScrollPane
•Fires ListSelectionEvent•valueChanged() handler
Andrew H. Fagg: CS 2334: GUI Components 5
![Page 6: GUI Programming: Components](https://reader030.vdocuments.mx/reader030/viewer/2022020701/61f8f7123d297461b96a84b5/html5/thumbnails/6.jpg)
Range Selection
JSlider
•Select a value from a range
•Sliding selection with a knob
Andrew H. Fagg: CS 2334: GUI Components 6
![Page 7: GUI Programming: Components](https://reader030.vdocuments.mx/reader030/viewer/2022020701/61f8f7123d297461b96a84b5/html5/thumbnails/7.jpg)
Multiple Windows
•Use multiple JFrames
•Example:•Create a JFrame with a button on it•When button is clicked, a new window appears• (like a progress bar and ‘OK’ button)
Andrew H. Fagg: CS 2334: GUI Components 7
![Page 8: GUI Programming: Components](https://reader030.vdocuments.mx/reader030/viewer/2022020701/61f8f7123d297461b96a84b5/html5/thumbnails/8.jpg)
Progress Bars
JProgressBar
•Provide feedback to a user about the progress of a file operation or a long computation
•Important for indicating that the program is busy (as opposed to frozen)
Andrew H. Fagg: CS 2334: GUI Components 8
![Page 9: GUI Programming: Components](https://reader030.vdocuments.mx/reader030/viewer/2022020701/61f8f7123d297461b96a84b5/html5/thumbnails/9.jpg)
Creating Menus
Object hierarchy (has-a):
•JFrame• JMenuBar: single drop-down menu
• JMenuItem: selectable item in a MenuBar
•JMenuItem actions:•Extend ActionListener
• actionPerformed method with ActionEvent as a parameter
Andrew H. Fagg: CS 2334: GUI Components 9
![Page 10: GUI Programming: Components](https://reader030.vdocuments.mx/reader030/viewer/2022020701/61f8f7123d297461b96a84b5/html5/thumbnails/10.jpg)
Pop-Up Dialog Boxes
Short conversations with users:
•Inform the user that an error has happened
•Prompt the user to answer a yes/no question
•Prompt the user for a file name
These windows are modal: they take the focus away from other windows
Andrew H. Fagg: CS 2334: GUI Components 10
![Page 11: GUI Programming: Components](https://reader030.vdocuments.mx/reader030/viewer/2022020701/61f8f7123d297461b96a84b5/html5/thumbnails/11.jpg)
New Class: JOptionPane
One method:showConfirmDialog(Component parentComponent,
Object message)
•parentComponent: Use for placement of the dialog box. Can be null
•Message: Often a String containing the user prompt
Andrew H. Fagg: CS 2334: GUI Components 11
![Page 12: GUI Programming: Components](https://reader030.vdocuments.mx/reader030/viewer/2022020701/61f8f7123d297461b96a84b5/html5/thumbnails/12.jpg)
New Class: JOptionPaneOne method:showConfirmDialog(Component parentComponent,
Object message,
String title,
int optionType,
int messageType)
• option:Type: YES_NO_OPTION, YES_NO_CANCEL_OPTION, or OK_CANCEL_OPTION
• messageType: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE
• Return: Index number of selected option
Andrew H. Fagg: CS 2334: GUI Components 12
![Page 13: GUI Programming: Components](https://reader030.vdocuments.mx/reader030/viewer/2022020701/61f8f7123d297461b96a84b5/html5/thumbnails/13.jpg)
File Selection
JFileChooser:
•Create instance of file chooser:choose = new JFileChooser(new File(“./data”))
•Sets default directory (here, the default is relative to the execution context)
•Note: does not automatically open dialog box
Andrew H. Fagg: CS 2334: GUI Components 14
![Page 14: GUI Programming: Components](https://reader030.vdocuments.mx/reader030/viewer/2022020701/61f8f7123d297461b96a84b5/html5/thumbnails/14.jpg)
File Selection
Invoke the file chooser:int ret = choose.showOpenDialog();
•Select an existing file to open (there are similar method calls for other operations
•Return is one of CANCEL_OPTION, APPROVE_OPTION, or ERROR_OPTION
•If APPROVE_OPTION, then the file can be retrieved by:
File file = choose.getSelectedFile();
Andrew H. Fagg: CS 2334: GUI Components 15
![Page 15: GUI Programming: Components](https://reader030.vdocuments.mx/reader030/viewer/2022020701/61f8f7123d297461b96a84b5/html5/thumbnails/15.jpg)
Data Models
ListModel: List of objects to be displayed by a selection component (e.g., a JList)
• On construction, a JList will automatically create a ListModelfrom the array of objects that it is given
JList jl = new JList({“A”,“B”});
Andrew H. Fagg: CS 2334: GUI Components 16
JList
ListModel
1
![Page 16: GUI Programming: Components](https://reader030.vdocuments.mx/reader030/viewer/2022020701/61f8f7123d297461b96a84b5/html5/thumbnails/16.jpg)
Data Models
• In many cases, the content of a JComponent, such as a JList stays constant: here, we use the default
•In other cases, the contents of the DataModel may change with time (this happens in project 4!)
•In this case, we must explicitly create a DataModel
Andrew H. Fagg: CS 2334: GUI Components 17
![Page 17: GUI Programming: Components](https://reader030.vdocuments.mx/reader030/viewer/2022020701/61f8f7123d297461b96a84b5/html5/thumbnails/17.jpg)
For JList: DefaultListModel
•Model creation:DefaultListModel model =
new DefaultListModel<String>();
•Can operate on this model as a list. For example:
model.clear();
model.addElement(“Foo”);
JList jl = new Jlist(model);
Andrew H. Fagg: CS 2334: GUI Components 18
![Page 18: GUI Programming: Components](https://reader030.vdocuments.mx/reader030/viewer/2022020701/61f8f7123d297461b96a84b5/html5/thumbnails/18.jpg)
DefaultListModel
•Every update to the list model will cause the JList to be repainted automatically
•Note that selections can change, too
Andrew H. Fagg: CS 2334: GUI Components 19
![Page 19: GUI Programming: Components](https://reader030.vdocuments.mx/reader030/viewer/2022020701/61f8f7123d297461b96a84b5/html5/thumbnails/19.jpg)
More swing components
•http://da2i.univ-lille1.fr/doc/tutorial-java/ui/features/components.html
•Java documentation:•http://docs.oracle.com/javase/tutorial/uiswing/c
omponents/componentlist.html
Andrew H. Fagg: CS 2334: GUI Components 20