-2
Hello, I am creating an application that needs to show some registered elements in a list, which I will show through Defaultlistmodel. Unfortunately within the application it recognizes the elements that were inserted but not as it should, since it does not show the name of the element but rather the path of the entity, wanted to know how do pro Deufaltlistmodel recognize my object and the elements within it as type String, not as random code. I’ve seen a similar post but it didn’t work for me there says to overwrite toString() but still doesn’t work.
Code of the class that creates the screen jam and inside it methods to fetch the values of the Bank:
public class CargosConsultar extends JPanel{
Cargos cargoAtual;
JLabel labelTitulo, labelCargo;
JTextField campoCargo;
JButton botaoPesquisar, botaoEditar, botaoExcluir;
DefaultListModel <Cargos> listasCargosModelo = new DefaultListModel();
JList <Cargos> listaCargos = new JList();
public CargosConsultar(){
criarComponentes();
criarEventos();
}
private void criarComponentes() {
setLayout (null);
labelTitulo = new JLabel("Consulta de cargos", JLabel.CENTER);
labelTitulo.setFont(new Font(labelTitulo.getFont().getName(), Font.PLAIN, 20));
labelCargo = new JLabel ("Nome do cargo", JLabel.LEFT);
campoCargo = new JTextField();
botaoPesquisar = new JButton("Pesquisar Cargo");
botaoEditar = new JButton ("Editar Cargo");
botaoEditar.setEnabled(false);
botaoExcluir = new JButton ("Excluir Cargo");
botaoExcluir.setEnabled(false);
listasCargosModelo = new DefaultListModel();
listaCargos = new JList();
listaCargos.setModel(listasCargosModelo);
listaCargos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
labelTitulo.setBounds(20, 20, 660, 40);
labelCargo.setBounds(150, 120, 400, 20);
campoCargo.setBounds(20, 140, 400, 40);
botaoPesquisar.setBounds(560, 140, 130, 40);
listaCargos.setBounds(150, 200, 400, 240);
botaoEditar.setBounds(560, 360, 130, 40);
botaoExcluir.setBounds(560, 400, 130, 40);
add(labelTitulo);
add(labelCargo);
add(campoCargo);
add(botaoPesquisar);
add(listaCargos);
add(botaoEditar);
add(botaoExcluir);
setVisible(true);
}
private void criarEventos(){
botaoPesquisar.addActionListener(e ->{
sqlConsultaCargos(campoCargo.getText());
});
}
private void sqlConsultaCargos(String nome){
Statement instrucaoSQL;
ResultSet rs;
try {
//conectando no banco de dados
Connection con = ConnectionFactory.getConnection();
//criando a instrução SQL
instrucaoSQL = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = instrucaoSQL.executeQuery("SELECT * FROM cargo WHERE nome like '%"+nome+"%'");
listasCargosModelo.clear();
while(rs.next()){
Cargos cargo = new Cargos();
cargo.setId(rs.getInt("id"));
cargo.setNome(rs.getString("nome"));
listasCargosModelo.addElement(cargo);
System.out.println("nome"+ cargo.getNome());
con.close();
}
}catch(SQLException ex){
JOptionPane.showMessageDialog(null,"Ocorreu um erro ao consultar os cargos"+ ex);
}
}
}
Class that owns the entity of Positions:
public class Cargos {
private int id;
private String nome;
@Override
public String toString(){
return getNome();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}