import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ComboBoxDemo2 implements ActionListener { private JFrame frame; private JComboBox cb1, cb2, cb3; private JLabel label; public ComboBoxDemo2() { frame = new JFrame("A Simple Swing App"); //frame.setSize(600, 400); Toolkit tk = frame.getToolkit(); Dimension dim = tk.getScreenSize(); int width = (int)dim.getWidth(); int height = (int)dim.getHeight(); frame.setSize(width, height); frame.setLayout(new FlowLayout()); String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; cb1 = new JComboBox(); cb2 = new JComboBox(months); cb3 = new JComboBox(); for(int i = 1; i<=31; i++){ cb1.addItem(i); } for(int i = 1970; i<2048; i++){ cb3.addItem(i); } cb1.addActionListener(this); cb2.addActionListener(this); cb3.addActionListener(this); frame.add(cb1); frame.add(cb2); frame.add(cb3); label = new JLabel("I show the selected Date"); label.setFont(new Font("Verdana", Font.BOLD, 18)); label.setForeground(Color.RED); frame.add(label); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public void actionPerformed(ActionEvent ae) { String message = ""; message += (Integer)cb1.getSelectedItem()+", "; message += (String)cb2.getSelectedItem()+", "; message += (Integer)cb3.getSelectedItem(); label.setText(message); } public static void main(String[] args) { new ComboBoxDemo2(); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter