简体   繁体   中英

Update a JList with new inputs

Basically, my project is to create a simple library system where any given user can have up to three books out at a time. I'm doing it with BreezySwing (because I'm required to) and using a JList to list the currently taken out books.

To return a book, the user clicks on the book to return in the list, and clicks the "Return" button. However, I can't figure out how to get the JList working properly.

How to add new information to the list?

Here is the code I have so far:

import java.util.*;
import javax.swing.*;
import BreezySwing.*;

public class ClientGUI extends GBFrame {
    private JButton returnBook, takeOutBook;
    private JLabel title, author;
    private JTextField titleInput, authorInput;
    private int bookCount = 0;
    DefaultListModel books = new DefaultListModel();
    private JList booksOut = new JList(books);

    public ClientGUI(){
        title = addLabel("Title: ",1,1,1,1);
        author = addLabel("Author: ",2,1,1,1);
        titleInput = addTextField("",1,2,2,1);
        authorInput = addTextField("",2,2,2,1);
        returnBook = addButton("Return",3,1,1,1);
        takeOutBook = addButton("Take Out",3,2,1,1);
        booksOut = addList(4,1,2,1);
        returnBook.setEnabled(false);
        books.addElement("Book - Author");
    }

    public void buttonClicked(JButton buttonObj){
        if(buttonObj == takeOutBook){
            if(bookCount < 3){
                String titleAdd = titleInput.getText();
                String authorAdd = authorInput.getText();
                books.addElement(titleAdd + "By: " + authorAdd);
                bookCount++;

                if(bookCount == 0){
                    takeOutBook.setEnabled(true);
                    returnBook.setEnabled(false);
                } else if(bookCount == 3){
                    takeOutBook.setEnabled(false);
                    returnBook.setEnabled(true);
                } else {
                    takeOutBook.setEnabled(true);
                    returnBook.setEnabled(true);
                }
            } else if(bookCount == 3){
                JOptionPane.showMessageDialog(null, "Please return a book first", "Invalid Choice", JOptionPane.INFORMATION_MESSAGE);
            }
        } else if(buttonObj == returnBook){
            //int n = (Integer) books.getSelectedValue();
            //books.remove(n);
            bookCount--;
            if(bookCount == 0){
                takeOutBook.setEnabled(true);
                returnBook.setEnabled(false);
            } else if(bookCount == 3){
                takeOutBook.setEnabled(false);
                returnBook.setEnabled(true);
            } else {
                takeOutBook.setEnabled(true);
                returnBook.setEnabled(true);
            }
        }
    }

    public static void main(String args[]){
        ClientGUI GUI = new ClientGUI();
        GUI.setSize(300,275);
        GUI.setTitle("Library");
        GUI.setVisible(true);
     }
}

Basically, what I need to do is get the JList working properly. The rest I can finish. Thanks.

Try DefaultListModel.addElement

It will fire update events for the list to respond to

UPDATE

Without knowing how BreezySwing works, I'd be focusing on this line...

booksOut = addList(4, 1, 2, 1);

Given what I believe the other lines in the code are doing, this would appear to "create" a new Jlist and return it (applying the layout).

This means that the model been used by booksOut is no longer the books model.

Try adding

booksOut.setModel(books);

After the addList line and see if that helps

Here is a 'null result' that you might work with until it breaks..

扩充清单

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

public class ExpandableList {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                final JPanel gui = new JPanel(new BorderLayout());

                String[] listValues = {
                        "Click", "To", "Add", "New", "Values"
                };
                final DefaultListModel model = new DefaultListModel();
                for (String s : listValues) {
                    model.addElement(s);
                }
                JList list = new JList(model);
                MouseListener addListener = new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent me) {
                        String s = JOptionPane.showInputDialog(
                                gui, "New text..");
                        if (s!=null) {
                            model.addElement(s);
                        }
                    }
                };
                list.addMouseListener(addListener);
                gui.add(new JScrollPane(list));

                JOptionPane.showMessageDialog(null, gui);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM