Here's my code. Why the values are not going into the second JList? Also, the second Jlist is not visible. All imports are in place and not visible in the following code:
public class Gui extends JFrame {
private JList l;
private JList l2;
private JButton b1;
public String [] cd = {"Album a", "Album b", "Album c", "Album d","Album e", "Album f", "Album g", "Album h"};
public Gui(){
super("Mover");
l = new JList (cd);
l.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
b1 = new JButton("Move");
b1.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
l2.setListData(l.getSelectedValuesList());
}
}
);
setLayout(new FlowLayout());
add(new JScrollPane(l));
add(b1);
add(new JScrollPane(l2));
}
}
Your JList l2
is null, You need to initialise it to before you can use it like you do with l
Add
l2 = new JList (/*your list2 or empty*/);
just after your initialisation of the JList l
Edit As I now see was mentioned in the comments
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.