简体   繁体   English

Java JList 未显示其元素

[英]Java JList not showing its elements

I've made a GUI using IntelliJ IDEA's form designer, and I have added a JList inside a JScrollPane.我使用 IntelliJ IDEA 的表单设计器制作了一个 GUI,并在 JScrollPane 中添加了一个 JList。 The thing is that no matter when or how I add elements to the JList, it doesn't show them.问题是,无论何时或如何将元素添加到 JList,它都不会显示它们。 I used the debug tool and I can see that the elements are inside the JList, they just aren't rendered.我使用了调试工具,可以看到元素在 JList 中,只是没有渲染。

I'm currently using a DefaultListModel, but I've tried using Vector and arrays without success.我目前正在使用 DefaultListModel,但我尝试使用 Vector 和 arrays 没有成功。 I have also tried using the function updateUI() in the JList, the JScrollPane and the JFrame itself, and the function ensureIndexIsVisible() with the last index of the list in the JList, but nothing.我还尝试在 JList、JScrollPane 和 JFrame 本身中使用 function updateUI(),以及 function 中的最后一个索引,但没有任何索引。

This form is called from another one and I don't think the code for the main one is needed, so I'll only paste here the code for the faulty form:这个表单是从另一个表单调用的,我认为不需要主表单的代码,所以我只将错误表单的代码粘贴到这里:

import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Enviar extends JFrame {
    private JTextField codigoTxt;
    private JButton anadirBtn;
    private JPanel enviar;
    private JLabel errorCodigoLbl;
    private JList<String> companerosLBox;
    private DefaultListModel<String> listaCompas = new DefaultListModel<>();
    private JButton eliminarSelecBtn;
    private JButton eliminarTodoBtn;
    private JTextField xPosTxt;
    private JTextField yPosTxt;
    private JLabel errorClickLbl;
    private JButton clickBtn;
    private JButton atrasBtn;
    private JScrollPane scrollPane;

    public Enviar() {
        setContentPane(enviar);
        setTitle("Remote Clicker - Enviar click");
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        pack();
        errorCodigoLbl.setVisible(false);
        errorClickLbl.setVisible(false);
        setVisible(true);
        listaCompas.addElement("sdd");
        listaCompas.addElement("sd2d");
        listaCompas.addElement("sdd3");
        companerosLBox = new JList<>(listaCompas);
        scrollPane = new JScrollPane(companerosLBox);
        anadirBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                anadirCompa(codigoTxt.getText());
            }
        });
        codigoTxt.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    anadirCompa(codigoTxt.getText());
                }
            }
        });
    }

    private void anadirCompa(String codigo) {
        if (valido(codigo)) {
            codigoTxt.setText("");
            errorCodigoLbl.setVisible(false);
            listaCompas.addElement(codigo);
            companerosLBox.setModel(listaCompas);
        } else {
            errorCodigoLbl.setVisible(true);
        }
    }

    private boolean valido(String codigo) {
        boolean res = true;
        int i = 0;
        while (res && i < codigo.length())
        {
            res = codigo.charAt(i) >= '0' && codigo.charAt(i) <= '9' || codigo.charAt(i) == '-';
            i++;
        }
        return res && codigo.indexOf('-') > 0 && codigo.indexOf('-') < codigo.length()-1;
    }
}

What else can I do or what am I doing wrong?我还能做什么或我做错了什么?

EDIT: I'll also add that if I populate the JList via the form builder itself, the data I add there is shown, but once it's loaded it doesn't change.编辑:我还要补充一点,如果我通过表单构建器本身填充 JList,则会显示我在那里添加的数据,但是一旦加载它就不会改变。

You should call jList.setModel() only once inside the constructor.您应该只在构造函数中调用一次 jList.setModel()。 You are calling it every time you add something to the list.每次向列表中添加内容时,您都会调用它。

Try this:尝试这个:

public class Enviar extends JFrame {
    //...
    private JList<String> companerosLBox = new JList<>();
    private DefaultListModel<String> listaCompas = new DefaultListModel<>();

    public Enviar() {
        //...
        listaCompas.addElement("sdd");
        listaCompas.addElement("sd2d");
        listaCompas.addElement("sdd3");
        companerosLBox.setModel(listaCompas);
    }

    private void anadirCompa(String codigo) {
        if (valido(codigo)) {
            codigoTxt.setText("");
            errorCodigoLbl.setVisible(false);
            listaCompas.addElement(codigo);
        } else {
            errorCodigoLbl.setVisible(true);
        }
    }

}

Ok, the problem was that IntelliJ IDEA's form designer doesn't work the same way that plain Java does.好的,问题是 IntelliJ IDEA 的表单设计器与普通的 Java 的工作方式不同。

The thing is that in this conditions it's not needed to create a new JList, so when I did companerosLBox = new JList<>(listaCompas);问题是在这种情况下不需要创建一个新的 JList,所以当我做companerosLBox = new JList<>(listaCompas); I was unbinding it from the form (I suppose).我将它从表单中解绑(我想)。

So, for the provided code to work, it's just needed to replace因此,要使提供的代码正常工作,只需要替换

companerosLBox = new JList<>(listaCompas);

with

companerosLBox.setModel(listaCompas);

in the constructor (and for correction, deleting that same line from anadirCompa() ).在构造函数中(为了更正,从anadirCompa()中删除同一行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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