Listing 14.1 Custom Date and Time Dialog Box using JDialog (TJDialog.java) /* * * */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.text.*; public class TJDialog extends JApplet implements ActionListener { // 1. Declare the underlying frame to which the // dialog is attached. Frame frame = null; public void init() { // 2. Get a handle on the applet's content pane. Container container = this.getContentPane(); // 3. Retrieve the underlying frame of the applet. // See snippet-5 for the details of getParentFrame(). frame = getParentFrame(container); // 4. Add a button to bring up the dialog box. JButton button = new JButton("Show Date and Time"); button.addActionListener(this); container.add(button, BorderLayout.SOUTH); } // 5. Retrieve the frame of the applet's container. public Frame getParentFrame(Component comp) { if (comp instanceof Frame) return (JFrame)comp; for (Component c = comp; c != null; c = c.getParent()) { // Check only for the Frame, not the JFrame! if (c instanceof Frame) return (Frame)c; } return null; // if there are no frames } // 6. Display the date and time for the current locale. public String showDateAndTime() { DateFormat dateAndTimeFormat = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.LONG); String dateAndTime = dateAndTimeFormat.format(new Date()); return dateAndTime; } // 7. When the button is displayed on the applet is operated… public void actionPerformed(ActionEvent ae) { String date_time = showDateAndTime(); // When the applet's button is clicked, create the Swing // dialog box and display it. DateDialog dialog = new DateDialog(frame, date_time, true); } } // 8. Create a custom dialog box that extends JDialog // with a title and message. class DateDialog extends JDialog implements ActionListener { Icon icon = new ImageIcon("clock.gif"); public DateDialog(Frame frame, String msg, boolean modal) { // 9. Call the JDialog constructor. super(frame, "Date & Time Dialog" , modal); // 10. Set the background color of DateDialog. this.getContentPane().setBackground(Color.white); // 11. Create a confirmation button. JButton button = new JButton("OK"); button.setPreferredSize(new Dimension(80, 25)); button.addActionListener(this); // 12. Create a label to display the time and date. JLabel label = new JLabel(msg, icon, JLabel.CENTER); label.setFont(new Font("Dialog", Font.BOLD, 22)); // 13. Add the message label and OK button to the dialog box. this.getContentPane().add(label, BorderLayout.CENTER); Box box = Box.createHorizontalBox(); box.add(box.createHorizontalGlue()); box.add(button); box.add(box.createHorizontalGlue()); this.getContentPane().add(box, BorderLayout.SOUTH); // 14. Resize the dialog box and position it at the center of // the applet. setSize(300,150); Dimension dialogDim = getSize(); Dimension frameDim = frame.getSize(); Dimension screenSize = getToolkit().getScreenSize(); Point location = frame.getLocation(); location.translate( (frameDim.width-dialogDim.width)/2, (frameDim.height-dialogDim.height)/2); location.x = Math.max( 0, Math.min(location.x, screenSize.width-getSize().width)); location.y = Math.max(0, Math.min(location.y, screenSize.height-getSize().height)); setLocation(location.x, location.y); this.show(); } // 15. When the OK button on the dialog box is clicked. public void actionPerformed(ActionEvent ae) { this.dispose(); } } Listing 14.2 Date and Time Message Option Pane (TJOptionPane1.java) /* * * */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.text.*; public class TJOptionPane1 extends JApplet { Container container = null; public void init() { // 1. Get a handle on the applet's content pane. container = this.getContentPane(); // 2. Add a box to the content pane. Box box = new Box(BoxLayout.X_AXIS); container.add(box); // 3. Create a button and add it to the box. JButton button = new JButton("Show Date and Time"); button.setPreferredSize(new Dimension(200,25)); button.addActionListener(new ButtonListener()); box.add(Box.createGlue()); box.add(button); box.add(Box.createGlue()); } // 4. Listener class that displays a message dialog box. class ButtonListener implements ActionListener { String title = "Example Message Dialog"; int messageType = JOptionPane.INFORMATION_MESSAGE; int optionType = JOptionPane.DEFAULT_OPTION; Object message = null; Object[] options = {ėThanksî}; String dateAndTime = null; public void actionPerformed(ActionEvent e) { // 5. Obtain the date and time. dateAndTime = showDateAndTime(); message = "The Time is " + dateAndTime; // 6. Create an option pane with specified attributes. JOptionPane pane = new JOptionPane(message, messageType, optionType, null, options); // 7. Create and show the dialog box to display the message. JDialog dialog = pane.createDialog(container, title); dialog.show(); // 8. Reset the message to null. message = null; } // 9. Obtain the current date and time with the format // of the current locale. public String showDateAndTime() { DateFormat formatter = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.LONG); String dateAndTime = formatter.format(new Date()); return dateAndTime; } } } Listing 14.3 TJOptionPane2.java /* * * */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.text.*; public class TJOptionPane2 extends JApplet { Container container = null; public void init() { // 1. Get a handle on the applet's content pane. container = this.getContentPane(); // 2. Add a box to the content pane. Box box = new Box(BoxLayout.X_AXIS); container.add(box); // 3. Add a button to the box. JButton button = new JButton("Show Time"); button.setPreferredSize(new Dimension(150,25)); button.addActionListener(new ButtonListener()); box.add(Box.createGlue()); box.add(button); box.add(Box.createGlue()); } // 4. The listener class. class ButtonListener implements ActionListener { // 5. Argument values for the confirmation dialog box. Object confirmText = "Do You Wish To See Date Also?"; String confirmTitle = "Date Confirmation Dialog"; int optionType = JOptionPane.YES_NO_OPTION; int messageType1 = JOptionPane.QUESTION_MESSAGE; // 6. Argument values for the message dialog box. Object information = null; String title = "Message Display Dialog"; int messageType2 = JOptionPane.INFORMATION_MESSAGE; // 7. Option selected. // If the selection is 'yes', selectedValue = 0; // If the selection is 'No', selectedValue = 1; int selectedValue; public void actionPerformed(ActionEvent e) { // 8. Display the confirmation dialog box. selectedValue = JOptionPane.showConfirmDialog(container, confirmText, confirmTitle, optionType, messageType1); // 9. Fetch the time or date and time. information = fetchInformation(); // 10. Display the message. JOptionPane.showMessageDialog(container, information, title, messageType2); } // 11. Returns the time or date and time depending // on the Yes or No choice made. public String fetchInformation() { DateFormat formatter = null; if (selectedValue == 0) { //If it is Yes. formatter = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.LONG); } else if(selectedValue == 1) { //If it is No. formatter = DateFormat.getTimeInstance( DateFormat.LONG); } // Format the time or date and time and return. return(formatter.format(new Date())); } } } Listing 14.4 TJOptionPane3.java /* * * */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.text.*; public class TJOptionPane3 extends JApplet { Container container = null; public void init() { // 1. Get a handle on the applet's content pane. container = this.getContentPane(); // 2. Add a box to the content pane. Box box = new Box(BoxLayout.X_AXIS); container.add(box); // 3. Add a button to display the time. JButton button = new JButton("Click Me"); button.setPreferredSize(new Dimension(150,25)); button.addActionListener(new ButtonListener()); box.add(Box.createGlue()); box.add(button); box.add(Box.createGlue()); } // 4. The button listener class. class ButtonListener implements ActionListener { // 5. Argument values for the option pane dialog box. String title1 = "Example Input Dialog"; String message1 = "Make a Selection"; int messageType1 = JOptionPane.QUESTION_MESSAGE; Icon icon = null; Object[] options = {"Date Only", "Time Only", "Date and Time"}; Object optionSelected = options[1]; // 6. Selection status of check box. Object selectedValue; // This field stores the selection made in the check box // as a string when you press the 'ok' button. If you press // the 'cancel' button, the 'null' value is received. // 7. Argument values for the message dialog box. Object message2 = null; String title2 = "Example Message Dialog"; int messageType2 = JOptionPane.INFORMATION_MESSAGE; public void actionPerformed(ActionEvent e) { // 8. Display the input dialog box to make a selection. selectedValue = JOptionPane.showInputDialog( container, message1, title1, messageType1, null, options, options[1]); // 9. Get the date, time, or date and time. if (selectedValue != null) { // i.e., if not 'cancel' message2 = getInformation(); // 10. Finally, display the message dialog box. JOptionPane.showMessageDialog(container, message2, title2, messageType2); } } // 11. Method to retrieve the information. public String getInformation() { DateFormat formatter = null; if (selectedValue == "Time Only") { formatter = DateFormat.getTimeInstance( DateFormat.SHORT); } else if (selectedValue == "Date Only") { formatter = DateFormat.getDateInstance( DateFormat.SHORT); } else if (selectedValue == "Date and Time") { formatter = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT); } // Now format the time or date and time and return. String information = formatter.format(new Date()); return information; } } }