JLabel text disappears after less than a second
Question
I made a JFrame in netbeans ( it currently contains only one JLabel) which connects to the database and gets a field and display it in the JLabel. when I run the program, the JLabel displays the field for less then a second, then the text in the JLabel disappears !! Can anyone tell me why is this happening ? I also tried it in a JTextField, also same problem.
code :
public class frame extends javax.swing.JFrame {
public frame() {
initComponents();
}
public void doingAll() {
StringBuffer message = new StringBuffer();
try {
Statement stmt;
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://localhost:3306/Junk2";
Connection con = DriverManager.getConnection( url,"root", "");
System.out.println("URL: " + url);
System.out.println("Connection: " + con);
stmt = con.createStatement();
stmt.executeQuery("select * from Junk2.example");
ResultSet rs = stmt.executeQuery("select * from Junk2.example");
int columns = rs.getMetaData().getColumnCount();
while(rs.next()){
for (int i=1;i<=columns;i++){
message.append(rs.getString(i)+" ");
}
message.append("\n");
}
System.out.println(message);
con.close();
}catch( Exception e ) {
e.printStackTrace();
}//end catch
jLabel1.setText(message.toString());
jTextField1.setText(message.toString());
java.awt.EventQueue.invokeLater(new Runnable(){public void run(){new frame().setVisible(true);}});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
Solution
I am guessing that somewhere else in your code, you are clearing the field.
Search through your code for places where
- you do setText("");
- remove the label from the screen
- draw another component over the top of the label
- have implemented your own draw method.
Alternatively, post you code here. Your question is far to vague to give a clear definite answer.
edit: following your code show
Your doingAll method is not static, which means you will have to have created a frame object to call it. Then, at the end of that method, you are creating a new frame with the code
java.awt.EventQueue.invokeLater(new Runnable(){public void run(){new frame().setVisible(true);}});
This doesn't make sense. There is more going on here, as this is not all your code, but I guess the above line of code is the starting point for some (if not all) of your problems.