The SelectFormat.java file provides the interface to select a date format from the various formats provided by the interface.
Listing 2-2 shows the contents of the SelectFormat.java file:
/*Import required packages*/ import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.JOptionPane; import java.util.*; import javax.swing.event.*; import java.awt.GridLayout; /* Class: SelectFormat-Creates a user interface to set the date format Methods: actionPerformed(): Defines the operations to be performed, when the end user clicks any button */ public class SelectFormat extends JFrame implements ActionListener { /*Declare variables*/ Font f,f1; JPanel pane,buttonPanel; JRadioButton rb1, rb2, rb3, rb4, rb5, rb6; ButtonGroup bg; JLabel title; JButton ok,cancel; public static String retvalue="dd/mm/yyyy"; SpeechCalendar calendar = null; /* SelectFormat(): Default constructor of SelectFormat class Parameters: calendar: An object of SpeechCalendar class Return Type: NA */ public SelectFormat(SpeechCalendar calendar) { this.calendar = calendar; /*Initialize the object of JPanel class*/ pane=new JPanel(); /*Initialize a JPanel to add buttons*/ buttonPanel=new JPanel(); /* Set the layout of JPanel, buttonPanel */ buttonPanel.setLayout(new GridLayout(1, 2)); /* Initialize the JRadioButtons */ rb1=new JRadioButton("dd/mm/yy"); rb2=new JRadioButton("dd/mm/yyyy"); rb3=new JRadioButton("mm/dd/yy"); rb4=new JRadioButton("mm/dd/yyyy"); rb5=new JRadioButton("yy/mm/dd"); rb6=new JRadioButton("yyyy/mm/dd"); /* Initialize a ButtonGroup */ bg=new ButtonGroup(); /* Add JRadioButtons to the ButtonGroup */ bg.add(rb1); bg.add(rb2); bg.add(rb3); bg.add(rb4); bg.add(rb5); bg.add(rb6); /* Set the layout of the JPanel, pane */ pane.setLayout(new GridLayout(8, 1)); /* Create objects of Font class f= font("Verdana", Font.BOLD, 12); f1=new Font("Verdana", Font.BOLD, 10); /* Set the title of the SelectFormat window */ title=new JLabel("Select the format of the date"); title.setFont(f); /* Initialize objects of JButton class */ ok=new JButton("OK"); cancel=new JButton("Cancel"); /* Set font of buttons */ ok.setFont(f1); cancel.setFont(f1); /* Add action listener to the JButtons */ ok.addActionListener(this); cancel.addActionListener(this); /* Add JButtons to the buttonPanel */ buttonPanel.add(ok); buttonPanel.add(cancel); /* Set the contents of the JPanel, pane */ pane.add(title); pane.add(rb1); pane.add(rb2); pane.add(rb3); pane.add(rb4); pane.add(rb5); pane.add(rb6); pane.add(buttonPanel); getContentPane().add(pane); } /* actionPerformed(): Defines the actions to be performed when the user clicks a button Parameters: ev: An object of ActionEvent Return Type:NA */ public void actionPerformed(ActionEvent ev) { /* This code executes when the user clicks the OK button */ if(ev.getSource()==ok) { /* Retrieve all elements of the Buttongroup in an enumeration */ Enumeration enum=bg.getElements(); /* check if there exists any element in the enumeration */ while(enum.hasMoreElements()) { JRadioButton button=(JRadioButton)enum.nextElement(); /* Retrieve the label of the selected JRadioButton */ if (button.isSelected()) { retvalue=button.getText(); this.setVisible(false); calendar.setFormat(retvalue); } } } /* This code executes when the user clicks the Cancel button */ else if(ev.getSource()==cancel) { this.setVisible(false); } } }
Download this listing .
In the above code, the constructor of the SelectFormat class accepts an object of the SpeechCalendar class as an input parameter to invoke the methods of the SpeechCalendar class.
In Listing 2-2, the actionPerformed() method is defined, which acts as an event listener and activates an appropriate method based on the button an end user clicks after selecting a date format. When an end user clicks the Cancel button, the actionPerformed() method invokes the setVisible() method to hide the interface. When an end user clicks the OK button, the actionPerformed() method invokes the getElements() method to set the selected date format.
Figure 2-3 shows the user interface to select a date format: