CS18000: Problem Solving and Object-Oriented Programming - Simple GUIs
Simple Graphical User Interfaces
Text-Based Interface vs. GUI
Text-Based Interface:
User enters data via keyboard.
Outputs displayed in a terminal window.
Graphical User Interface (GUI):
Displays controls or widgets in a window.
User interacts with these controls, and the program responds to events.
Dialog Concepts
Prerequisites: Application must be running on a Java Virtual Machine (JVM) with a configured window system.
Java dialogs are modal, meaning they block the application code from running until the user responds.
Java GUI components adapt to the “Look and Feel” of the local system.
JOptionPane Class
Purpose: A core class in Java for creating modal dialogs.
Import Statement:
import javax.swing.JOptionPane;Common Static Methods:
showMessageDialogshowInputDialogshowConfirmDialogshowOptionDialog
Common Arguments for JOptionPane
Location: Where the dialog appears on the screen (null centers it).
Message: String, icon, or HTML content displayed in the dialog.
Message Type: Affects the look and feel and icon displayed.
Option Type: Specifies default buttons included.
Icon: Custom icon to replace the default icon.
Title: String for the dialog window heading.
Initial Value: Default value for certain input fields.
Many arguments are optional and can use defaults.
Message Type Parameter
Determines the icon displayed and varies by system look and feel.
Possible Values:
JOptionPane.PLAIN_MESSAGE(-1)JOptionPane.ERROR_MESSAGE(0)JOptionPane.INFORMATION_MESSAGE(1)JOptionPane.WARNING_MESSAGE(2)JOptionPane.QUESTION_MESSAGE(3)
JOptionPane Methods
showMessageDialog:
Displays a message to the user.
This is a void method (only displays, does not return any value).
showConfirmDialog:
Prompts for user confirmation (e.g., “Yes”, “No”, “Cancel”).
Method returns an integer indicating the user's selection.
Default options include:
JOptionPane.YES_NO_OPTIONJOptionPane.YES_NO_CANCEL_OPTIONJOptionPane.OK_CANCEL_OPTIONPossible return values:
JOptionPane.YES_OPTION(0)JOptionPane.NO_OPTION(1)JOptionPane.CANCEL_OPTION(2)JOptionPane.CLOSED_OPTION(-1)
showInputDialog:
Requests user input and returns a String.
Allows for free typing or selection from a list (dropdown).
Can create dropdowns using an array of Strings and specify an initial value.
showOptionDialog:
More configurable, allowing for different button arrangements.
Returns the index of the button selected by the user.
Accepts multiple parameters (e.g., message, title, icons).
Codon Extractor Example
Problem: Write a program to read a DNA sequence and display the codons within it.
Definitions:
DNA Sequence: Sequence of characters in ACGT.
Codon: Sequence of three characters in the DNA sequence.
Algorithm Steps:
Prompt user for DNA.
Validate input.
Break DNA into 3-character chunks.
Repeat until the user indicates they are done.
CodonExtractor Class Example
Main Method:
```java
int continueProgram;
do {
String input = JOptionPane.showInputDialog("Enter a DNA sequence").toUpperCase();
String message = "Do you want to continue?";
if (isValid(input))
displayCodons(input);
else
message = "Invalid DNA Sequence.\n" + message;
continueProgram = JOptionPane.showConfirmDialog(null, message, "Alert", JOptionPane.YESNOOPTION);
} while (continueProgram == JOptionPane.YES_OPTION);
JOptionPane.showMessageDialog(null, "Thanks for using the Codon Extractor!");
- **isValid Method**: Validates the DNA sequence.
java
public static boolean isValid(String dna) {
String validBases = "ACGT";
for (int i = 0; i < dna.length(); i++) {
char base = dna.charAt(i);
if (validBases.indexOf(base) == -1)
return false;
}
return true;
}
- **displayCodons Method**: Displays valid codons and handles remaining bases.
java
public static void displayCodons(String dna) {
String message = "";
for (int i = 0; i < dna.length() - 2; i += 3)
message += "\n" + dna.substring(i, i + 3);
int remaining = dna.length() % 3;
if (remaining == 1)
message += "\n" + dna.charAt(dna.length() - 1) + "*"; else if (remaining == 2) message += "\n" + dna.substring(dna.length() - 2, dna.length()) + "";
message = "dna length: " + dna.length() + "\n\nCodons: " + message;
JOptionPane.showMessageDialog(null, message, "Codons in DNA", JOptionPane.INFORMATION_MESSAGE);
}
### JFileChooser Class
- **Purpose**: Lets users choose files on their file system.
- **Usage**:
- Create a new `JFileChooser` object and set the title with `fc.setDialogTitle(title)`.
- Call `fc.showOpenDialog(null)` to display the dialog.
- Check the return value (0 for open, 1 for cancel) and then get the selected file using `fc.getSelectedFile()`.
### FileChooser Example
- **Code Example**:
java
import java.io.File;
import javax.swing.JFileChooser;
public class FileChooser {
public static void main(String[] args) {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Choose Important File");
int val = fc.showOpenDialog(null);
System.out.println(val);
File f = fc.getSelectedFile();
System.out.println(f);
}
}
```