0

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);
}

}

Pafo
  • 29
  • 4
  • 1
    Please clarify your question so that you're asking one distinct and clear question. Right now it's a muddle with lots of little questions -- what is your **main** problem? How is your current code not working to solve this one main problem? – Hovercraft Full Of Eels Feb 09 '18 at 15:56
  • My main problem is that when i move items from the left list only 1 item can show in the right list. – Pafo Feb 09 '18 at 15:58
  • Yes, that's how you've written your code, to get only a single index. See the duplicate please. – Hovercraft Full Of Eels Feb 09 '18 at 15:59
  • I am looking at the second comment, but i am having a hard time understanding it. Can you explain me? – Pafo Feb 09 '18 at 16:01
  • Hard time understanding *what*? Have you looked at the duplicate used to close this question? That's where your answer is. Again, you must be specific about your questions and problems here. – Hovercraft Full Of Eels Feb 09 '18 at 16:03
  • The way you wrote your code only allows for the transfer of one item from the left list. This is why only one appears on the right list. – Micheal O'Dwyer Feb 09 '18 at 16:05
  • Okey, here my questions: 1. What is the use of DefaulListModel? 2. Why using Iterator, instead of for cycle? 3. What does this code this code - > for (Iterator it = jList1.getSelectedValuesList().iterator(); it.hasNext();) does? – Pafo Feb 09 '18 at 16:07
  • How can i fix it to allow more than one item? I've changed ListSelectionModel.MULTIPLE_INTERVAL_SELECTION . It was Single interval, but still the same. – Pafo Feb 09 '18 at 16:08
  • 1. DefaulListModel is a possible model class for your JList -- if you need to know what method it has, check the API. If you're unsure of the entire use of Model-View for Swing components, check out the [JList Tutorial](https://docs.oracle.com/javase/tutorial/uiswing/components/list.html). 2. You can loop any way you want to loop. 3. [this link](https://stackoverflow.com/questions/18410035/ways-to-iterate-over-a-list-in-java) – Hovercraft Full Of Eels Feb 09 '18 at 16:11
  • Okey, but can you tell me why i can only transfer one item? – Pafo Feb 09 '18 at 16:14
  • because that is how you've written your code. – Hovercraft Full Of Eels Feb 09 '18 at 16:20

0 Answers0