JLabel is not displayed

2

I created a label where it would contain the status of the connection process with the database, but it does not appear and I have no idea why this happens, since I have been looking at the code for three days and I do not know What happens, it would be great if someone helped me find the cause, please.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

public class Funcionarios extends Janela implements ActionListener{

    Connection conexao = Conectar.getConnection();
    String status = Conectar.status;

    JTextField txt_codigo, txt_nome, txt_ende, txt_tele, txt_nasc, txt_cpf, txt_carg, txt_email;
    JLabel lbl_codigo, lbl_nome, lbl_ende, lbl_tele, lbl_nasc, lbl_cpf, lbl_carg, lbl_email, lbl_status, txt_status;
    JButton btn_inserir, btn_alterar, btn_excluir, btn_consultar;

    public Funcionarios(){
        super("Funcionários", new Dimension(460, 400));
        //getContentPane().setBackground(new Color(56, 76, 244));
        setLocationRelativeTo(null);
        setLayout(null);

        // Definição do posicionamento das labels
        lbl_codigo = new JLabel("Código:");
        lbl_codigo.setBounds(10, 30, 80, 15);
        getContentPane().add(lbl_codigo);
        lbl_codigo.setForeground(Color.black);

        lbl_nome = new JLabel("Nome:");
        lbl_nome.setBounds(10, 60, 80, 15);
        getContentPane().add(lbl_nome);
        lbl_nome.setForeground(Color.black);

        lbl_ende = new JLabel("Endereço:");
        lbl_ende.setBounds(10, 90, 80, 15);
        getContentPane().add(lbl_ende);
        lbl_ende.setForeground(Color.black);

        lbl_tele = new JLabel("Telefone:");
        lbl_tele.setBounds(10, 120, 60, 15);
        getContentPane().add(lbl_tele);
        lbl_tele.setForeground(Color.black);

        lbl_nasc = new JLabel("Data de nascimento:");
        lbl_nasc.setBounds(10, 150, 200, 15);
        getContentPane().add(lbl_nasc);
        lbl_nasc.setForeground(Color.black);

        lbl_cpf = new JLabel("CPF:");
        lbl_cpf.setBounds(10, 180, 50, 15);
        getContentPane().add(lbl_cpf);
        lbl_cpf.setForeground(Color.black);

        lbl_carg = new JLabel("Cargo:");
        lbl_carg.setBounds(10, 210, 50, 15);
        getContentPane().add(lbl_carg);
        lbl_carg.setForeground(Color.black);

        lbl_email = new JLabel("E-mail:");
        lbl_email.setBounds(10, 240, 50, 15);
        getContentPane().add(lbl_email);
        lbl_email.setForeground(Color.black);

        // Definição do posicionamento das caixas de texto
        txt_codigo = new JTextField();
        txt_codigo.setBounds(130, 30, 150, 20);
        getContentPane().add(txt_codigo);

        txt_nome = new JTextField("");
        txt_nome.setBounds(130, 60, 150, 20);
        getContentPane().add(txt_nome);

        txt_ende = new JTextField("");
        txt_ende.setBounds(130, 90, 150, 20);
        getContentPane().add(txt_ende);

        txt_tele = new JTextField("");
        txt_tele.setBounds(130, 120, 150, 20);
        getContentPane().add(txt_tele);

        txt_nasc = new JTextField("");
        txt_nasc.setBounds(130, 150, 150, 20);
        getContentPane().add(txt_nasc);

        txt_cpf = new JTextField();
        txt_cpf.setBounds(130, 180, 150, 20);
        getContentPane().add(txt_cpf);

        txt_carg = new JTextField("");
        txt_carg.setBounds(130, 210, 150, 20);
        getContentPane().add(txt_carg);

        txt_email = new JTextField("");
        txt_email.setBounds(130, 240, 150, 20);
        getContentPane().add(txt_email);

        // Definição do posicionamento dos botões
        btn_inserir = new JButton("Cadastrar");
        btn_inserir.setBounds(10, 320, 100, 30);
        getContentPane().add(btn_inserir);

        btn_alterar = new JButton("Alterar");
        btn_alterar.setBounds(120, 320, 100, 30);
        getContentPane().add(btn_alterar);

        btn_excluir = new JButton("Excluir");
        btn_excluir.setBounds(230, 320, 100, 30);
        getContentPane().add(btn_excluir);

        btn_consultar = new JButton("Consultar");
        btn_consultar.setBounds(340, 320, 100, 30);
        getContentPane().add(btn_consultar);

        // Posição do status
        lbl_status = new JLabel("Status:");
        lbl_status.setBounds(10, 280, 50,15);
        getContentPane().add(lbl_status);
        lbl_status.setForeground(Color.black);

        txt_status = new JLabel(this.status);
        txt_status.setForeground(Color.RED);
        txt_status.setBounds(10, 280, 200, 20);

        // Atribuindo eventos aos botões
        btn_inserir.addActionListener(this);
        btn_alterar.addActionListener(this);
        btn_excluir.addActionListener(this);
        btn_alterar.addActionListener(this);
    }

    public static void main(String args[]) {
        Funcionarios tela;
        tela = new Funcionarios();
        tela.setVisible(true);
        tela.setResizable(false);
    }

    void LimpaCampos() {
        txt_codigo.setText("");
        txt_nome.setText("");
        txt_ende.setText("");
        txt_tele.setText("");
        txt_nasc.setText("");
        txt_cpf.setText("");
        txt_carg.setText("");
        txt_email.setText("");
    }

    public void actionPerformed(ActionEvent botao) {
        if(botao.getSource() == btn_inserir) {
            boolean ok;
            ok = gravaFuncionarios();
            if(ok) {
                LimpaCampos();
                txt_status.setText("Dados gravados.");
            }
            else {
                txt_status.setText("Erro ao gravar");
            }
        }
        if(botao.getSource() == btn_excluir) {
            boolean ok;
            ok = excluirFuncionarios();
            if(ok) {
                LimpaCampos();
                txt_status.setText("Registro excluído.");
            }
            else {
                txt_status.setText("Código não cadastrado.");
                LimpaCampos();
            }
        }

        if (botao.getSource()== btn_alterar) {
            boolean ok ;
            ok = alterarFuncionarios();
            if (ok){
                txt_status.setText("Registro Alterado");
            }else{
                txt_status.setText("Código não encontrado");
            }
            LimpaCampos();
        }
        if(botao.getSource() == btn_consultar ){
            boolean ok ;
            ok = consultaFuncionarios();
            if ( ok ){
                txt_status.setText("");
            }else{
                txt_status.setText("Código não cadastrado");
                LimpaCampos();
            }

        }

    }

    boolean gravaFuncionarios() {
        boolean ok = true;
        //int vCodigo = Integer.parseInt(txt_codigo.getText());
        String vNome = txt_nome.getText();
        String vEnde = txt_ende.getText();
        String vTele = txt_tele.getText();
        String vNasc = txt_nasc.getText();
        String vCPF = txt_cpf.getText();
        String vCargo = txt_carg.getText();
        String vEmail = txt_email.getText();

        String vSQL;
        vSQL = "INSERT INTO FUNCIONARIOS VALUES('"+ vNome +"', '"+ vEnde +"', '"+ vTele +"', '"+ vNasc +"', '"+ vCPF +"', '"+ vCargo +"', '"+ vEmail +"')";

        try {
            Statement st = conexao.createStatement();
            st.executeUpdate(vSQL);
        }
        catch(SQLException erro) {
            ok = false;
        }
        return ok;
    }

    boolean excluirFuncionarios() {
        boolean ok = true;

        int vCodigo = Integer.parseInt(txt_codigo.getText());

        String vSQL;
        vSQL = "DELETE FROM FUNCIONARIOS WHERE CodFunc=" + vCodigo;

        try {
            Statement st = conexao.createStatement();
            st.executeUpdate(vSQL);
        }
        catch(SQLException erro) {
            ok = false;
        }
        return ok;
    }

    boolean alterarFuncionarios() {
        boolean ok = true;
        int vCodigo = Integer.parseInt(txt_codigo.getText());
        String vNome = txt_nome.getText();
        String vEnde = txt_ende.getText();
        String vTele = txt_tele.getText();
        String vNasc = txt_nasc.getText();
        String vCPF = txt_cpf.getText();
        String vCargo = txt_carg.getText();
        String vEmail = txt_email.getText();

        String vSQL;
        vSQL = "UPDATE FUNCIONARIOS SET NomFunc='" + vNome + "', EndFunc='" + vEnde + "' TelFunc='" + vTele + "' NasFunc='" + vNasc + "' CPFFunc='" + vCPF + "' CgoFunc='" + vCargo + "' EmaFunc='" + vEmail + "'WHERE CodFunc=" + vCodigo;

        try {
            Statement st = conexao.createStatement();
            st.executeUpdate(vSQL);
        }
        catch(SQLException erro) {
            ok = false;
        }
        return ok;
    }

    boolean consultaFuncionarios(){
        boolean ok = true ;
        int vCodigo = Integer.parseInt(txt_codigo.getText());
        String sql; 

        sql = "SELECT * FROM FUNCIONARIOS WHERE CodFunc=" + vCodigo ;
        try{
            Statement st = conexao.createStatement();
            ResultSet rs = st.executeQuery(sql) ;
            if (rs.next()){
                txt_nome.setText(rs.getString("NomFunc"));
                txt_ende.setText(rs.getString("EndFunc"));
                txt_tele.setText(rs.getString("TelFunc"));
                txt_nasc.setText(rs.getString("NasFunc"));
                txt_cpf.setText(rs.getString("CPFFunc"));
                txt_carg.setText(rs.getString("CgoFunc"));
                txt_email.setText(rs.getString("EmaFunc"));
            }else{
                ok = false ; 
            }
        }catch(Exception erro){
            ok = false ;
        }
        return ok ;
    }
}

Please help me !!!!!! And thank you.

    
asked by anonymous 28.11.2014 / 22:59

1 answer

3

Your problem is that it seems like you forgot about it:

    getContentPane().add(txt_status);

To be placed right after this:

    txt_status = new JLabel(this.status);
    txt_status.setForeground(Color.RED);
    txt_status.setBounds(10, 280, 200, 20);

In addition the position of this Label on the screen appears to be incorrect. I think it should be this:

    txt_status.setBounds(130, 280, 200, 15);

To help you, I should also say that your program has several other problems:

  • SQL Injection.

  • Manipulate Swing components outside the EDT . This can cause bugs that are difficult to crawl.

  • A generic ActionListener instead of using specific ActionListener s. This tends to make the code confusing and difficult to move.

  • Blend of data access code with UI code. This is a bad programming practice.

  • Non-standard variable naming.

  • You use fields with packet visibility. Rarely is this what is desired. I think they should be private.

  • Handling exceptions well half a mouth. You show "Code not registered" and "Code not found" if the user did not find what he was looking for or if there was an error in accessing the database. It is important to distinguish these two situations to prevent the user from thinking that the information has disappeared from the system when an error in the connection to the database actually occurred.

  • Input kludge : If the user types a text instead of a number in txt_codigo , it will pop a NumberFormatException . If it types too large a text to fit into the database in some fields, it will pop a SQLException as well.

  • It seems like txt_codigo is a global variable. And you should know that using global variables is not a good programming practice.

  • When closing the window, nothing happens. The system does not shut down. You may have put the code to do this in the Conectar.status superclass, but it may be missing.

29.11.2014 / 01:06