Graphical User Interfaces Part II Dialog Boxes Typically
Graphical User Interfaces: Part II
Dialog Boxes • Typically take the form of a small window that ‘pops up’ during program execution. Part of the login ‘dialog’ James Tam
JDialog Example • Location of the full example: /home/233/examples/gui/9 dialog. Example James Tam
The Driver Class public class Driver { public static void main(String [] args) { My. Dialog a. Dialog = new My. Dialog(); a. Dialog. set. Bounds(100, 300, 200); a. Dialog. set. Visible(true); } } James Tam
Class My. Dialog public class My. Dialog extends JDialog implements Action. Listener { private static final int MATCH = 0; private static final String ACTUAL_PASSWORD = "123456"; private JPassword. Field a. Password. Field; private JLabel a. Label; public My. Dialog() { a. Label = new JLabel("Enter password"); a. Label. set. Bounds(50, 20, 120, 20); a. Password. Field = new JPassword. Field(); a. Password. Field. set. Bounds(50, 40, 120, 20); a. Password. Field. add. Action. Listener(this); //Event handler set. Layout(null); add. Controls(); // #2 set. Default. Close. Operation(JDialog. DISPOSE_ON_CLOSE); } James Tam
Class My. Dialog (2) public void add. Controls() { add(a. Label); add(a. Password. Field); } James Tam
Class My. Dialog (3) public void action. Performed(Action. Event e) { Component a. Component = (Component) e. get. Source(); if (a. Component instanceof JPassword. Field) { JPassword. Field a. Password. Field = (JPassword. Field) a. Component; String pass. Word. Entered = new String(a. Password. Field. get. Password()); if (pass. Word. Entered. compare. To(ACTUAL_PASSWORD) == MATCH) login. Success(); // #4 else login. Failed() } } James Tam
Class My. Dialog (4) public void login. Success() { JDialog success = new JDialog(); success. set. Title("Login successful!"); success. set. Size(200, 50); success. set. Visible(true); clean. Up(success); } public void clean. Up(JDialog popup) { try Thread. sleep(3000); catch (Interrupted. Exception ex) System. out. println("Program interrupted"); this. set. Visible(false); this. dispose(); popup. set. Visible(false); popup. dispose(); System. exit(0); // Dialog cannot end whole program } James Tam
Class My. Dialog (5) public void login. Failed() { JDialog failed = new JDialog(); failed. set. Title("Login failed!"); failed. set. Size(200, 50); failed. set. Visible(true); clean. Up(failed); } public void clean. Up(JDialog popup) { try Thread. sleep(3000); catch (Interrupted. Exception ex) System. out. println("Program interrupted"); this. set. Visible(false); this. dispose(); popup. set. Visible(false); popup. dispose(); System. exit(0); // Dialog cannot end whole program } James Tam
Dialog Boxes And “User-Friendly Design” • Note: used sparingly dialog boxes can communicate important information or to prevent unintentional and undesired actions. James Tam
Dialog Boxes And “User-Friendly Design” (2) • They interupt the regular use of the program so make sure they are only used sparingly – …they can easily be over/misused!) James Tam
Dialogs Are Frequently Used Online • Great! I’ve got the info that I need. James Tam
Dialogs Are Frequently Used Online • Hey I was reading that! James Tam
Types Of Input Text Fields: Short • JText. Field (you already learned): Used to get short user input – e. g. , entering login or personal information. Bing search query James Tam
Types Of Input Text Fields: Long • Getting more extensive input – e. g. , feedback form, user review/comments on a website – Requires the use of another control: JText. Area Facebook status update field James Tam
The Driver Class public class Driver { public static void main(String [] args) { JFrame frame = new JFrame(); frame. set. Size(400, 250); JText. Area text = new JText. Area(); JScroll. Pane scroll. Pane = new JScroll. Pane(text); text. set. Font(new Font("Times", Font. BOLD, 32)); for (int i = 0; i < 10; i++) text. append("foo" + i + "n"); frame. add(scroll. Pane); My. Document. Listener l = new My. Document. Listener(); (text. get. Document()). add. Document. Listener(l); frame. set. Visible(true); frame. set. Layout(null); frame. set. Default. Close. Operation(JFrame. DISPOSE_ON_CLOSE); } } James Tam
The Text Listener: My. Document. Listener public class My. Document. Listener implements Document. Listener { public void changed. Update(Document. Event e) { // Modify System. out. println("updated"); method(e); } public void insert. Update(Document. Event e) { // Add System. out. println("insert"); System. out. println(e. get. Length()); method(e); } public void remove. Update(Document. Event e) { // Remove System. out. println("removed"); method(e); } } James Tam
The Text Listener: My. Document. Listener (2) public void method(Document. Event e) { Document d = e. get. Document(); try { String s = d. get. Text(0, d. get. Length()); System. out. println(s); } catch (Bad. Location. Exception ex) { System. out. println(ex); } } James Tam
Controls Affecting Other Controls • As previously shown this is not an uncommon occurrence • The code to react to the event allows for easy access to the control that raised the event. James Tam
Ways Of Accessing Other Controls 1. Via Java Swing containment – Example to illustrate with JButton control: – /home/233/examples/gui/6 control. Affect. Controls – JT’s $0. 02 • Stylistically acceptable (of course!) • Can be challenging to track down specific container/method James Tam
Ways Of Accessing Other Controls (2) 2. Implementing the listener class as a nested inner class. – (Recall that if one class is defined inside the definition of another class that the inner class is within the scope of the outer class and as a consequence it can access private attributes or methods). – JT’s $0. 02: take care that you don’t employ this technique too often and/or to bypass encapsulation/information hiding. public class My. Frame extends JFrame { private JLabel a Label; . . . private class My. Window. Listener extends Window. Adapter { public void window. Closing (Window. Event e) { a. Label. set. Text(“Shutting down”); } } } // End definition for inner window listener class // End definition for outer frame class James Tam
Ways Of Accessing Other Controls (3) 3. Adding the control as an attribute of the control that could raise the event. – Once you have access to the container then you can use accessor methods to get a reference to all the GUI components contained within that container. – The previously mentioned example (#6) illustrated this: public class My. Frame extends Jframe { private JLabel a. Label 1; private JLabel a. Label 2; . . . public JLabel get. Label 1 () { return a. Label 1; } public JLabel get. Label 2 () { return a. Label 2; } } – JT’s $0. 02: • Replaces Java’s containment with a simpler one that you created James Tam
Ways Of Accessing Other Controls (4) – Note: adding one control as an attribute of another control need not be limited only to actual ‘containers’ such as JFrame or JDialog – Example (button event changes a label) public class My. Button extends JButton { private JLabel a. Label; . . . public Jlabel get. Label() { return(a. Label); } } public class My. Button. Listener implements Action. Listener { public void action. Performed(Action. Event e) { My. Button a. Button = (My. Button) e. get. Source(); JLabel a. Label = a. Button. get. Label(); } } James Tam
Example Illustrating The Third Approach 1 And Adding Graphics To Controls • Location of the complete example: /home/233/examples/gui/11 containment 1 Adding a control as an attribute of another control need not be limited only to traditional container classes such as a JFrame James Tam
The Driver Class public class Driver { public static void main(String [] args) { My. Frame a. Frame = new My. Frame(); a. Frame. set. Visible(true); } } James Tam
Class My. Frame public class My. Frame extends JFrame { public static final String DEFAULT_LABEL_STRING = "Number presses: "; public static final int WIDTH = 700; public static final int HEIGHT = 300; private My. Button frame. Button; private My. Button label. Button; private JLabel a. Label; private int num. Presses; public My. Frame() { num. Presses = 0; initialize. Controls(); initialize. Frame(); } James Tam
Class My. Frame (2) public void add. Controls() { add(frame. Button); add(label. Button); add(a. Label); } public JLabel get. Label() { return(a. Label); } public int get. Num. Presses() { return(num. Presses); } public void increment. Presses() { num. Presses++; } James Tam
Class My. Frame (3) public void initialize. Frame() { set. Size(WIDTH, HEIGHT); set. Layout(null); add. Controls(); set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); } James Tam
Class My. Frame (4) No path provided: public void initialize. Controls() { location is the same directory as the program Image. Icon an. Icon = new Image. Icon("Icon. Pic. gif"); frame. Button = new My. Button("Affects window", an. Icon, this); frame. Button. set. Bounds(50, 100, 150, 20); frame. Button. add. Action. Listener (new Frame. Button. Listener()); // Frame events only label. Button = new My. Button("Affects label", an. Icon, this); label. Button. set. Bounds(250, 100, 150, 20); label. Button. add. Action. Listener (new Label. Button. Listener()); // Label events only a. Label = new JLabel(DEFAULT_LABEL_STRING + Integer. to. String(num. Presses)); a. Label. set. Bounds(450, 100, 150, 20); } } James Tam
Class My. Button public class My. Button extends JButton { private Component a. Component; public My. Button(String s, Image. Icon pic, Component a. Component) { super(s, pic); this. a. Component = a. Component; } Each instance will have a reference to a Java GUI widget (label, frame etc. ) Image reference passed onto the appropriate super class constructor public Component get. Component() { return(a. Component); } } James Tam
Class To Change Label: Label. Button. Listener public class Label. Button. Listener implements Action. Listener { public void action. Performed(Action. Event an. Event) { My. Button a. Button = (My. Button) an. Event. get. Source(); My. Frame a. Frame = (My. Frame) a. Button. get. Component(); a. Frame. increment. Presses(); // Frame stores count JLabel a. Label = a. Frame. get. Label(); String s = My. Frame. DEFAULT_LABEL_STRING; int current. Presses = a. Frame. get. Num. Presses(); s = s + Integer. to. String(current. Presses); a. Label. set. Text(s); // Label displays current count } } James Tam
Class To Update Frame: Frame. Button. Listener public class Frame. Button. Listener implements Action. Listener { // Assumes screen resolution is at least 1024 x 768 private final static int MAX_X = 1023; private final static int MAX_Y = 767; // Time in milliseconds private final int DELAY_TIME = 2500; James Tam
Class To Update Frame: Frame. Button. Listener (2) public void action. Performed(Action. Event an. Event) { My. Button a. Button = (My. Button) an. Event. get. Source(); JFrame a. Frame = (JFrame) a. Button. get. Component(); a. Frame. set. Title("Don't you click me! I'm in a bad mood!!!"); Random a. Generator = new Random(); // Control randomly “runs away” based on screen size int x = a. Generator. next. Int(MAX_X); int y = a. Generator. next. Int(MAX_Y); a. Frame. set. Location(x, y); // Move control to new location a. Button. set. Background(Color. RED); // Control is angry pause(); a. Frame. set. Title(""); // Angry text is gone } James Tam
Class To Update Frame: Frame. Button. Listener (3) private void pause() // Give user time to note GUI changes { try { Thread. sleep(DELAY_TIME); } catch (Interrupted. Exception ex) { ex. print. Stack. Trace(); } } } James Tam
- Slides: 34