Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hold data within a DefaultListModel in java

I'm trying to make a list, using a JList/DefaultListModel, that holds data within itself.

Here is my problem.

DefaultListModel my_docs = new DefaultListModel();
JList my_jlist = new JList(my_docs);
// From here, I add elements to my_docs..
my_docs.addElement("Document 1");
my_docs.addElement("Document 2");
my_docs.addElement("Document 3");

What could I do to hold the text of each document (averaging around 4,000 bytes) in the element, not assigning to another variable until clicked on?

For example, if I clicked on Document 2 in the list, the document's text would be assigned to a variable. If I clicked on Document 1 in the list, it would do the same thing.

Sorry for asking this with so little information, I just have no idea where to start.

like image 920
Hairr Avatar asked Jul 22 '26 20:07

Hairr


1 Answers

You can create a wrapper for the document. For example, the wrapper can expose data and name. The data can also be loaded on demand if needed. Add instances of the wrapper to the list. Use document name to render each entry of the list. Upon selection of the entry in the list use selected item to work with. Here is a simple example that displays a list of documents. Once a document is selected its content is displayed in the text area.

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

public class TestListModel {
    public TestListModel() {

        DefaultListModel model = new DefaultListModel();
        model.addElement(new DocumentWrapper("Doc 1", "Doc 1 data"));
        model.addElement(new DocumentWrapper("Doc 2", "Doc 2 data"));
        model.addElement(new DocumentWrapper("Doc 3", "Doc 3 data"));

        final JList list = new JList();

        final JTextArea output = new JTextArea(5, 40);
        list.setModel(model);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        list.setCellRenderer(new DocumentWrapperRenderer());

        list.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {
                    @Override
                    public void valueChanged(ListSelectionEvent e) {
                        if (!e.getValueIsAdjusting()) {
                            DocumentWrapper docWrapper = (DocumentWrapper) list
                                    .getSelectedValue();
                            if (docWrapper != null) {
                                output.setText(docWrapper.getData());
                            } else {
                                output.setText("");
                            }
                        }
                    }
                });

        JFrame frame = new JFrame("Document list");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JScrollPane(list));
        frame.add(output, BorderLayout.SOUTH);
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);
    }

    public class DocumentWrapper {
        private String data;
        private String name;

        public DocumentWrapper(String name, String data) {
            this.name = name;
            this.data = data;
        }

        public String getData() {
            return data;
        }

        public String getName() {
            return name;
        }
    }


    public static class DocumentWrapperRenderer extends DefaultListCellRenderer {
        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean hasFocus) {
            if (value instanceof DocumentWrapper) {
                return super.getListCellRendererComponent(
                        list, ((DocumentWrapper) value).getName(), index,
                        isSelected, hasFocus);
            }
            return super.getListCellRendererComponent(list, value, index, 
                    isSelected, hasFocus);
        }
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestListModel();
            }
        });
    }
}
like image 129
tenorsax Avatar answered Jul 25 '26 10:07

tenorsax



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!