Add string array to JList in Java
Description
The following code shows how to add string array to JList.
Example
import java.awt.FlowLayout;
//from w ww . ja va 2 s.c om
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] selections = { "green", "java2s.com", "orange", "dark blue" };
JList list = new JList(selections);
frame.add(new JScrollPane(list));
frame.pack();
frame.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »