Skip to content

Instantly share code, notes, and snippets.

@arbo77
Created August 10, 2012 23:50
Show Gist options
  • Save arbo77/3319041 to your computer and use it in GitHub Desktop.
Save arbo77/3319041 to your computer and use it in GitHub Desktop.
Simple Java GUI example (ComboBox & ListBox)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class Combo extends JFrame{
private JLabel lbl1 = new JLabel("Bilangan : ");
private JTextField tBil = new JTextField();
private JButton btnTambah = new JButton("Tambah");
private DefaultComboBoxModel ganjil = new DefaultComboBoxModel();
private JComboBox cmb = new JComboBox(ganjil);
private DefaultListModel genap = new DefaultListModel();
private JList list = new JList(genap);
private JButton btnTutup = new JButton("Tutup");
public Combo(){
setTitle("ComboBox & ListBox");
setSize(350,300);
setLocation(new Point(250,250));
setLayout(null);
setResizable(false);
initComponent();
initEvent();
}
private void initComponent(){
lbl1.setBounds(20,20,60,20);
tBil.setBounds(90,20,80,20);
btnTambah.setBounds(180,20,50,20);
cmb.setBounds(20,60,150,20);
list.setBounds(180,60,140,150);
btnTutup.setBounds(250,220,70,20);
add(lbl1);
add(tBil);
add(btnTambah);
add(cmb);
add(list);
add(btnTutup);
}
private void initEvent(){
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(1);
}
});
btnTambah.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnTambahClick(e);
}
});
btnTutup.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnTutupClick(e);
}
});
}
private void btnTambahClick(ActionEvent evt){
Integer p;
try
{
p = Integer.parseInt(tBil.getText());
if (p % 2 == 0)
{
genap.insertElementAt(p.toString(),0);
}else{
ganjil.insertElementAt(p.toString(),0);
}
}
catch (Exception e)
{
System.out.println(e);
JOptionPane.showMessageDialog(null,
e.toString(),
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
private void btnTutupClick(ActionEvent evt){
System.exit(0);
}
}
class frmCombo {
public static void main(String[] args)
{
Combo f = new Combo();
f.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment