Presentation is loading. Please wait.

Presentation is loading. Please wait.

Building Applications Dialogs Passing data between windows Validating Input using FocusListeners.

Similar presentations


Presentation on theme: "Building Applications Dialogs Passing data between windows Validating Input using FocusListeners."— Presentation transcript:

1 Building Applications Dialogs Passing data between windows Validating Input using FocusListeners

2 Dialogs Dialogs are windows that appear on top of the main window Used when the application need further to provide or request information to/from the user

3 Dialogs 2 kinds of dialog simple message popped up to the user (JOptionPane [static classes] ) –information message, confirmation required from user, short input required from user (e.g. password) more complex (JDialog) E.g. Filechooser

4 Simple dialogs: JOptionPane JOptionPane class can be used for most simple dialogs e.g. Single Message with an OK button Message with an set of choices JOptionPane class obviously provides some way of configuring the message, configuring buttons, icons etc...

5 Has a set of static methods for the types of simple dialogs you might want to create.: static methods – showXXXDialog –showMessageDialog = a simple one-button dialog –showConfirmDialog = asks user to confirm with standard Yes/No buttons –showInputDialog = gets a string from the user –showOptionDialog = customised Dialog variety of buttons, customised button text customised messages, icons, etc JOptionPane Class

6 –showMessageDialog () –showConfirmDialog () –showInputDialog () –showOptionDialog () Static Methods of JOptionPan class Which of the following methods of the JOptionClass for which dialog?

7 –showMessageDialog –showConfirmDialog –showInputDialog –showOptionDialog Static Methods of JOptionPan class

8 JOptionPane static methods – showXXXDialog –showMessageDialog = a simple one-button dialog –showConfirmDialog = asks user to confirm with standard Yes/No buttons –showInputDialog = gets a string from the user –showOptionDialog = customised Dialog variety of buttons, customised button text customised messages, icons, etc Remember to handle the response from the user

9 JDialog Object Component Container Window Dialog JDialog Frame JFrame more complex (JDialog) than simple dialogs supported by JOptionPane E.g. Filechooser

10 FileChooser dialog JFileChooser API Methods all: setting directory, file types, etc

11 Dialogs Dialogs can be modal –user forced to interact with the dialog before continuing (e.g. JOptionPane dialogs) non modal –user can interact with other windows/dialogs while dialog is displayed Modal can be disruptive to user –use it wisely…

12 Creating Dialogs Creating a JDialog you should specify –owner = parent for dialog when owner is destroyed so are child dialogs –title –modal setting when true, blocks input to all other windows in the program Various constructors e.g. JDialog (Frame owner, String title, boolean modal)

13 JDialog JDialogs work like Jframes (i.e. they are windows with content..) –setContentPane() –setDefaultCloseOperation() –setVisible() –pack() New –setLocationRelativeTo ( Component) centers the dialog over the specified component

14 Passing data between windows In an application data needs to pass from window to window…

15 Passing data between windows Different ways: include methods in dialog class to get data from the dialog, getXXX() –for simple data –Not too much data to be transferred use an object –A special object to store all info to be transferred to/from dialogue –Good for more complicated data structures/validation –More flexible –Usually referred to as a “data transfer object”

16 Data Transfer Object Create an object that encapsulates all data to be transferred between windows Just define the data as attributes, and put in get /set methods The contents of the object completely depends on what data you’re transferring between dialog and frame. E.g. transferring a “find” and “replace” string

17 Data transfer object (DTO) public class DTO{ private String findStr = “”; private String replStr = “”; public String getFindStr(){ return findStr; } public String getReplStr(){ return replStr; } public void setAll(String fs, String rs){ this.findStr = fs; this.replStr = rs; } E.g. transferring a “find” and “replace” string: the DTO Needs to hold both strings, and provide methods for setting and retrieving them (getting)

18 How to implement a dialog with data transfer So to use a dialog that requires passing data. E.g. This is the dialog This is the frame that calls the dialog (clicking the “add” button) Need 1.. Code in the frame that creates the dialog 2.. A dialog class 3.. A Data transfer class to hold the data

19 Implementing a dialog with data transfer public class MyDialogTest extends JFrame implements SomeSortOfListener { // Include the DTO and Dialog as attributes in your frame DTO dto; MyDialog dialog = null; … } JFrame as normal:

20 (1) JFrame Class include listener and event handler that initiates dialog instantiate DTO object and pass to dialog in the constructor /** in event handler… e.g. in Action Performed method after user has clicked a button or menu bar that results in a dialog being displayed….**/ if (dialog == null) { // first time user selects to open dialog, instantiate the dialog dialog = new MyDialog(this, dto); } Else / not first time, dialog already exists, just show the dialog (set visible) // Control then returns from displaying dialog // extract the data from the DTO and use it – control comes back to here… String findStr = dto.getFindStr(); // etc …

21 (2) Data Transfer Object Data transfer object Holds the various data to be transferred – as discussed In this case… first name/ surname

22 class MyDialog extends JDialog implements SomeSortofListener{ private DTO dto; private JFrame owner; MyDialog(parameters in to the contructor..){ super(parameters in to Jdialog superclass constructor); this.dto= dto; // format the content pane with controls… // set relative position to owner etc.. } public void eventHandler(Event e) { // validate data entered from dialog controls (optional) // extract all data from dialog controls and // pass to DTO } public void showDialog(){ // Make the dialog visible } better to hide and show dialog than instantiate it each time it is requested (3) Dialog Class

23 Predefined Dialogs There are dialogs set up already for your use because they are used so often e.g.: JColorChooser –static method that displays colour dialog and returns colour/null JFileChooser includes methods to open the standard Open dialog and Save dialog

24 JFileChooser //Create a file chooser JFileChooser fc = new JFileChooser();... //In response to a button click: int returnVal = fc.showOpenDialog(aComponent); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); //This is where a real application would //open the file. … } else { // user cancelled from dialog … }

25 Handling Focus A component gains focus when –the user clicks it –the user tabs to it –the user interacts with it FocusEvents are fired whenever a component gains/loses focus.. Examples of what might happen on losing focus? FocusListener will ‘listen’ for FocusEvents on components it is registered with –2 event handlers: public void focusGained(FocusEvent e) public void focusLost(FocusEvent e)

26 Handling Focus To give focus to a component: –Use the requestFocusInWindow() method To make sure a particular textfield has focus whenever the window is displayed: –override the windowActivated event handler of the WindowListener addWindowListener(new WindowAdapter() { public void windowActivated(WindowEvent e) { textField.requestFocusInWindow(); } });

27 Example // text field components private JTextField firstname, surname; // button components private JButton save, cancel; ……….. // Make firstname textField get the focus whenever frame is activated. addWindowListener(new WindowAdapter() { public void windowGainedFocus(WindowEvent e) { firstname.requestFocusInWindow(); save.setEnabled(false); } });

28 Using Focus for Handling Input “Engineering for Errors” –E.g. Enabling/Disabling buttons when user can/should not use them (e.g. in Animal Match game..) –E.g. Validate input before enabling Save button Use FocusListener: –Register the focusListener with the textfields –Override the focusLost event handler to check that there is text is all required fields If so, enable Save –Remember to disable Save each time window is displayed

29 LAB On any frame that you’ve developed so far, add functionality so that when the “X “ button is pressed on the top right, a message is displayed asking the user if they are sure they want to exit.

30 LAB Then.. Create a frame that shows a dialog when the “add” button is pressed. The dialog takes a Firstname, surname, and displays it back on the frame


Download ppt "Building Applications Dialogs Passing data between windows Validating Input using FocusListeners."

Similar presentations


Ads by Google