Mari kita mengenal lebih jauh tentang Java
Kelanjutan dari komponen-komponen Swing namun ini merupakan contoh program dari JList
Contoh Program Sederhana JList
import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;
public class ListTest extends JFrame {
private JList lstColor;
private final String arrColorName[] =
{ "Black","Blue","Cyan","Dark Gray","Gray","Green","Light Gray",
"Magenta","Orange","Pink","Red","Yellow","White"
};
private final Color arrColor[] =
{ Color.BLACK,Color.BLUE,Color.CYAN,Color.DARK_GRAY,Color.GRAY,
Color.GREEN,Color.LIGHT_GRAY,Color.MAGENTA,Color.ORANGE,Color.PINK,
Color.RED,Color.YELLOW,Color.WHITE
};
private Container container;
public ListTest() {
super ("Mencoba JList");
container = getContentPane();
container.setLayout(new FlowLayout());
lstColor = new JList (arrColorName);
lstColor.setVisibleRowCount(5);
//hanya boleh pilih satu
lstColor.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
container.add(new JScrollPane (lstColor));
//container.add(lstColor);
lstColor.addListSelectionListener(
new ListSelectionListener() { //adanya di package javax.swing.event
public void valueChanged (ListSelectionEvent e) {
container.setBackground(arrColor[lstColor.getSelectedIndex()]);
}
} //end of class
);
setSize (400,300);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main (String args[]) {
ListTest test = new ListTest();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Jangan sampai salah memasukan nama warna yah... karena java menggunakan bahasa inggris jadi kalau kamu menginputnya Hijau maka java tidak mengenalnya, masukan inputannya Green. "ingat java itu case sensitif"