【问题标题】:Java Jframe issue updating Jlist replace entire list using arrayList of objectJava Jframe 问题更新 Jlist 使用对象的 arrayList 替换整个列表
【发布时间】:2021-12-11 02:53:16
【问题描述】:

这是我第一次在这里寻求帮助,对不起,如果我做错了什么,请告诉我。我是母语为西班牙语的人,很抱歉有些文字是西班牙语。我刚开始使用 java,所以如果您在我的代码上看到问题(除了我的要求),请告诉我,谢谢。

解释代码: 我有一个表单,它通过“JtextFields”获取四个字符串,并在您按下按钮后(通过 btAgregarRegistro 侦听器)将它们放在“Cliente”类中我的对象“cliente1”的属性上。然后我将对象添加到 arrayList

//Arraylist declaration
private List<Cliente> arregloClientes = new ArrayList<>();

//Add the object to the ArrayList
                arregloClientes.add(cliente1);

然后我更新了 Jlist“listClientes”(在函数 actualizarLista() 上),这样您就可以在添加记录后在表单上看到它。 该更新调用类 (Cliente) getDatosClientes 的方法,该方法将所有属性带到 DefaultListModel,然后将其显示在 Jlist 上 我的问题: 当我添加一条新记录时,我添加的最后一条记录,替换整个列表等等。所以我的方法“getDatosClientes()”的第一个打印是好的,但是第二个,替换列表中的第一个。我会说问题出在更新上,但是我尝试了很多东西,但似乎没有任何效果。我希望我能正确地解释自己。非常感谢!

所有代码如下:

Main.java(主)

package nickoos;

public class Main
{
    public static void main(String[] args)
    {
        Formulario1();

    }

    public static void Formulario1()
    {
        Formulario frame1 = new Formulario(); //instanciar
        frame1.setContentPane(new Formulario().getPanel()); //mostrar el panel
        frame1.show();
        frame1.setSize(800,600);
        frame1.setLocation(600,230);
    }

}

Cliente.java(类)

package nickoos;

public class Cliente
{
    private String nombre;
    private String apellido;
    private String email;
    private String telefono;

    public Cliente(String nombre, String apellido, String email, String telefono)
    {
        this.nombre = nombre;
        this.apellido = apellido;
        this.email = email;
        this.telefono = telefono;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getApellido() {
        return apellido;
    }

    public void setApellido(String apellido) {
        this.apellido = apellido;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getTelefono() {
        return telefono;
    }

    public void setTelefono(String telefono) {
        this.telefono = telefono;
    }

    public String getDatosClientes()
    {
        return "Nombre: " + getNombre() + " Apellido: " + getApellido() + " Email: " + getEmail() + " Teléfono: " + getTelefono();
    }
}

Formulario.java(表单)

package nickoos;

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

public class Formulario extends JFrame {
    private JButton btAgregarRegistro;
    private JPanel panel;
    private JLabel lbTitulo;
    private JLabel lbNombre;
    private JTextField txNombre;
    private JLabel lbApellido;
    private JLabel lbInstrucciones;
    private JLabel lbSimetria;
    private JLabel lbEmail;
    private JLabel lbTelefono;
    private JTextField txApellido;
    private JTextField txEmail;
    private JTextField txTelefono;
    private JList listClientes;
    private JLabel lbSubtituloLista;
    private JButton btEliminarRegistro;
    private JButton btModificarRegistro;
    private List<Cliente> arregloClientes = new ArrayList<>();

    //Instance the class
    Cliente cliente1 = new Cliente("","","","");


    public JPanel getPanel() {
        return panel;
    }


    public Formulario()
    {
        //Listener to add a record
        btAgregarRegistro.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {

                //Set the attributes to the object 
                cliente1.setNombre(txNombre.getText());
                cliente1.setApellido(txApellido.getText());
                cliente1.setEmail(txEmail.getText());
                cliente1.setTelefono(txTelefono.getText());

                //Add the object to the ArrayList
                arregloClientes.add(cliente1);

                //Update the list
                actualizarLista();

                //Clear textFields after adding the record
                blanquearCampos();
            }
        });

        //Listener "delete"
        btEliminarRegistro.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                int indice = listClientes.getSelectedIndex();
                arregloClientes.remove(indice);
                actualizarLista();

            }
        });
    }

    //Clear textFields
    private void blanquearCampos()
    {
        txNombre.setText("");
        txApellido.setText("");
        txEmail.setText("");
        txTelefono.setText("");
    }

    //Update list
    private void actualizarLista()
    {
        DefaultListModel datos = new DefaultListModel();

        for (int i = 0; i < arregloClientes.size(); i++)
        {
            Cliente index = arregloClientes.get(i);
            datos.addElement(index.getDatosClientes());
        }
        listClientes.setModel(datos);
    }

}

【问题讨论】:

    标签: java swing arraylist jlist defaultlistmodel


    【解决方案1】:

    移动这条线

    //Instance the class
    Cliente cliente1 = new Cliente("","","","");
    

    进入这个方法

    btAgregarRegistro.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent e)
                {
    
                //move it HERE
    
                    //Set the attributes to the object 
                    cliente1.setNombre(txNombre.getText());
                    cliente1.setApellido(txApellido.getText());
                    cliente1.setEmail(txEmail.getText());
                    cliente1.setTelefono(txTelefono.getText());
    
                    //Add the object to the ArrayList
                    arregloClientes.add(cliente1);
    
                    //Update the list
                    actualizarLista();
    
                    //Clear textFields after adding the record
                    blanquearCampos();
                }
            });
    

    正在发生的事情是您在内存中引用同一个对象并只是更改其值。您要做的是每次要添加客户端时创建一个新对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-13
      • 2013-08-06
      • 2012-04-09
      • 2011-07-20
      • 2012-05-06
      • 1970-01-01
      • 2011-10-12
      • 2011-02-07
      相关资源
      最近更新 更多