无法将信息从arraylist拉到jpanel

时间:2018-02-08 12:41:25

标签: jpanel

我无法编译我的代码以显示从ArrayList到Jpanel的一些自定义信息。似乎问题是在109行,我检查了多个页面,我仍然无法得出答案。 请帮忙!

主要情况是我从数据库中获取信息并将其存储到ArrayList>然后我继续分解该数组以从数据库中获取值并将这些值添加到ArrayList中以更清楚地显示信息

package FP.Graphics;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

import FP.Domain.ListamientoDeudas;
import FP.Domain.MySQL;

import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
import java.awt.ScrollPane;

public class EstadoCuotas extends JFrame {

    private JPanel contentPane;
    private static String Identificador;
    public String Item1;//contenedor del nombre a listar
    public String Item2; //contenedor del mes a listar
    public String Item3;//contenedor del monto a listar
    ArrayList<ListamientoDeudas> source;//contenedor de instancias de la clase "Listamiento"
    private JTable table1;



    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    EstadoCuotas frame = new EstadoCuotas(Identificador);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }



    /**
     * Create the frame.

     */
    public EstadoCuotas(String identificadorFromMainWindow) {
        getContentPane().setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 800, 600);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        Identificador=identificadorFromMainWindow;

        JButton btnVolver = new JButton("Volver");
        btnVolver.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                MenuPrincipal menu =new MenuPrincipal(Identificador);
                menu.setVisible(true);
                setVisible(false);
            }
        });
        btnVolver.setBounds(348, 527, 89, 23);
        contentPane.add(btnVolver);

        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setBounds(69, 32, 672, 452);
        contentPane.add(scrollPane);







        ArrayList<ArrayList<String>> aux = new ArrayList<ArrayList<String>>();//Lista de listas de string auxiliar para obtener las deudas del socio o socios
        if (Identificador == "1" || Identificador== "2")//Si el nivel de autoridad es "ADMIN" o "GESTOR" se va a poder ver toda la informacion
        {
            aux = MySQL.GetAllDebtDataPart1();//Igualamos la lista auxiliar a la lista que retorna la funcion "GetAllDebtData" (la cual trae todas las deudas activas en el sistema)      
        }
        else //De lo contrario se mostrara la informacion del socio que consulta
        {
            aux = MySQL.GetSingleDebtDataPart1(Identificador);//Igualamos la lista auxiliar a la lista que retorna la funcion "GetAllDebtData" (la cual trae todas las deudas activas en el sistema para el socio afectado)
        }
        source = new ArrayList<ListamientoDeudas>();
        for(int i = 0 ; i < aux.size() ; i++) 
        {
            ArrayList<String> currentList = aux.get(i);

            for (int j = 0; j < currentList.size(); j++)
            {
              ListamientoDeudas deuda=new ListamientoDeudas(currentList.get(0).toString(),currentList.get(1).toString(),currentList.get(2).toString());
              source.add(deuda);
            }
        }  
        String[] columnNames = {"Nombre","Monto","Mes"};
        Object[] data = source.toArray();
        table1 = new JTable(columnNames,data);
        table1.setBounds(69, 32, 672, 452);
        contentPane.add(table1);

    }
}

在这里,它是ListamientoDeudas类

package FP.Domain;

public class ListamientoDeudas {
    public ListamientoDeudas(String item1, String item2, String item3)//Constructor que recibe 3 parametros
    {
        this.name = item1;//igualacion de variable "item1" a la variable "name"
        this.month = item2;//igualacion de variable "item2" a la variable "month"
        this.amount = item3;//igualacion de variable "item3" a la variable "amount"
    }

    public String name ;//variable String
    public String month ;//variable String
    public String amount ;//variable String
}

****** ******* UPDATE 这个更新是为了展示MySQL.GetAllDebtDataPart1()和MySQL.GetSingleDebtDataPart1(Identificador)的功能是如何工作的,这些基本上会在DB有债务时从数据库中获取值。函数本身返回一个ArrayList&gt;,从@Abra的答案读取的一种可能性是返回一个类型object [] []的变量,有没有办法实现尝试在MySQL上实现相同的查询功能

public static ArrayList<ArrayList<String>> GetAllDebtDataPart1()
{
    ArrayList<ArrayList<String>> aux = new ArrayList<ArrayList<String>>();
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        connect = DriverManager.getConnection(ConnectionString);
        statement = connect.createStatement();
        resultSet = statement.executeQuery("Select * FROM ProyectoJuan.Transacciones where Pago='No'");
        while (resultSet.next())
        {
            ArrayList<String> aux2 = new ArrayList<String>();
            aux2 = GetAllDebtDataPart2(resultSet.getString("ReferenciaSocio"), resultSet.getString("Mes"), resultSet.getString("Monto"));
            aux.add(aux2);
        }
    }
    catch (Exception e)
    {

    }


    Close();
    return aux;
}

public static ArrayList<String> GetAllDebtDataPart2(String identificador, String item1, String item2)
{
    ArrayList<String> container = new ArrayList<String>();
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        connect2 = DriverManager.getConnection(ConnectionString);
        statement2 = connect2.createStatement();
        resultSet2 = statement2.executeQuery("Select * From ProyectoJuan.Socios Where Id=" + identificador);

        while (resultSet.next())
        {
            container.add(resultSet.getString("Nombre"));
            container.add(item1);
            container.add(item2);
        }
    }
    catch (Exception e)
    {

    }


    return container;
}
public static ArrayList<ArrayList<String>> GetSingleDebtDataPart1(String identificador)
{
    ArrayList<ArrayList<String>> aux = new ArrayList<ArrayList<String>>();
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        connect = DriverManager.getConnection(ConnectionString);
        statement = connect.createStatement();
        resultSet = statement.executeQuery("Select * FROM ProyectoJuan.Transacciones where Pago='No'");
        while (resultSet.next())
        {
            ArrayList<String> aux2 = new ArrayList<String>();
            aux2 = GetSingleDebtDataPart2(identificador, resultSet.getString("ReferenciaSocio"), resultSet.getString("Mes"), resultSet.getString("Monto"));
            if (aux2.size() != 0)
            {
                aux.add(aux2);
            }
        }
    }
    catch (Exception e)
    {

    }


    Close();
    return aux;
}

public static ArrayList<String> GetSingleDebtDataPart2(String cedula, String idSocio, String mes, String monto)
{
    ArrayList<String> container = new ArrayList<String>();
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        connect2 = DriverManager.getConnection(ConnectionString);
        statement2 = connect2.createStatement();
        resultSet = statement.executeQuery("Select * FROM ProyectoJuan.Socios where Cedula=" + cedula);
        while (resultSet.next())
        {
            if (idSocio == resultSet.getString("Id"))
            {
                container.add(resultSet.getString("Nombre"));
                container.add(mes);
                container.add(monto);
            }
        }
    }
    catch(Exception e)
    {

    }


    return container;
}

1 个答案:

答案 0 :(得分:0)

在类EstadoCuotas的构造函数中,更改变量source的类型并填充如下:

int rows = aux.size();
int columns = 0;
if (rows > 0) {
    ArrayList<String> primero = aux.get(0);
    if (primero != null) {
        columns = primero.size();
    }
}
Object[][] source = new Object[rows][columns];
for (int row = 0; row < rows; row++) {
    ArrayList<String> aRow = aux.get(row);
    for (int col = 0; col < columns; col++) {
        source[row][col] = aRow.get(col);
    }
}

然后您可以按如下方式创建table1

table1 = new JTable(source, columnNames);