Selasa, 04 Oktober 2011

Program ComboBox Java

Contoh program Java berikut ini mendemonstrasikan bagaimana membuat inputan combo box (select box) serta penerapan listener di dalam object Combo Box. Untuk membuat combo box kita dapat menggunakan class JComboBox. Program akan menampilkan pilihan combo box berisi nama-nama file gambar dan jika dipilih maka gambar yang bersangkutan akan ditampilkan.
Berikut ini tampilannya:


01import java.awt.*;
02import java.awt.event.*;
03import javax.swing.*;
04
05public class ComboBoxTest extends JFrame {
06
07    private JComboBox cmbImage;
08    private JLabel label;
09    private String arrGambar[] = {"Blushing.png", "Cool.png", "Happy.png","Smile.png","Winking.png"};
10    private Icon arrIcon[] = { new ImageIcon(arrGambar[0]),
11                   new ImageIcon(arrGambar[1]),
12                   new ImageIcon(arrGambar[2]),
13                   new ImageIcon(arrGambar[3]),
14                   new ImageIcon(arrGambar[4])
15                 };
16
17    public ComboBoxTest() {
18
19        super ("Mencoba Combo Box");
20        Container container = getContentPane();
21        container.setLayout(new FlowLayout());
22        cmbImage = new JComboBox (arrGambar);
23        cmbImage.setMaximumRowCount(3);
24
25        cmbImage.addItemListener(
26            new ItemListener() {
27                public void itemStateChanged (ItemEvent e) {
28                    if (e.getStateChange() == ItemEvent.SELECTED)
29                        label.setIcon(arrIcon[cmbImage.getSelectedIndex()]);
30                }
31            } //end anonimous inner class
32        );
33
34        container.add(cmbImage);
35        label = new JLabel (arrIcon[0]);
36        container.add(label);
37
38        setSize (350,400);
39        setLocationRelativeTo(null);
40        setVisible(true);
41    }
42
43    public static void main (String args[]) {
44        ComboBoxTest test = new ComboBoxTest ();
45        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
46    }
47
48} //end of class



source : google

Contoh Program Java

Popup menu merupakan menu yang akan ditampilkan saat kita melakukan klik kanan di window. Program berikut ini merupakan contoh program membuat popup menu di Java. Class yang digunakan adalah JPopupMenu.
Berikut ini tampilan programnya:



 erikut ini source code programnya:
01import java.awt.*;
02import java.awt.event.*;
03import javax.swing.*;
04 
05public class PopupMenuTest extends JFrame {
06    private JRadioButtonMenuItem items[];
07    private final Color colorValues[] = {Color.BLUE, Color.YELLOW, Color.RED};
08    private JPopupMenu popMenu;
09 
10    public PopupMenuTest() {
11        super ("Menu Popup");
12 
13        ItemHandler handler = new ItemHandler();
14        String colorNames[] = {"Biru", "Kuning", "Merah"};
15 
16        ButtonGroup colorGroup = new ButtonGroup();
17        popMenu = new JPopupMenu();
18        items = new JRadioButtonMenuItem [colorValues.length];
19 
20        for (int i = 0; i < items.length; i++) {
21            items[i] = new JRadioButtonMenuItem (colorNames[i]);
22            popMenu.add (items[i]);
23            colorGroup.add (items[i]);
24            items[i].addActionListener(handler);
25           }
26 
27        getContentPane().setBackground(Color.WHITE);
28 
29        addMouseListener(
30            new MouseAdapter() {
31                public void mousePressed (MouseEvent e) {
32                    showPopupMenu (e);
33                }
34 
35                public void mouseReleased (MouseEvent e) {
36                    showPopupMenu (e);
37                }
38 
39                private void showPopupMenu(MouseEvent e) {
40                    if (e.isPopupTrigger())
41                        popMenu.show(e.getComponent(), e.getX(), e.getY());
42                }
43            } //end of anonymous class
44        ); //end of addMouseListener
45 
46        setSize (400,300);
47        setLocationRelativeTo (null);
48        setVisible (true);
49    }
50 
51    public static void main (String args[]) {
52        JFrame.setDefaultLookAndFeelDecorated(true);
53        PopupMenuTest test = new PopupMenuTest();
54        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
55    }  
56 
57    private class ItemHandler implements ActionListener {
58        public void actionPerformed (ActionEvent e) {
59            //
60            for (int i = 0; i < items.length; i++) {
61                if (e.getSource() == items[i]) {
62                    getContentPane().setBackground (colorValues[i]);
63                    return;
64                }
65            }
66        }
67    }
68}