0

I am trying to append a string to print into a JTextArea, but some reason it comes up with this error "Non-static variable cannot be referenced from a static context".

It should go to the database and get the data told to get. This is the problem

jTextArea1.append(resultado);

Here is my current code

 package ft3;

 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 import javax.swing.JOptionPane;



public class JanelaIListarProdutos1 extends javax.swing.JFrame {

/**
 * Creates new form JanelaIListarProdutos1
 */
public JanelaIListarProdutos1() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jPanel1.setBackground(new java.awt.Color(204, 204, 204));
    jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "LISTAGEM DE PRODUTOS", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 1, 14))); // NOI18N

    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jTextArea1.addInputMethodListener(new java.awt.event.InputMethodListener() {
        public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
        }
        public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
            jTextArea1InputMethodTextChanged(evt);
        }
    });
    jScrollPane1.setViewportView(jTextArea1);

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(29, 29, 29)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 421, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(31, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE)
            .addContainerGap())
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

private void jTextArea1InputMethodTextChanged(java.awt.event.InputMethodEvent evt) {                                                  
    // TODO add your handling code here:
}                                                 

/**
 * @param args the command line arguments
 */



public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(JanelaIListarProdutos1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(JanelaIListarProdutos1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(JanelaIListarProdutos1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(JanelaIListarProdutos1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>




    ligaBD ligacaoBD = new ligaBD();
    Connection con = ligacaoBD.obterLigacao();


    String query;
    Statement xpto;
    ClasseProduto classeproduto = null;

    try{
        xpto = con.createStatement();
        query = "SELECT codigo, nome, categoria, preco_s_iva, taxa_iva, preco_c_iva FROM produtos";
        ResultSet rs = xpto.executeQuery(query);
        while (rs.next()){
            classeproduto = new ClasseProduto();
            classeproduto.setCodigo(rs.getInt("codigo"));
            classeproduto.setNome(rs.getString("nome"));
            classeproduto.setCategoria(rs.getString("categoria"));
            classeproduto.setPreco_s_iva(rs.getDouble("preco_s_iva"));
            classeproduto.setTaxa_iva(rs.getDouble("taxa_iva"));
            classeproduto.setPreco_c_iva(rs.getDouble("preco_c_iva"));

            //int codigo = rs.getInt(1);
            //String nome = rs.getString(2);
            //String categoria = rs.getString(3);
            //Double preco_s_iva = rs.getDouble(4);
            //Double taxa_iva = rs.getDouble(5);
            //Double preco_c_iva = rs.getDouble(6);

           String resultado = "Codigo: "+classeproduto.getCodigo()+"\r\n"+"Nome: "+classeproduto.getNome()+"\r\n"+"Categoria: "+classeproduto.getCategoria()+"\r\n"+"Preco Sem IVA: "+classeproduto.getPreco_s_iva()+"\r\n"+"Taxa da IVA: "+classeproduto.getTaxa_iva()+"\r\n"+"Preco Com IVA: "+classeproduto.getPreco_c_iva();
            pesquisa jt = new pesquisa(); 
            jTextArea1.append(resultado);

           // System.out.println ("Codigo: "+classeproduto.getCodigo()+"\r\n"+"Nome: "+classeproduto.getNome()+"\r\n"+"Categoria: "+classeproduto.getCategoria()+"\r\n"+"Preco Sem IVA: "+classeproduto.getPreco_s_iva()+"\r\n"+"Taxa da IVA: "+classeproduto.getTaxa_iva()+"\r\n"+"Preco Com IVA: "+classeproduto.getPreco_c_iva());

        }




    } catch (SQLException sqle) {
        JOptionPane.showMessageDialog(null, sqle + "Erro no query");
        //System.out.println("Erro no query");
    }

    ligacaoBD.fecharligacao(con);



    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new JanelaIListarProdutos1().setVisible(true);
        }
    });
}


// Variables declaration - do not modify                     
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration                   

}

0 Answers0