Java Programming Tutorial – 73 – Moving List Items Program

///Java Programming Tutorial – 73 – Moving List Items Program

Java Programming Tutorial – 73 – Moving List Items Program

FavoriteLoadingAdd to favorites

Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –

source

Java468x60
By |2019-12-07T00:22:12+00:00December 7th, 2019|Java Video Tutorials|27 Comments

27 Comments

  1. Hania Khan December 7, 2019 at 12:22 am - Reply

    Why do you need to add setSelectionMode to rightList, as we arent selecting anything from the second list?

  2. Caligula December 7, 2019 at 12:22 am - Reply

    rightlist.setListData(leftlist.getSelectedValuesList().toArray(new String[1]));
    0 can sometimes cause problems

  3. Rohit Sarkar December 7, 2019 at 12:22 am - Reply

    the correct code is
    import java.awt.*;

    import java.awt.event.*;

    import javax.swing.*;

    import javax.swing.event.*;

    public class tie extends JFrame {

    private JList leftside;

    private JList rightside;

    private JButton move;

    private static String[] naruto={"rohit","sasake","sakura","garra","lee"};

    @SuppressWarnings({ "rawtypes", "unchecked" })

    public tie()

    {

    super ("the naruto bar");

    setLayout(new FlowLayout());

    leftside=new JList(naruto);

    leftside.setVisibleRowCount(4);

    leftside.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );

    add( new JScrollPane(leftside));

    move=new JButton("move–>");

    move.addActionListener(

    new java.awt.event.ActionListener(){

    public void actionPerformed(ActionEvent e) {

    rightside.setListData(leftside.getSelectedValuesList().toArray());

    }

    }

    );

    add(move);

    rightside=new JList();

    rightside.setVisibleRowCount(4);

    rightside.setFixedCellHeight(10);

    rightside.setFixedCellWidth(110);

    rightside.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );

    add( new JScrollPane(rightside));

    }

    }

  4. intbild December 7, 2019 at 12:22 am - Reply

    the fact that you don;t say where all those methods come from.. if there are built in? or they come from certain import? This fact that you just write things down without saying where are they defined and stuff.. it just makes the tutorial super confusing and hard

  5. Yasser Akbbach December 7, 2019 at 12:22 am - Reply

    Fresh news: 22/4/2018
    replace: rightList.setListData(leftList.getSelectedValues());
    by: rightList.setListData(leftList.getSelectedValuesList().toArray(new String[0]));
    Thanks to eclipse 😉

  6. Kenneth Milota December 7, 2019 at 12:22 am - Reply

    FAQ: How do you delete the items from the list and add them to the other list after moving them?

    The JList isn't capable of adding and deleting items after it's initialized. In order to do this, you need to use the DefaultListModel in addition. You create a DefaultListModel and reference your JList, and then you can add or delete items from the list.

  7. Modern Warrior December 7, 2019 at 12:22 am - Reply

    I reaaally dont like to see any warnings on the final code.

    1. To fix raw type warnings write JList<String> instead of JList when declaring/initializing lists.
    2. To fix the warning about the class that "extends JFrame", just do the first suggestion eclipse offers.

    These two used to be enough until this program.
    If you used the foregoing fixes in this program, then there should appear a type error in the *setListData*.
    Error: The method expects a String array, but toArray() returns an Object array.
    Fix: Simply put new String[0] inside toArray().
    toArray( new String[0] ) returns a String array and error disappears.

  8. Muhammad Taymoor December 7, 2019 at 12:22 am - Reply

    Why always got error in "new ActionListener"
    i just paste my code below, anyone help please.

    movebutton = new JButton("Move—>");
    movebutton.addActionListener(
    new ActionListener() {
    public void ActionPerformed(ActionEvent event) {
    rightlist.setListData(leftlist.getSelectedValues());
    }
    }
    );

  9. iViralGeeks December 7, 2019 at 12:22 am - Reply

    If your code is not working.

    Find:
    rightList.setListData(leftList.getSelectedValues());
    Replace With:
    rightList.setListData(leftList.getSelectedValuesList().toArray());

    Hope your problem is solved 🙂

  10. jayshil bhagat December 7, 2019 at 12:22 am - Reply

    anyone who is getting error put this code instead of what was shown in video

    movebutton.addActionListener(
    new ActionListener(){
    public void actionPerformed(ActionEvent e){
    List<String> values = leftlist.getSelectedValuesList();
    rightlist.setListData(values.toArray(new String[values.size()]));
    }
    }

    );
    dont forget to add java.util.List class(important)

  11. Danny James December 7, 2019 at 12:22 am - Reply

    OK So Here Is The Code Which Allows You To Move An Item From The Left List to The Right List But Also Remove The Selected Item From The Left List, It Works The Same When Removing; I'm Only Learning Java Myself And Thought I'd Try And Push My Limits Did A Bit Of Research And Got The Below Code. If Anybody Can See Anything Wrong Or Would Like To Tell Me An Easier Way Please Let Me Know.
     
    ======== 

    import java.awt.*;
    import java.awt.event.*;
    import java.util.List;
    import javax.swing.*;
    import javax.swing.event.*;

    public class Gui extends JFrame{

    private JList lstAvailableNames;
    private JList lstAcceptedNames;
    private JButton btnAdd;
    private JButton btnRemove;

    private static String[] availableNames = {"Name1","Name2","Name3","Name4"}; // Create Array Of Names.

    DefaultListModel model;
    DefaultListModel model2;

    public Gui(){

    super("The Title"); // Super Allows Access To Super Class. – Create Window Title.
    setLayout(new FlowLayout());

    // Create lstAvailableNames JList Object.
        model = new DefaultListModel();    
    lstAvailableNames = new JList(model); // JList Params: Array Of Values
    lstAvailableNames.setVisibleRowCount(3);
    lstAvailableNames.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    add(new JScrollPane(lstAvailableNames));

    // Add Items From Array Into JList
    for(int i = 0; i < availableNames.length; i++)
    model.addElement(availableNames[i]);

    btnAdd = new JButton("Add >>");
    add(btnAdd);
    btnRemove = new JButton("<< Remove");
    add(btnRemove);

        model2 = new DefaultListModel();  
    lstAcceptedNames = new JList(model2); // JList Params: Array Of Values
    lstAcceptedNames.setVisibleRowCount(3);
    lstAcceptedNames.setFixedCellWidth(120);
    lstAcceptedNames.setFixedCellHeight(20);
    lstAcceptedNames.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    add(new JScrollPane(lstAcceptedNames));

    btnAdd.addActionListener(
    new ActionListener(){
    public void actionPerformed(ActionEvent event){
    //lstAcceptedNames.setListData(lstAvailableNames.getSelectedValuesList().toArray());

    // Remove Selected Elements From Left List And Add To Right List.
    for (Object sel : lstAvailableNames.getSelectedValuesList().toArray()) {
            if (model2.indexOf(sel) == -1) {
                model2.addElement(sel);
                model.remove(model.indexOf(sel));
            }
        }
    }
    }
    );

    btnRemove.addActionListener(
    new ActionListener(){
    public void actionPerformed(ActionEvent event){

    // Remove Selected Elements From Right List And Add Back To Left List.
    for (Object sel : lstAcceptedNames.getSelectedValuesList().toArray()) {
            if (model.indexOf(sel) == -1) {
                model.addElement(sel);
                model2.remove(model2.indexOf(sel));
            }
        }
    }
    }
    );

    }

    }

  12. airon papogs December 7, 2019 at 12:22 am - Reply

    um @thenewboston do you know how to make akarnaugh map please i really need it plz.

  13. sathursan ushakaran December 7, 2019 at 12:22 am - Reply

    if we wanted to use the data stored in right list how would we do that, also where is it stored

  14. Arnav Narula December 7, 2019 at 12:22 am - Reply

    If the horizontal scroller is annoying in the rightlist, I found a solution to remove it:
    Instead of using:  add (new JscrollPane (rightlist));
    Use: JScrollPane pane2 = new JscrollPane (rightlist);
             pane2.setHorizontalScrollBarPolicy (JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                 add (pane2);

  15. vljzlj December 7, 2019 at 12:22 am - Reply

    My code does not work. The line rightbox.setListData(leftbox.getSelectedValues()); is not appropriately set up, in my code there is a straight line passing through the getSelectedValues()); Can anyone pls help me?

  16. X _ December 7, 2019 at 12:22 am - Reply

    getSelectedValues() is deprecated, "As of JDK 1.7, replaced by getSelectedValuesList()", but this method only returns a list, which can not be transferred to object[]. Then you can use getSelectedValuesList().toArray(). but actually even if it is deprecated, you can still use it in your program. it works, i tried.

  17. TheOne AndOnly December 7, 2019 at 12:22 am - Reply

    System.out.println("Bucky for president");

  18. Roberto Perez December 7, 2019 at 12:22 am - Reply

    rightlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

    this one is not necesary, in the program you made, you dont have to choose anything from the rightlist :S

  19. Amit Sarusi December 7, 2019 at 12:22 am - Reply

    Someone can show me how to make the sentence delete after the moving??

  20. Lethe Atlas December 7, 2019 at 12:22 am - Reply

    6/6/2015..first time to see one episode's view count drops below 100k..

  21. freekupo December 7, 2019 at 12:22 am - Reply

    Since the rightlist was added with the code in the constructor and not in the method for the button click, does this mean that java is constantly checking the rightlist array and updates the display?

  22. ryan kross December 7, 2019 at 12:22 am - Reply

    Almost finished Woooo

  23. Epicnerd73 December 7, 2019 at 12:22 am - Reply

    Thanks a lot bucky, this was awesome! (still think you should have added gammon :P)

  24. TheAdio89 December 7, 2019 at 12:22 am - Reply

    everzone on 1.7 with probles with the getselectedvalues… i just added this small line in the line bevore the Actionperformed 
    @SuppressWarnings("deprecation")

  25. MadApples December 7, 2019 at 12:22 am - Reply

    I made it so it Deletes from the left array, and adds it to the right array!

    thats a great practice 😀

  26. Crush3333 December 7, 2019 at 12:22 am - Reply

    This solution does not seem to fix mine. What would one type to search a solution to this problem?

  27. TheRenosree December 7, 2019 at 12:22 am - Reply

    and the way he teaches it, is the worst. I comment again after 6 months after many Java Programs and I am telling you this:

    Let's both make a Java program and deliver the code to someone higher. Your program might be better but if u follow Bucky's rules the guy that will see your program will not understand what u have done. Most of your programs in near future will be done in a group, if the others cannot understand your code you will be problem to them. In my case, they can fix the mistakes.

Leave A Comment

*