Fill in a jCombobox using Join and Hibernate

Asked

Viewed 87 times

1

I’m doing a job at college where I’m supposed to create a scheduling system for car dealerships. The technologies used are Java, Swing, and Hibernate.

I divided the project in layers to facilitate, created the classes customer, seller and commitment to schedule the activities of sellers with customers. For each of these classes are in the Domain layer, the connection methods to save, search exclude are in the dao layer, and there is a layer called control that calls the methods of an interface of each DAO class.

I made the client screen, I made the Sellers screen, but on the Commitment screen has a JComboBox that was to show the sellers and another to show the customers , but only appear their references and not the names.

How do I get the names of the sellers and customers?

Project in the Bitbucket

Follow code of control commitment:

package control;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;
import model.dao.CompromissoDao;
import model.dao.CompromissoDaoImp;
import model.domain.Cliente;
import model.domain.Compromisso;
import model.domain.Vendedor;
import model.service.ServiceLocator;
import org.jdesktop.observablecollections.ObservableCollections;

/**
 *
 * @author joao.rolim
 */
public final class CompromissoControl {
    private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
    private Compromisso compromissoDigitado;
    private Compromisso compromissoSelecionado;
    private List<Vendedor> vendedorTabela;
    private List<Cliente> clienteTable;
    private List<Compromisso> compromissoTabela;
    private final CompromissoDao compromissoDao;

    public CompromissoControl() {
        compromissoDao = ServiceLocator.getCompromissoDao();
        compromissoTabela = ObservableCollections.observableList(new ArrayList<Compromisso>());

        clienteTable = ObservableCollections.observableList(
                new ArrayList<Cliente>());
        clienteTable.addAll(compromissoDao.buscarClientes());

        vendedorTabela = ObservableCollections.observableList(new ArrayList<Vendedor>());
        vendedorTabela.addAll(compromissoDao.buscarVendedores());        
        novo();
        pesquisar();
    }

    public Compromisso getCompromissoDigitado() {
        return compromissoDigitado;
    }

    public void setCompromissoDigitado(Compromisso compromissoDigitado) {
        Compromisso oldcompromissoDigitado = this.compromissoDigitado;
        this.compromissoDigitado = compromissoDigitado;
        propertyChangeSupport.firePropertyChange("compromissoDigitado",oldcompromissoDigitado,compromissoDigitado);
    }

    public Compromisso getCompromissoSelecionado() {
        return compromissoSelecionado;
    }

    public void setCompromissoSelecionado(Compromisso compromissoSelecionado) {
        this.compromissoSelecionado = compromissoSelecionado;
        if (this.compromissoSelecionado != null) {
            setCompromissoDigitado(compromissoSelecionado);
        }

    }

    public List<Compromisso> getCompromissoTabela() {
        return compromissoTabela;
    }

    public void setCompromissoTabela(List<Compromisso> compromissoTabela) {
        this.compromissoTabela = compromissoTabela;
    }


    public void addPropertyChangeListener(PropertyChangeListener property){
       propertyChangeSupport.addPropertyChangeListener(property);
    }
    public void removePropertyChangeListener(PropertyChangeListener property){
        propertyChangeSupport.removePropertyChangeListener(property);
    }

    public void novo() {
        setCompromissoDigitado(new Compromisso());
    }


    public void salvar(){
        compromissoDao.salvarAtualizar(compromissoDigitado);
    }

    public void excluir(){
        compromissoDao.exluir(compromissoSelecionado);
    }

    public void pesquisar() {
        compromissoTabela.clear();
        compromissoTabela.addAll(compromissoDao.pesquisaGeral(compromissoDigitado));
    }

    public List<Vendedor> getVendedorTabela() {
        return vendedorTabela;

    }

    public void setVendedorTabela(List<Vendedor> vendedorTabela) {
        //Vendedor vendedor = this.vendedorTabela.
        this.vendedorTabela = vendedorTabela;
    }


    public List<Cliente> getClienteTable() {
        return this.clienteTable;


    }

    public void setClienteTable(List<Cliente> clienteTable) {
        this.clienteTable = clienteTable;
    }


}

1 answer

1


The simplest way to solve the problem of the JComboBox is overwriting the method toString() of its two Seller and Customer classes, returning whatever you want to be displayed. From what I’ve seen in the codes, you can do so for both classes:

public String toString(){
    return this.nome;
}

In this way, the JCombobox will display the seller’s name and the Customer’s name. Another way is creating your own Combomodel, but then you’d have to create one for each combo.

  • 1

    Boy, that’s all I needed, man, how could I forget toString() thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.