What i need to do is basically that - http://www.esus.com/images/jlistex.jpg , with the difference that I have to make textArea from where i will add the items to the Jlist.I haven't started doing that, because i have another problem.
First of all, I am having a hard time with those designs, so i am using flowLayout for simplicity, but it would be nice if someone tells me how to make it like the one in the picture. I've managed to move items from leftList to rightList, but i can't add more than 1. This is one of the problems. DefaultListModel is something i added recently, honestly i am not sure what it does. The last thing i have to fix is the buttons, which i move the items. I am probably doing something awfully wrong, because i can somehow remove everything without selecting any item on the list. The items i have added are just to see if my program is working.
public class Lesson5 extends JFrame{
private JList leftList;
private JList rightList;
private JButton moveRight;
private JButton moveLeft;
private JTextArea textArea;
private JButton add;
private DefaultListModel model;
private DefaultListModel model2;
Lesson5(){
super("Lesson5");
setLayout(new FlowLayout());
model=new DefaultListModel();
model2=new DefaultListModel();
leftList=new JList(model);
model.addElement("Something");
model.addElement("Another");
leftList.setVisibleRowCount(5);
leftList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
add(new JScrollPane(leftList));
moveRight=new JButton(">");
moveLeft=new JButton("<");
add(moveLeft);
add(moveRight);
moveRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int index=leftList.getSelectedIndex();
rightList.setListData(leftList.getSelectedValuesList().toArray());
if(index!=-1) {
model.remove(index);
}
}
});
moveLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int index=leftList.getSelectedIndex();
leftList.setListData(rightList.getSelectedValuesList().toArray());
if(index!=-1) {
model.remove(index);
}
}
});
rightList=new JList(model2);
rightList.setVisibleRowCount(5);
rightList.setFixedCellHeight(100);
rightList.setFixedCellHeight(20);
rightList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
add(new JScrollPane(rightList));
}
public static void main(String[] args) {
Lesson5 lesson5=new Lesson5();
lesson5.setVisible(true);
lesson5.setBounds(100,100,350,300);
}
}