Hi
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import java.text.SimpleDateFormat;
public class ComboBoxList extends JPanel implements ActionListener {
static JFrame frame;
JLabel result;
String currentdate;
String list;
public ComboBoxList() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
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"
};
currentdate = patternExamples[0];
JLabel lable1 = new JLabel("Enter the pattern string or");
JLabel lable2 = new JLabel("select one from the list:");
JComboBox patternList = new JComboBox(patternExamples);
patternList.setEditable(true);
patternList.addActionListener(this);
//Create the UI for displaying result.
JLabel resultLabel = new JLabel("Current Date/Time", JLabel.LEADING);
result = new JLabel(" ");
result.setForeground(Color.black);
result.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.black),
BorderFactory.createEmptyBorder(5,5,5,5)
));
//Lay out everything.
JPanel jpanel = new JPanel();
jpanel.setLayout(new BoxLayout(jpanel, BoxLayout.PAGE_AXIS));
jpanel.add(lable1);
jpanel.add(lable2);
patternList.setAlignmentX(Component.LEFT_ALIGNMENT);
jpanel.add(patternList);
JPanel resultPanel = new JPanel(new GridLayout(0, 1));
resultPanel.add(resultLabel);
resultPanel.add(result);
jpanel.setAlignmentX(Component.LEFT_ALIGNMENT);
resultPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
add(jpanel);
add(Box.createRigidArea(new Dimension(0, 10)));
add(resultPanel);
setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
reformat();
} //constructor
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
String newSelection = (String)cb.getSelectedItem();
currentdate = newSelection;
reformat();
}
// date format taday
public void reformat() {
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(currentdate);
try {
String dateString = formatter.format(date);
result.setForeground(Color.black);
result.setText(dateString);
}
catch (IllegalArgumentException e) {
result.setForeground(Color.red);
result.setText("Error: " + e.getMessage());
}
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("ComboBoxList example using in swing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new ComboBoxList();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.setSize(300 ,200);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
----------------------------------------
Please visit for more information.
http://www.roseindia.net/java/example/java/swing/Thanks.
Amardeep