Java Program to create a JList from ArrayList
In this program, you will learn how to create a JList from ArrayList in java.
DefaultListModel model = new DefaultListModel();
jl = new JList(model);
Example: How to create a JList from ArrayList in java.
import javax.swing.*;
import java.util.ArrayList;
import java.awt.*;
class Test extends JFrame {
JList jl;
JLabel lb;
Test() {
DefaultListModel model = new DefaultListModel();
jl = new JList(model);
jl.setBounds(50, 100, 180, 300);
add(jl);
ArrayList<String> list = new ArrayList();
list.add("Inida");
list.add("China");
list.add("Aus");
list.add("Japan");
int i = 0;
for (String st : list) {
model.add(i, st);
i++;
}
lb = new JLabel("Example of JList from ArrayList by Xiith");
lb.setFont(new Font("Serif", Font.PLAIN, 24));
lb.setBounds(40, 40, 450, 30);
add(lb);
setLayout(null);
setSize(600, 500);
setVisible(true);
}
public static void main(String args[]) {
Test t = new Test();
}
}