Problem filling ComboBox in Java (Eclipse)

1

I had already loaded a similar question but it did not work. The idea is a recipe program. I have a class called "Input", several inputs are registered, and then those inputs (one or more) are loaded into a "Plato" (another class). The supplies the warehouses in an Array, and the Dishes contain Array of Inputs. This is defined in the Rest class.

Now, I was able to create the graphical interface using WindowBuilder to fill generate my supplies and load the Array. but lifting the interface of the dishes the Input Array gives me an error. That is to say. I want to show them in a JcomboBox, and I throw error. It can also be a Jlist, in fact, in the dishes interface, as I generate them I will show them in a Jlist, as I show below.

I would like to know which is the best way, if I show them in a combo, or in a list. and how to solve this error. (It tells me that I must change the type that returns the method returnsputs within the Rest class).

Thank you very much everyone for your time!

public class Insumo {

private static int INCREMENTAR_CODIGO = 0;

private int codigo;
private String descripcion;
private int precio;

public int getPrecio() {
    return precio;
}
public void setPrecio(int precio) {
    this.precio = precio;
}

public int getCodigo() {
    return codigo;
}

public String getDescripcion() {
    return descripcion;
}
public void setDescripcion(String descripcion) {
    this.descripcion = descripcion;
}

public String toString() {
    return codigo +"-"+descripcion;
}

public Insumo(String descripcion, int precio) {
    this.codigo = ++INCREMENTAR_CODIGO;
    this.descripcion = descripcion;
    this.precio = precio;
}

}

package model;

import java.util.ArrayList;

public class Resto {

private static ArrayList<Insumo> insumos = new ArrayList<Insumo>();
private static ArrayList<Plato> platos = new ArrayList<Plato>();
private static ArrayList<Mesa> mesas = new ArrayList<Mesa>();

public void addInsumo(String descripcion, int precio) {
    insumos.add(new Insumo(descripcion, precio));       
}

public int largoArray (){
    return insumos.size();
}

public ArrayList<Insumo> devolverInsumos(){
    return insumos;
}

public void addPlatoComun(String nombre) {
    platos.add(new PlatoComun(nombre));
}

public void addPlatoEspecial(String nombre) {
    platos.add(new PlatoEspecial(nombre));
}

public ArrayList<Plato> devolverPlatos(){
    return platos;
}

public void addMesa(){
    mesas.add(new Mesa());
}

public ArrayList<Mesa> devolverMesas(){
    return mesas;
}

public ArrayList<Mesa> devolverMesasDisponibles(){
    ArrayList<Mesa> disponibles = new ArrayList<Mesa>();

    for(Mesa m: mesas) {
        if(m.isDisponible()){
            disponibles.add(m);
        }
    }

    return disponibles;
}

public void abrirMesa(Mesa m){
    m.cambiarAbierta();
}



public void agregarPlatosAMesa(Mesa m, ArrayList<Plato> p){
    m.agregarPlatos(p);
}

public void agregarInsumosAPlato(Plato p, ArrayList<Insumo> i){
    p.agregarInsumos(i);
}

public void addInsumo(Insumo ins) {
    // TODO Auto-generated method stub

}

}

Here is the error of the JComboBox

package vista;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import model.Insumo;
import model.PlatoEspecial;
import model.Resto;
import net.miginfocom.swing.MigLayout;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JList;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;

public class Interface_Plato extends JDialog {

private final JPanel contentPanel = new JPanel();
private JTextField txt_codigo;
private JTextField txt_desc;
private final ButtonGroup buttonGroup = new ButtonGroup();
private Resto resto;


public Interface_Plato(Resto r) {
    resto = r;

    setTitle("Alta de Platos");
    setBounds(100, 100, 450, 300);
    getContentPane().setLayout(new BorderLayout());
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    contentPanel.setLayout(new MigLayout("", "[grow][grow]", "[][][][][][grow]"));

        JLabel lblCodigo = new JLabel("Codigo:");
        contentPanel.add(lblCodigo, "cell 0 0,alignx trailing");


        txt_codigo = new JTextField();
        contentPanel.add(txt_codigo, "cell 1 0,alignx left");
        txt_codigo.setColumns(10);


        JLabel lblDescripcion = new JLabel("Descripcion:");
        contentPanel.add(lblDescripcion, "cell 0 1,alignx trailing");


        txt_desc = new JTextField();
        contentPanel.add(txt_desc, "cell 1 1,alignx left");
        txt_desc.setColumns(10);

        JLabel lblInsumo = new JLabel("Insumo:");
        contentPanel.add(lblInsumo, "cell 0 2,alignx trailing");


        JComboBox<Insumo> cb_insumos = new JComboBox<Insumo>();
        contentPanel.add(cb_insumos, "cell 1 2,growx"); 
        DefaultComboBoxModel<Insumo> modelito = new 
DefaultComboBoxModel<Insumo>();
        cb_insumos.setModel(modelito);
//  llenar JList con los Insumos
        cb_insumos.addItem(r.devolverInsumos());
        modelito.addElement(r.devolverInsumos());
        for (int i=0;i<r.largoArray(); ) {
            cb_insumos.addItem(r.devolverInsumos(i));
        }

//problema para cargar el jlist con objetos                     

        JButton btnNewButton = new JButton("Agregar Insumo al Plato");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

            }
        });
        contentPanel.add(btnNewButton, "cell 1 3");

        JLabel lblEsPlatoEspecial = new JLabel("Tipo de Plato");
        contentPanel.add(lblEsPlatoEspecial, "cell 0 4");

        JRadioButton rdbtnComn = new JRadioButton("Com\u00FAn");
        buttonGroup.add(rdbtnComn);
        contentPanel.add(rdbtnComn, "flowx,cell 1 4");


        JList<Insumo> list = new JList<Insumo>();

        contentPanel.add(list, "cell 1 4");     

        contentPanel.add(list, "cell 1 5,grow");

        JRadioButton rdbtnEspecial = new JRadioButton("Especial");
        buttonGroup.add(rdbtnEspecial);
        contentPanel.add(rdbtnEspecial, "cell 1 4");

        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
        getContentPane().add(buttonPane, BorderLayout.SOUTH);

        JButton okButton = new JButton("Confirmar");


        JButton btnCancelar = new JButton("Cancelar");
        btnCancelar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                dispose();
            }
            });

            buttonPane.add(btnCancelar);
            okButton.setActionCommand("OK");
            buttonPane.add(okButton);
            getRootPane().setDefaultButton(okButton);

            JButton cancelButton = new JButton("Cancel");
            cancelButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    dispose();
                }
            });
}

}

And so I generate my Inputs

package vista;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import model.Insumo;
import model.Resto;
import net.miginfocom.swing.MigLayout;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollBar;
import javax.swing.JList;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


import java.awt.Font;
import java.util.ArrayList;

public class Interface_Insumo extends JDialog {

private final JPanel contentPanel = new JPanel();
private JTextField txt_Codigo;
private JTextField txt_Nombre;
private Resto resto;
private Insumo insumo;


JList<Insumo> list = new JList<Insumo>();
private JTextField txt_Precio;
DefaultListModel<Insumo> model = new DefaultListModel<Insumo>();

public Interface_Insumo(Resto r) {
    setTitle("Alta de Insumos");
    resto = r;
    setBounds(100, 100, 450, 300);
    getContentPane().setLayout(new BorderLayout());
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    contentPanel.setLayout(new MigLayout("", "[10px][grow][grow]", "[10px][][][][][][][]"));

        JPanel panel = new JPanel();
        contentPanel.add(panel, "cell 0 0,alignx left,aligny top");


        JLabel lblCrearNuevoInsumo = new JLabel("Crear nuevo insumo");
        lblCrearNuevoInsumo.setFont(new Font("Yu Gothic UI Light", Font.BOLD, 18));
        contentPanel.add(lblCrearNuevoInsumo, "cell 1 0");


        JLabel lblCodigo = new JLabel("Codigo:");
        contentPanel.add(lblCodigo, "cell 0 1,alignx trailing");

       //Igual q la linea de abajo pero paso todo en 1 linea

        txt_Codigo = new JTextField();
        txt_Codigo.setEditable(false);
        contentPanel.add(txt_Codigo, "cell 1 1,alignx left");

        JLabel lblDescripcion = new JLabel("Nombre:");
        contentPanel.add(lblDescripcion, "cell 0 2,alignx trailing");

        txt_Nombre = new JTextField();
        contentPanel.add(txt_Nombre, "cell 1 2,alignx left");
        txt_Nombre.setColumns(10);

        JScrollBar scrollBar = new JScrollBar();
        contentPanel.add(scrollBar, "flowx,cell 1 5");

/*/
 * Creo y defino el modelo de la lista de insumos que se hará visible. */

        list.setModel(model);
//          
        contentPanel.add(list, "cell 1 5");

        JLabel lblPrecio = new JLabel("Precio:");
        contentPanel.add(lblPrecio, "cell 0 3,alignx trailing");

        txt_Precio = new JTextField();
        contentPanel.add(txt_Precio, "cell 1 3,alignx left");
        txt_Precio.setColumns(10);

        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
        getContentPane().add(buttonPane, BorderLayout.SOUTH);

// Creo el obtjeto insumo dentro del array

        JButton okButton = new JButton("Confirmar");
            okButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    int precio = Integer.parseInt(txt_Precio.getText());
                    Insumo ins = new Insumo(txt_Nombre.getText(), precio);

// Inserto el Array? o el objeto individual?
//                      ArrayList<Insumo> insumosd = new ArrayList<Insumo>();
//                      insumosd.add(ins);
//                      resto.addInsumo(insumo);                            
                    resto.addInsumo(ins); // <-- Forma Correcta ??
//  validacion                      
                    System.out.printf(txt_Codigo.getText()+" "+ txt_Nombre.getText());

((DefaultListModel<Insumo>)list.getModel()).addElement(ins);
                    System.out.print(resto.devolverInsumos());

                }
            });
            okButton.setActionCommand("OK");
            buttonPane.add(okButton);
            getRootPane().setDefaultButton(okButton);


            JButton cancelButton = new JButton("Cancel");
            cancelButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    dispose();

                }
            });

            cancelButton.setActionCommand("Cancel");
            buttonPane.add(cancelButton);

}

}
    
asked by Lenz 22.02.2018 в 01:00
source

0 answers