title: Java, how to make duallist
body: <p>Here is my code. Trully i am trying to learn java but i don't get it. </p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.GridLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  import javax.swing.DefaultListModel;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JList;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  
  
  
  public class Dual{
      public static JFrame myframe;
      public JPanel mypanel;
      private JList sourceList;
  
        private DefaultListModel sourceListModel;
  
        private JList destList;
  
        private DefaultListModel destListModel;
  
        private JButton addButton;
  
        private JButton removeButton;
  
  
  
  
  
      public Dual(){
          myframe = new JFrame("Dual List Box");
          myframe.setDefaultCloseOperation(myframe.EXIT_ON_CLOSE);
          myframe.setSize(400, 300);
          myframe.setResizable(false);
          mypanel = new JPanel();
          myframe.getContentPane().add(mypanel);
          initScreen();
          data();
      }
  
      private void initScreen() {
          mypanel.setLayout(new GridLayout(0, 2));
          sourceListModel = new DefaultListModel();
          sourceList = new JList(sourceListModel);
  
          addButton = new JButton("&gt;&gt;");
          addButton.addActionListener(new AddListener());
          removeButton = new JButton("&lt;&lt;");
          //removeButton.addActionListener(new RemoveListener());
  
          destListModel = new DefaultListModel();
          destList = new JList(destListModel);
  
          JPanel leftPanel = new JPanel(new BorderLayout());
          leftPanel.add(new JLabel("Available Elements:"), BorderLayout.NORTH);
          leftPanel.add(new JScrollPane(sourceList), BorderLayout.CENTER);
          leftPanel.add(addButton, BorderLayout.SOUTH);
  
          JPanel rightPanel = new JPanel(new BorderLayout());
  
          rightPanel.add(new JLabel("Selected Elements:"), BorderLayout.NORTH);
          rightPanel.add(new JScrollPane(destList), BorderLayout.CENTER);
          rightPanel.add(removeButton, BorderLayout.SOUTH);
  
          mypanel.add(leftPanel);
          mypanel.add(rightPanel);
  
  
  
  
  
  
        }
      private void data(){
          //sourceListModel.firstElement(request.getParameter("t" + i));
          sourceListModel.addElement("ABC");
          sourceListModel.addElement(new String[] { "One", "Two", "Three" });
          sourceListModel.addElement(new String[] { "Four", "Five", "Six" });
          sourceListModel.addElement(new String[] { "Seven", "Eight", "Nine" });
          sourceListModel.addElement(new String[] { "Ten", "Eleven", "Twelve" });
          sourceListModel.addElement(new String[] { "Thirteen", "Fourteen", "Fifteen" });
          sourceListModel.addElement(new String[] { "Sixteen", "Seventeen", "Eighteen" });
          sourceListModel.addElement(new String[] { "Nineteen", "Twenty", "Thirty" });
          sourceListModel.addElement("GREHDH");
          destListModel.addElement("DER");
          destListModel.addElement("GRTD");
          destListModel.addElement("HRET");
      }
  
      private void showSourceSelected() {
          Object selected[] = sourceList.getSelectedValues();
          System.out.println(selected.length); 
          for (int i = selected.length - 1; i &gt;= 0; --i) {
              System.out.println(selected[i]);
            }
      }
      private void change1Selected() {
          Object selected[] = sourceList.getSelectedValues();
          int descout = destList.getModel().getSize();
          int c = 0;
          //System.out.println(selected.length); 
          for (int i = descout + 1; i &lt;=selected.length+descout; i++) {
              destListModel.addElement(selected);
  
              destListModel.addElement("belble"+c);
              c++;
          }
          for (int i = selected.length - 1; i &gt;= 0; --i) {
              sourceListModel.removeElement(selected[i]);
  
              //System.out.println(selected[i]);
            }
          //sourceList.getSelectionModel().clearSelection();
  
      }
          //myframe = new JFrame("Dual List Box ");
      //myframe.;
      private class AddListener implements ActionListener {
          public void actionPerformed(ActionEvent e) {
            Object selected[] = sourceList.getSelectedValues();
            showSourceSelected();
            change1Selected();
            //sourceList.getSelectionModel().clearSelection();
            //addDestinationElements(selected);
            //clearSourceSelected();
          }
        }
  }
  </code></pre>
  
  <p>Left button is running, as in topic i am trying to make duallist. 
  I don't know how to handel a single or multiply selection. 
  How to make adding a single or multiplay elements.
  How to switch betwen list without funny jlang.object or string blabla.</p>
  
  <p>Based on: <a href="http://www.java2s.com/Tutorial/Java/0240__Swing/DualListBoxSample.htm" rel="nofollow">Dual List Box</a> Sample : Dual List « Swing « Java Tutorial</p>
  
  <hr>
  
  <p>Edited
  New Code</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.GridLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  
  import javax.swing.DefaultListModel;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JList;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.event.ListSelectionEvent;
  import javax.swing.event.ListSelectionListener;
  
  
  
  public class Dual{
  public static JFrame myframe;
  public JPanel mypanel;
  private JList sourceList;
  
    private DefaultListModel sourceListModel;
  
    private JList destList;
  
    private DefaultListModel destListModel;
  
    private JButton addButton;
  
    private JButton removeButton;
  
  public Dual(){
      myframe = new JFrame("Dual List Box");
      myframe.setDefaultCloseOperation(myframe.EXIT_ON_CLOSE);
      myframe.setSize(400, 300);
      myframe.setResizable(false);
      mypanel = new JPanel();
      myframe.getContentPane().add(mypanel);
      initScreen();
      data();
  }
  
  private void initScreen() {
      mypanel.setLayout(new GridLayout(0, 2));
      sourceListModel = new DefaultListModel();
      sourceList = new JList(sourceListModel);
  
      addButton = new JButton("&gt;&gt;");
      addButton.addActionListener(new AddListener());
      removeButton = new JButton("&lt;&lt;");
      removeButton.addActionListener(new RemoveListener());
  
      destListModel = new DefaultListModel();
      destList = new JList(destListModel);
  
      ListSelectionListener listener1 = new ListSelectionListener(){
  
          JList b = null;
  
          @Override
          public void valueChanged(ListSelectionEvent e) {
              if (!e.getValueIsAdjusting()) {
  
              // TODO Auto-generated method stub
              //System.out.println(sourceselcted());
              if(e.getSource().equals(sourceList)){
              System.out.println("Source selcted");
  
                  if(b!=e.getSource()){
  
                      destList.clearSelection();
                  }
              }
              else{
              System.out.println("Destination selcted");
              if(b!=e.getSource()){
  
                  sourceList.clearSelection();
              }
              }
              b=(JList) e.getSource();
              //System.out.println(b);
          }
          }
  
      };
      destList.addListSelectionListener(listener1);
      sourceList.addListSelectionListener(listener1);
  
      JPanel leftPanel = new JPanel(new BorderLayout());
      leftPanel.add(new JLabel("Available Elements:"), BorderLayout.NORTH);
      leftPanel.add(new JScrollPane(sourceList), BorderLayout.CENTER);
      leftPanel.add(addButton, BorderLayout.SOUTH);
  
      JPanel rightPanel = new JPanel(new BorderLayout());
  
      rightPanel.add(new JLabel("Selected Elements:"), BorderLayout.NORTH);
      rightPanel.add(new JScrollPane(destList), BorderLayout.CENTER);
      rightPanel.add(removeButton, BorderLayout.SOUTH);
  
      mypanel.add(leftPanel);
      mypanel.add(rightPanel);
      /* sourceList.addMouseListener(new MouseAdapter() {
  
          int lastSelectedIndex;
  
          public void mouseClicked(MouseEvent e) {
  
              int index = sourceList.locationToIndex(e.getPoint());
  
              if (index != -1 &amp;&amp; index == lastSelectedIndex) {
                  sourceList.clearSelection();
              }
  
              lastSelectedIndex = sourceList.getSelectedIndex();
          }
      });
  
      */
  
  
  
  
    }
  private void data(){
      sourceListModel.addElement("ABC");
      sourceListModel.addElement("One" );
      sourceListModel.addElement("Four");
      sourceListModel.addElement("Seven");
      sourceListModel.addElement("Ten");
      sourceListModel.addElement("Thirteen");
      sourceListModel.addElement("Sixteen");
      sourceListModel.addElement("Nineteen");
      sourceListModel.addElement("GREHDH");
      destListModel.addElement("DER");
      destListModel.addElement("GRTD");
      destListModel.addElement("HRET");
  }
  
  private void showSourceSelected() {
      Object selectedcount[] = sourceList.getSelectedValues();
      System.out.println(selectedcount.length); 
      for (int i = selectedcount.length - 1; i &gt;= 0; --i) {
          System.out.println(selectedcount[i]);
        }
  }
  private void showSourceSelected2() {
      Object selectedcount[] = destList.getSelectedValues();
      System.out.println(selectedcount.length); 
      for (int i = selectedcount.length - 1; i &gt;= 0; --i) {
          System.out.println(selectedcount[i]);
        }
  }
  private int countlist(JList list){
      return list.getSelectedValues().length;
  
  }
  private void change1Selected() {
      Object selectedcount[] = sourceList.getSelectedValues();
      int count = selectedcount.length;
      Object selected = sourceList.getSelectedValue();
      //int descout = destList.getModel().getSize();
      //int c = 0;
      //System.out.println(selected.length); 
     // for (int i = descout + 1; i &lt;=descout+1; i++) {
      for(int i=0;i&lt;=count-1;i++){
          destListModel.addElement(selectedcount[i]);
          sourceListModel.removeElement(selectedcount[i]);
  
     // }
     // for (int i = selected.length - 1; i &gt;= 0; --i) {
  
      }   
          //System.out.println(selected[i]);
        //}
      //sourceList.getSelectionModel().clearSelection();      
  }
  private void change2Selected() {
      Object selectedcount[] = destList.getSelectedValues();
      int count = selectedcount.length;
      Object selected = destList.getSelectedValue();
      //int descout = destList.getModel().getSize();
      //int c = 0;
      //System.out.println(selected.length); 
     // for (int i = descout + 1; i &lt;=descout+1; i++) {
      for(int i=0;i&lt;=count-1;i++){
          sourceListModel.addElement(selectedcount[i]);
          destListModel.removeElement(selectedcount[i]);
  
          //destListModel.addElement("belble"+c);
          //c++;
     // }
     // for (int i = selected.length - 1; i &gt;= 0; --i) {
  
      }   
          //System.out.println(selected[i]);
        //}
      //sourceList.getSelectionModel().clearSelection();
  
  }
  
  private class AddListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {
        Object selected[] = sourceList.getSelectedValues();
        //showSourceSelected();
  
        System.out.println(countlist(sourceList));
        change1Selected();
        //sourceList.getSelectionModel().clearSelection();
        //addDestinationElements(selected);
        //clearSourceSelected();
      }
    }
  private class RemoveListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {
        Object selected[] = destList.getSelectedValues();
        //showSourceSelected2();
  
        System.out.println(countlist(destList));
        change2Selected();
        //sourceList.getSelectionModel().clearSelection();
        //addDestinationElements(selected);
        //clearSourceSelected();
      }
    }
  private boolean sourceselcted() {
      int sourcelength = sourceList.getSelectedValues().length;
      if(sourcelength&gt;=1)return true;
    return false;    
  }
  private boolean destselcted() {
      int sourcelength = destList.getSelectedValues().length;
      if(sourcelength&gt;=1)return true;
    return false;    
  }
  
  }
  </code></pre>
  
  <p>How to sort?? Are there any ready functions, or i had to make it myself??
  What are Jlist with Object//String declarations??
  What are difrences betwen DefaultListModel,AbstractListModel,ListModel and when to use what??
  How to make Jlist with subtype data ABC - Name: xxx,ForVesrion: yyy,Type: zzz; and get sorting by xxx or yyy or zzz or any other  yyy/zzz/xxx..??</p>