简体   繁体   中英

My JList isn't show

i write a program, but when i start him, my JList isn't show. Anyone Can see what is bad? https://pastebin.com/CWFnSjen , it work like this: in JTextField i need to put a Word, then after click "enter" this world must be added to JList

   public static void main(String[] args)
{
    new DrawSwing();
}

public DrawSwing()
{
    SwingUtilities.invokeLater(() -> createGUI());
}

protected void createGUI()
{

    JPanel main = new JPanel();
    main.setLayout(new BorderLayout());
     WlasnyMode<String> model = new WlasnyMode<>();
     MyTextField text = new MyTextField(model);
    MyFrame jf = new MyFrame();
  jf.setTitle("Rysowanie");
  jf.setDefaultCloseOperation(MyFrame.EXIT_ON_CLOSE);  
    jf.setLocation(50,50);         jf.setResizable(true);
    JPanel  uper =new JPanel();
    uper.setLayout(new GridLayout(1,1));
    JPanel textPanel = new JPanel();
    jf.setLayout(new BorderLayout());
    textPanel.setLayout(new BorderLayout());

uper.add(text);


    textPanel.setPreferredSize(new Dimension(500,500));
    JList<String> lista = new JList<>(model);
    JScrollPane scroll = new JScrollPane(lista);
    textPanel.add(scroll);
    lista.setCellRenderer(new MyCellRenderer());
    lista.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    lista.setLayoutOrientation(JList.VERTICAL);
    lista.setVisibleRowCount(30);
    lista.setBounds(100,100 , 300, 300);
   textPanel.add(lista);
    main.add(uper,BorderLayout.NORTH);
    main.add(textPanel,BorderLayout.CENTER);
 jf.setContentPane(main);  
 jf.pack( ); 
 jf.setVisible(true);   
  }
 }

You JList is appearing just fine. If I add lista.setBackground(Color.BLUE); to your code, the JList can clearly be seen.

Part of the problem is the fact that you've wrapped it in another JPanel ( textPanel ) which is generally screwing a bunch of things up. You don't need it and it will help reduce some of the complexity in your code

The "core" problem is with your implementation of ListModel . When the model is changed, you are required to generate an appropriate event. Because the whole structure of the list is been changed, you will need to use fireContentsChanged , for example...

public class WlasnyMode<T> extends AbstractListModel<String> {
    //...
    void addElement(String text) {

        this.text.add(text);
        Collections.sort(this.text);
        fireContentsChanged(this, 0, this.text.size() - 1);

    }

}

Also, KeyListener is a poor choice for dealing with input, especially when ActionListener will provide the same job in a platform independent manner...

public class MyTextField extends JTextField implements ActionListener {

    WlasnyMode model;

    MyTextField(WlasnyMode model) {
        super();
        this.model = model;
        addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        model.addElement(this.getText());
        System.out.println(this.getText());
    }

}

But this also raises a bunch of questions over the validity of extending from JTextField

Runnable example...

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.*;
import javax.swing.event.ListDataListener;

public class DrawSwing {

    public static void main(String[] args) {
        new DrawSwing();
    }

    public DrawSwing() {
        SwingUtilities.invokeLater(() -> createGUI());
    }

    protected void createGUI() {
        // utworzenie okna
        JPanel main = new JPanel();
        main.setLayout(new BorderLayout());
        WlasnyMode<String> model = new WlasnyMode<>();
        MyTextField text = new MyTextField(model);
        JFrame jf = new JFrame();

        // określenie tytułu okna
        jf.setTitle("Rysowanie");

        // obsługa zamknięcia okna JFrame
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // określenie położenia okna
        jf.setLocation(50, 50);

        // uniemoĹĽliwienie zmiany rozmiarĂłw okna
        jf.setResizable(true);

        JPanel uper = new JPanel();
        uper.setLayout(new GridLayout(1, 1));
        JPanel textPanel = new JPanel();
        jf.setLayout(new BorderLayout());
        textPanel.setLayout(new BorderLayout());

        uper.add(text);

        JList<String> lista = new JList<>(model);
        JScrollPane scroll = new JScrollPane(lista);
        lista.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        lista.setLayoutOrientation(JList.VERTICAL);
        lista.setVisibleRowCount(30);
        lista.setBackground(Color.BLUE);

        main.add(uper, BorderLayout.NORTH);
        main.add(scroll, BorderLayout.CENTER);

        //jf.addKeyListener('f');
        // utworzenie obszaru rysowania - pulpitu
        // wymiana domyślnego pulpitu na własny
        jf.setContentPane(main);

        // upakowanie okna
        jf.pack();

        // wyświetlenie okna
        jf.setVisible(true);
    }

    public class WlasnyMode<T> extends AbstractListModel<String> {

        ArrayList<String> text = new ArrayList<>();

        //WlasnyMode()
        //{
        //  super();
        //  }
        @Override
        public String getElementAt(int liczba) {
            // TODO Auto-generated method stub
            return text.get(liczba);
        }

        @Override
        public int getSize() {
            // TODO Auto-generated method stub
            return text.size();
        }

        void addElement(String text) {

            this.text.add(text);
            Collections.sort(this.text);
            fireContentsChanged(this, 0, this.text.size() - 1);

        }

        void remove(int index) {
            text.remove(index);
        }

        void removeRange(int fromIndex, int toIndex) {
            for (int i = fromIndex; i < toIndex; i++) {
                text.remove(i);
            }
        }

        public void addListDataListener(ListDataListener l) {
            super.addListDataListener(l);
        }

    }

    public class MyTextField extends JTextField implements ActionListener {

        WlasnyMode model;

        MyTextField(WlasnyMode model) {
            super();
            this.model = model;
            addActionListener(this);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            model.addElement(this.getText());
            System.out.println(this.getText());
        }

    }
}

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