简体   繁体   中英

DefaultListModel in JList in JScrollPane, can't see the JList

I'm trying to do a generic JList handled with a DefaultListModel inside a JScrollPane. However, I can't see the JList.

Here is the class :

FieldScrollList :

    public class FieldScrollList<T> extends JScrollPane {

        private DefaultListModel<T> listModel;


        public int length () {
            return listModel.size();
        }

        public FieldScrollList () {

            setBorder(new TitledBorder(this.getClass().getSimpleName()));
            setBackground(Color.PINK);

            listModel = new DefaultListModel<>();
            JList<T> jList = new JList<>(listModel);
            add(jList);


            jList.setBorder(new TitledBorder(jList.getClass().getSimpleName()));


        }

        public void clear () {
            listModel.clear();
        }

        public void push(T t) {
            listModel.add(length(),t);
        }

        public <C extends Collection<T>> void pushAll(C coll) {
            coll.forEach(this::push);
        }

        public void pushAll(T[] coll) {
            for (T t : coll) {
                push(t);
            }
        }
    }

And Here is the class using it. In this example, I a FieldScrollList which sould display list items : hi and hello.

public class test {


    public static void main(String[] args) {
        new Thread(() -> {
            //---------------------------------- Content initialization ------------------

            JFrame frame = new JFrame("Test");
            JPanel panel = new JPanel();
            FieldScrollList<String> list = new FieldScrollList<String>();

            //---------------------------------- Strings initialization ------------------


            ArrayList<String> strings = new ArrayList<>();
            strings.add("Hello");
            strings.add("Hi");
            strings.forEach(list::push);

            //---------------------------------- JPanel configuration --------------------

            panel.setLayout(new GridLayout(1,1));
            panel.add(list);

            //---------------------------------- JFrame configuration --------------------

            frame.add(panel);
            frame.setPreferredSize(new Dimension(550,600));
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            frame.setVisible(true);
        }).start();

    }
}

And the result is like that :

GUI创建

The goal of the borders and setbackgrounds are to display the location and area of the contents (visually)

I don't understand why the fields are not displayed

Don't extend JScrollPane . You are not adding any functionality to the scrollpane. All those methods are related to the ListModel and have nothing to do with a JScrollPane .

add(jList);

Don't add components to a scrollpane. The JScrollPane is a compound component that contains JScrollBars and a JViewport . The JList needs to be added to the viewport.

Don't add the JList to the panel. You need to add the JScrollPane to the panel

Typically this is done with basic code like:

JScrollPane scrollPane = new JScrollPane( list );
panel.add( scrollPane );

You are created and manipulating Swing objects not on the EDT. Your Runnable should be invoked by SwingUtilities.invokeLater inside static void main.

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