Import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import Java.text.SimpleDateFormat;
/**
* <p>title:combobox dropdown field Demo </p>
* <p>description: Format today's date </p>
by selecting or entering a date format
* <p>copyright:copyright (c) 2003</p>
* <p>Filename:ComboBoxDemo.java</p>
* @version 1.0
*/
public class Comboboxdemo extends JPanel
implements ActionListener {
static JFrame frame;
JLabel result;
String Currentpattern;
/**
*<br> Method Description: constructor. Initializing the form widget
*<br> input Parameters:
*<br> return type:
*/
public Comboboxdemo () {
SetLayout (This, Boxlayout.page_axis) (new boxlayout);
string[] Patternexamples = {
"dd mmmmm yyyy",
"DD. Mm.yy ",
"Mm/dd/yy",
"yyyy. Mm.dd g´at´hh:mm:ss z ",
"EEE, MMM d,´´yy",
"h:mm a",
"H:mm:ss:sss",
"k:mm a,z",
"yyyy. MMMMM.DD GGG hh:mm AAA "
};
currentpattern = patternexamples[0];
//Set up a specification user interface
JLabel patternLabel1 = new JLabel ("Enter a character format or");
JLabel PatternLabel2 = new JLabel ("Select one from the Drop-down list:");
JComboBox patternlist = new JComboBox (patternexamples);
patternlist.seteditable (TRUE),//Callout here ComboBox editable
Patternlist.addactionlistener (this);
//Create a display result user interface
JLabel ResultLabel = new JLabel ("Current date/Time",
jlabel.leading);//equivalent to left
result = new JLabel ("");
Result.setforeground (Color.Black);
Result.setborder (Borderfactory.createcompoundborder (
Borderfactory.createlineborder (color.black),
Borderfactory.createemptyborder (5,5,5,5)
));
//Layout Components
JPanel Patternpanel = new JPanel ();
patternpanel.setlayout (New BoxLayout (Patternpanel,
Boxlayout.page_axis));
Patternpanel.add (PATTERNLABEL1);
Patternpanel.add (PATTERNLABEL2);
Patternlist.setalignmentx (component.left_alignment);
Patternpanel.add (patternlist);
JPanel Resultpanel = new JPanel (new GridLayout (0, 1));
Resultpanel.add (ResultLabel);
Resultpanel.add (Result);
Patternpanel.setalignmentx (component.left_alignment);
Resultpanel.setalignmentx (component.left_alignment);
Add (Patternpanel);
Add (Box.createrigidarea (new Dimension (0, 10));
Add (Resultpanel);
SetBorder (Borderfactory.createemptyborder (10,10,10,10));
reformat ();
}
/**
*<br> Method Description: Event handling
*<br> input Parameters:
*<br> return type:
*/
public void actionperformed (ActionEvent e) {
JComboBox cb = (JComboBox) e.getsource ();
String newselection = (string) cb.getselecteditem ();
Currentpattern = newselection;
reformat ();
}
/**
*<br> Method Description: Format and display today's date
*<br> input Parameters:
*<br> return type:
*/
public void reformat () {
Date today = new Date ();
SimpleDateFormat formatter =
new SimpleDateFormat (Currentpattern);
try {
String datestring = Formatter.format (today);
Result.setforeground (Color.Black);
Result.settext (datestring);
} catch (IllegalArgumentException iae) {
Result.setforeground (color.red);
Result.settext ("Error:" + iae.getmessage ());
}
}
/**
*<br> Method Description: Main method
*<br> input Parameters:
*<br> return type:
*/
public static void Main (string[] args) {
jframe.setdefaultlookandfeeldecorated (TRUE);
Create a form
frame = new JFrame ("Comboboxdemo");
frame.setdefaultcloseoperation (jframe.exit_on_close);
//Create a face plate container
jcomponent Newcontentpane = new Comboboxdemo ();
Newcontentpane.setopaque (TRUE);
Frame.setcontentpane (Newcontentpane);
//Display form
Frame.pack ();
frame.setvisible (TRUE);
}
}