Peencher JComboBox ao clicar na tabela

4 respostas
mysql
meyer

Boa noite,
Tenho um medoto preencheCamposTabelaPet, quando clicar na tabela ele preenche meu formulario.
Tenho um JComboBox cliente que é de outra tabela.
Esta tudo funcionando o CRUD.
Mas quando tendo preencher comboBox cliente ao clicar na tabela ele não preenche, como faço para quando clicar ele preencher com o cliente que estou selecionando.
Segue abaixo o metodo preencher preencheCamposTabelaPet.

public void preencheCamposTabelaPet(){

try {

int preencher = jtPet.getSelectedRow();
this.jlId_pet         .setText(jtPet.getModel().getValueAt(preencher, 0).toString());
        this.jtfNomePet       .setText(jtPet.getModel().getValueAt(preencher, 1).toString());
        this.jtfRacaPet       .setText(jtPet.getModel().getValueAt(preencher, 2).toString());
        this.jtfCorPet        .setText(jtPet.getModel().getValueAt(preencher, 3).toString());
        this.jtfPeloPet       .setText(jtPet.getModel().getValueAt(preencher, 4).toString());
        this.jtfIdadepet      .setText(jtPet.getModel().getValueAt(preencher, 5).toString());
        this.jtfImagem01Pet   .setText(jtPet.getModel().getValueAt(preencher, 6).toString());
        this.jtfObservcaoPet  .setText(jtPet.getModel().getValueAt(preencher, 7).toString());
        this.jcbCliente.setSelectedItem(jtPet.getModel().getValueAt(preencher, 8).toString());

4 Respostas

M

Para invocar esse método você vai precisar que o usuário clique na tabela certo? Para isso você tem que adicionar um mouseListener (MousePressed) à sua tabela.
Segue o exemplo:

table.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent evt) {
            super.mousePressed(evt);
            if (evt.getClickCount() == 2) {
                int linha = table.rowAtPoint(evt.getPoint());
                int valor = Integer.valueOf(table.getValueAt(linha, coluna).toString());
            }
        }
    });
meyer

Então ja tenho um evento de clique na tabela, ele preenche todos os campos do formulário menos o JComboBox que é de outra tabela.

M

Então adiciona essa condição

meyer

Desculpa mas como posso usar essa condição no meu metodo ?

public void preencheCamposTabelaPet(){

try {

int preencher = jtPet.getSelectedRow();
this.jlId_pet         .setText(jtPet.getModel().getValueAt(preencher, 0).toString());
    this.jtfNomePet       .setText(jtPet.getModel().getValueAt(preencher, 1).toString());
    this.jtfRacaPet       .setText(jtPet.getModel().getValueAt(preencher, 2).toString());
    this.jtfCorPet        .setText(jtPet.getModel().getValueAt(preencher, 3).toString());
    this.jtfPeloPet       .setText(jtPet.getModel().getValueAt(preencher, 4).toString());
    this.jtfIdadepet      .setText(jtPet.getModel().getValueAt(preencher, 5).toString());
    this.jtfImagem01Pet   .setText(jtPet.getModel().getValueAt(preencher, 6).toString());
    this.jtfObservcaoPet  .setText(jtPet.getModel().getValueAt(preencher, 7).toString());
    this.jcbCliente.setSelectedItem(jtPet.getModel().getValueAt(preencher, 8).toString());
Criado 17 de novembro de 2018
Ultima resposta 18 de nov. de 2018
Respostas 4
Participantes 2