Chris Johnson, Index
Containers
User Interface Design Using Java
Chris Johnson, Index
Frames
- Windows & dialogs link to frames.
- Frames can stand-alone on screen.
- Warning! They will not close.
- Catch Event.WINDOW_DESTROY.
User Interface Design Using Java
Chris Johnson, Index
Dialogs
- Frames dont block thread execution.
- You may want to do this.
- Open or save a file?
- User must finish this before continuing
User Interface Design Using Java
Chris Johnson, Index
Dialogs
- Similar to frames.
- Used if a window depends on another.
- For example, search depends on file.
User Interface Design Using Java
Chris Johnson, Index
Dialogs
dialog = new Dialog(myFrame, "SimpleDialog", true);
/* true implies a modal dialog */
- Modal dialogs:
- block thread execution;
- block input to other windows.
- Close window to continue:
- by dialog completion;
- or by cancel button.
User Interface Design Using Java
Chris Johnson, Index
FileDialog
- AWT provides peer specific dialogs.
- Depends on platform you run on.
User Interface Design Using Java
Chris Johnson, Index
FileDialog
- Some browsers dont have FileDialog.
- Should applets write to disk?
- Should they read local files?
- Java console will notify error.
User Interface Design Using Java
Chris Johnson, Index
FileDialog
/*
File: FileDialog.java
Author: Chris Johnson
johnson@dcs.gla.ac.uk
Modified: 21.10.98
Description:
Illustrates dialogs to load and save a file.
*/
import java.applet.Applet;
import java.awt.*;
public class FileDialogTest extends Applet{
Button load = new Button("Load file...");
Button save = new Button("Save file...");
FileDialog dialog;
String filename;
public void init(){
add(load); /* set up buttons that users presses on */
add(save); /* initial screen to then call up either*/
} /* the load or save dialogs as appropriate */
public boolean action(Event event, Object what){
showStatus(null);
if (event.target == load){
Button button = load;
Frame myFrame = getFrame(button);
dialog = new FileDialog(myFrame, "Load A File");
}
else if (event.target == save){
Button button = save;
Frame myFrame = getFrame(button);
dialog = new FileDialog(myFrame, "Save A File", FileDialog.SAVE);
}
dialog.show();
if((filename = dialog.getFile())!= null)
showStatus(filename);
else
showStatus("FileDialog Cancelled");
return true;
}
static Frame getFrame (Component c) {
Frame frame = null;
while ((c= c.getParent())!= null){
if (c instanceof Frame)
frame = (Frame) c;
}
return frame;
}
}
User Interface Design Using Java
Chris Johnson, Index
Containers
User Interface Design Using Java
Chris Johnson, Index
Containers
Object
|
Component
|
------------
Container
|
-----------
Panel Window
|
----------
Dialog Frame
|
FileDialog
User Interface Design Using Java
Chris Johnson, Index
Containers
Hint: use your browser to open this image.
User Interface Design Using Java
Chris Johnson, Index
Windows
- Very basic.
- No border, titlebar or menubar.
- Cannot be resized.
User Interface Design Using Java
Chris Johnson, Index
Windows
- void pack()
minimum size to hold components.
- void show()
makes window visible.
- void dispose()
hide window and free resources.
- void toFront(), void toBack()
place window on top/bottom.
User Interface Design Using Java
Chris Johnson, Index
Windows
- Windows are not always visible.
- Use show() then toFront().
- dispose() frees platform resources.
- Just call show() to regenerate window.
User Interface Design Using Java
Chris Johnson, Index
Frames
- Do have a border and a title bar.
- Are resizable and can have menus.
- Used in applets and applications.
User Interface Design Using Java
Chris Johnson, Index
Frames
/*
* A frame
* Author: Chris Johnson (johnson@dcs.gla.ac.uk), revised 11/10/98
* Produces a warning window on the screen
* Beware - there is no way of closing the frame!
*/
import java.awt.*;
public class SimpleWarningFrame extends Frame {
static private final int frame_height = 150;
static private final int frame_width = 250;
public SimpleWarningFrame () {
setBackground(Color.red);
setForeground(Color.black);
}
public static void main (String[] args){
Frame f= new SimpleWarning();
f.setTitle("Warning");
f.resize(frame_width, frame_height);
f.show(true);
}
}
User Interface Design Using Java
Chris Johnson, Index
Frames
- Untrusted applets give warning.
- If not, could trick user of frame.
- Cannot remove warning.
- getWarningString().
User Interface Design Using Java
Chris Johnson, Index
Frames
- Windows & dialogs link to frames.
- Frames can stand-alone on screen.
- Warning! They will not close.
- Catch Event.WINDOW_DESTROY.
User Interface Design Using Java
Chris Johnson, Index
Dialogs
- Frames dont block thread execution.
- You may want to do this.
- Open or save a file?
- User must finish this before continuing
User Interface Design Using Java
Chris Johnson, Index
Dialogs
- Similar to frames.
- Used if a window depends on another.
- For example, search depends on file.
User Interface Design Using Java
Chris Johnson, Index
Dialogs
dialog = new Dialog(myFrame, "SimpleDialog", true);
/* true implies a modal dialog */
- Modal dialogs:
- block thread execution;
- block input to other windows.
- Close window to continue:
- by dialog completion;
- or by cancel button.
User Interface Design Using Java
Chris Johnson, Index
FileDialog
- AWT provides peer specific dialogs.
- Depends on platform you run on.
User Interface Design Using Java
Chris Johnson, Index
FileDialog
- Some browsers dont have FileDialog.
- Should applets write to disk?
- Should they read local files?
- Java console will notify error.
User Interface Design Using Java
Chris Johnson, Index
FileDialog
/*
File: FileDialog.java
Author: Chris Johnson
johnson@dcs.gla.ac.uk
Modified: 21.10.98
Description:
Illustrates dialogs to load and save a file.
*/
import java.applet.Applet;
import java.awt.*;
public class FileDialogTest extends Applet{
Button load = new Button("Load file...");
Button save = new Button("Save file...");
FileDialog dialog;
String filename;
public void init(){
add(load); /* set up buttons that users presses on */
add(save); /* initial screen to then call up either*/
} /* the load or save dialogs as appropriate */
public boolean action(Event event, Object what){
showStatus(null);
if (event.target == load){
Button button = load;
Frame myFrame = getFrame(button);
dialog = new FileDialog(myFrame, "Load A File");
}
else if (event.target == save){
Button button = save;
Frame myFrame = getFrame(button);
dialog = new FileDialog(myFrame, "Save A File", FileDialog.SAVE);
}
dialog.show();
if((filename = dialog.getFile())!= null)
showStatus(filename);
else
showStatus("FileDialog Cancelled");
return true;
}
static Frame getFrame (Component c) {
Frame frame = null;
while ((c= c.getParent())!= null){
if (c instanceof Frame)
frame = (Frame) c;
}
return frame;
}
}
User Interface Design Using Java
Chris Johnson, Index
Containers