Program ComboBox
/**
* @(#)DemoComboBox.java
*
*
* @author IrwanChortex
* @version 1.00 2010/7/4
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class DemoComboBox implements ActionListener {
public JLabel labelNama,labelLahir,labelTanggal,labelBulan, labelTahun;
public JTextField tfNama,tfInfo;
public JComboBox comboTanggal, comboBulan, comboTahun;
public JButton btnProses;
public DemoComboBox() {
String s1 = "Nama :";
labelNama = new JLabel(s1);
labelNama.setLocation(10,10);
labelNama.setSize(labelNama.getPreferredSize());
tfNama = new JTextField(25);
tfNama.setLocation(10,30);
tfNama.setSize(tfNama.getPreferredSize());
String s2 = "Tanggal Lahir :";
labelLahir = new JLabel(s2);
labelLahir.setLocation(10,55);
labelLahir.setSize(labelLahir.getPreferredSize());
labelTanggal = new JLabel("Hari ke :");
labelTanggal.setLocation(35,75);
labelTanggal.setSize(labelTanggal.getPreferredSize());
comboTanggal = new JComboBox();
comboTanggal.setLocation(35,95);
comboTanggal.setSize(labelTanggal.getPreferredSize());
for (int i=0;i<31;i++)>
comboTanggal.addItem(new String().valueOf(i+1));
}
labelBulan = new JLabel("Bulan :");
labelBulan.setLocation(95,75);
labelBulan.setSize(labelBulan.getPreferredSize());
comboBulan = new JComboBox();
comboBulan.setLocation(95,93);
comboBulan.setSize(labelLahir.getPreferredSize());
String[]bulan={"Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember"};
for (int i=0;i
comboBulan.addItem(bulan[i]);
}
labelTahun = new JLabel("Tahun :");
labelTahun.setLocation(190,75);
labelTahun.setSize(labelTahun.getPreferredSize());
comboTahun = new JComboBox();
comboTahun.setLocation(190,93);
comboTahun.setSize(labelLahir.getPreferredSize());
for (int i=1960;i<2007;i++)>
comboTahun.addItem(new String().valueOf(i));
}
btnProses = new JButton("Porses Data");
btnProses.setLocation(305,25);
btnProses.setSize(btnProses.getPreferredSize());
btnProses.addActionListener(this);
btnProses.setMnemonic('P');
tfInfo = new JTextField(50);
tfInfo.setLocation(90,255);
tfInfo.setSize(255,25);
tfInfo.setEditable(false);
}
public void createAndShowGUI(){
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Contoh JComboBox IRWAN");
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(labelNama);
frame.getContentPane().add(tfNama);
frame.getContentPane().add(labelLahir);
frame.getContentPane().add(labelTanggal);
frame.getContentPane().add(comboTanggal);
frame.getContentPane().add(labelBulan);
frame.getContentPane().add(comboBulan);
frame.getContentPane().add(labelTahun);
frame.getContentPane().add(comboTahun);
frame.getContentPane().add(btnProses);
frame.getContentPane().add(tfInfo);
frame.setBounds(0,0,450,350);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == btnProses) {
String s = tfNama.getText() +", Lahir pada " +
comboTanggal.getSelectedItem()+" "+
comboBulan.getSelectedItem()+" "+comboTahun.getSelectedItem();
tfInfo.setText(s);
}
}
public static void main(String[]args){
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run(){
DemoComboBox app = new DemoComboBox();
app.createAndShowGUI();
}
});
}
}
// OUTPUT
No comments