Super_Fly
Super_Fly

Reputation: 115

JList remove a list using a object

I am trying to make a jFrame that will remove a list the user selects. However, I am having some trouble trying to get it to work.

Here is the gui code.

public class Books extends JFrame
{
   private JList bookList;           
   private JList selectedBookList;   
   private JButton addButton;   
   private JButton removeButton; 
   private JButton addUpButton;
   private JPanel bookPanel;        
   private JPanel selectedBookPanel; 
   private JPanel buttonPanel;        

   private String[] books = { "I Did It Your Way", 
                              "The History of Scotland"};

  public Books()
  {
      super("Books");

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      setLayout(new BorderLayout());

      // Build the panels.
      buildBookPanel();
      buildSelectedBookPanel();
      buildButtonPanel();

      // Add the panels to the content pane.
      add(bookPanel, BorderLayout.WEST);
      add(selectedBookPanel, BorderLayout.EAST);
      add(buttonPanel, BorderLayout.CENTER);

      // Pack and display the window.
      pack();
      setVisible(true);
   }

   private void buildBookPanel()
   {
       bookPanel = new JPanel();
       bookList = new JList(books);

       bookList.setSelectionMode(
       ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

       bookList.setVisibleRowCount(7);

       // Add the list to a scroll pane.
      JScrollPane monthListScrollPane = new JScrollPane(bookList);

      // Add the scroll pane to the panel.
      bookPanel.add(monthListScrollPane);
   }

   private void buildSelectedBookPanel()
   {
       selectedBookPanel = new JPanel();
       selectedBookList = new JList();
       selectedBookList.setVisibleRowCount(7);

       JScrollPane selectedMonthScrollPane =
                 new JScrollPane(selectedBookList);

       selectedBookPanel.add(selectedMonthScrollPane);
   }

   private void buildButtonPanel()
   {
      buttonPanel = new JPanel();
      addButton = new JButton("Get Selections");
      removeButton=new JButton("Remove Selections");
      addUpButton=new JButton("Check Out");
      addButton.addActionListener(new ButtonListener());
      removeButton.addActionListener(new removeButton());
      addUpButton.addActionListener(new ButtonListener());

      buttonPanel.add(addButton);
      buttonPanel.add(removeButton);
      buttonPanel.add(addUpButton);
   }

Here is the action ActionListener for the remove button. It is supposed to remove the user entered from the book list once the user selects it. How does a selection get removed?

   private class removeButton implements ActionListener
   {
        public void actionPerformed(ActionEvent e)
        {
            Object[] selections = bookList.getSelectedValues();
        }
   }

Here is what I have.

enter image description here

Upvotes: 0

Views: 65

Answers (1)

Umer Farooq
Umer Farooq

Reputation: 782

Use DefaultListModel instead of array for book items

In your code you have used array for books

private String[] books = { "I Did It Your Way","The History of Scotland"};

Replace the above statement with

private DefaultListModel<String> books = new DefaultListModel<>();
private DefaultListModel<String> selectedBooks = new DefaultListModel<>();

and inside buildBookPanel() method, add book item like this

   books.addElement("I Did It Your Way");
   books.addElement("The History of Scotland");
   books.addElement("Another book name");

Your buildBookPanel() method should look like this

private void buildBookPanel(){

       bookPanel = new JPanel();

       books.addElement("I Did It Your Way");
       books.addElement("The History of Scotland");
       books.addElement("Another book name");

       bookList = new JList(books);

       bookList.setSelectionMode(
       ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

       bookList.setVisibleRowCount(7);

       // Add the list to a scroll pane.
      JScrollPane monthListScrollPane = new JScrollPane(bookList);

      // Add the scroll pane to the panel.
      bookPanel.add(monthListScrollPane);
   }

In method buildSelectedBookPanel() change selectedBookList = new JList(); to selectedBookList = new JList(selectedBooks);

private void buildSelectedBookPanel(){

       selectedBookPanel = new JPanel();
       selectedBookList = new JList(selectedBooks);
       selectedBookList.setVisibleRowCount(7);

       JScrollPane selectedMonthScrollPane = new JScrollPane(selectedBookList);

       selectedBookPanel.add(selectedMonthScrollPane);
   }

Add listener to addButton in buildButtonPanel() method

private void buildButtonPanel()
   {
      buttonPanel = new JPanel();
      addButton = new JButton("Get Selections");
      removeButton=new JButton("Remove Selections");
      addUpButton=new JButton("Check Out");

      addButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0)
        {

            for(Object book : bookList.getSelectedValues())
            {
                selectedBooks.addElement(book.toString());
                books.removeElement(book);
            }
        }
    });

      removeButton.addActionListener(new removeButton());
      addUpButton.addActionListener(new ButtonListener());

      buttonPanel.add(addButton);
      buttonPanel.add(removeButton);
      buttonPanel.add(addUpButton);
   }

Finally, the ActionListener for removeButton

private class removeButton implements ActionListener
   {
       @Override
        public void actionPerformed(ActionEvent e)
        {

           for(Object book : selectedBookList.getSelectedValues())
           {
                books.addElement(book.toString());
                selectedBooks.removeElement(book);
           }



        }

   }

Upvotes: 1

Related Questions