Show item of an Arraylist in a Jtextfield

Asked

Viewed 75 times

0

Good evening community, I have a problem in a Project I am working, I created a table in mysql with a few words, after that I made a Resultset of this table and saved the names of the words obtained from this Resultset in an Arraylist, Now I want to show some Word that’s in this Arraylist on Jtextfield but when I try to display it, it shows nothing. Here’s a Problem Print and the Full Code, from now on, Thank you!.inserir a descrição da imagem aqui

import java.awt.Borderlayout; import java.awt.Eventqueue; import java.util.Arraylist; import java.util.Collections; import java.util.List;

import javax.swing.Jframe; import javax.swing.Jpanel; import javax.swing.border.Emptyborder;

import com.mysql.Cj.x.protobuf.MysqlxCrud.Collection; import com.mysql.Cj.x.protobuf.MysqlxSql.Stmtexecute;

import javax.swing.Jtextfield; import javax.swing.Jtextpane; import java.awt.Color; import java.awt.Font; import java.sql.Connection; import java.sql.Preparedstatement; import java.sql.Resultset; import java.sql.Sqlexception; import java.sql.Statement;

import javax.swing.Swingconstants;

public class Menu extends Jframe {

private JPanel contentPane;
private List Palavras;


public static void main(String[] args) throws SQLException {
    
    List Palavras = new ArrayList<>();
    List PalavrasSql = new ArrayList<>();
    
    FactoryConnection fc = new FactoryConnection();
    Connection cc = fc.Connect();
    System.out.println("Conectado!");
    
    Statement stm = cc.createStatement();
    stm.execute("select * from Palavras");
    ResultSet rst = stm.getResultSet();
    while (rst.next()) {
        Palavras.add(rst.getString("Palavra"));
        System.out.println(Palavras);
    }
    
    System.out.println(Palavras.get(2));
    
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Menu frame = new Menu();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public Menu() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBackground(Color.BLUE);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    
    JTextField PainelPalavra = new JTextField();
    PainelPalavra.setColumns(5);
    PainelPalavra.setHorizontalAlignment(SwingConstants.CENTER);
    PainelPalavra.setForeground(Color.BLACK);
    PainelPalavra.setFont(new Font("Sylfaen", Font.BOLD, 18));
    PainelPalavra.setEditable(false);
    PainelPalavra.setBackground(Color.LIGHT_GRAY);
    PainelPalavra.setBounds(103, 67, 237, 114);
    contentPane.add(PainelPalavra);
    try {
        PainelPalavra.setText(Palavras.get(0).toString());
    } catch (Exception e) {
        Exception NullPointerException;
    }
}

}

  • Welcome to the site. Your code can improve, among other things it’s mixing database access code with presentation code, access to the database is potentially subject to SQL Injection and you’re capturing generic rather than specific exceptions (some you shouldn’t even capture). I particularly suggest taking to the Stackexchange Code Review (note: the people there speak English) when it is working properly and accept the feedback they give you there. It’s a good learning experience.

1 answer

1

The error is that you are declaring two variables with the same name Palavras (that by the way is outside the Java standard, the right is in lowercase) and making confusion between them.

One is instance variable (which you do not fill) and another is local method variable main() (the one you fill in, only you’re not using in the JTextField, is using the other).

Browser other questions tagged

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