Reputation: 13
Trying to display sql results in a JLabel. I'm using netbeans so it does auto code generation for the swing components.
Welcome_screen.java, main method
package my.welcomescreen;
import java.io.InputStream;
import java.sql.*;
import javax.swing.*;
public class Welcome_screen extends javax.swing.JFrame {
public Welcome_screen() {
initComponents();
}
private void initComponents() {
jLabel9 = new javax.swing.JLabel();
}
public static void main(String args[]) {
Welcome_screen courses = new Welcome_screen();
courses.getCourses();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Welcome_screen().setVisible(true);
}
});
}
public void getCourses(){
db obj = new db();
obj.dbconnect();
String Query = "select * from courses";
try{
Connection con = obj.dbconnect();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(Query);
if(rs.next()){
//System.out.println(rs.getString("name"));
jLabel9.setText(rs.getString("name"));
}
}catch(Exception e){
System.out.println(e);
}
}
private javax.swing.JLabel jLabel9;
}
db.java, database connect file.
package my.welcomescreen;
import java.sql.*;
public class db {
public static Connection dbconnect(){
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/st2", "root", "");
}
catch (ClassNotFoundException | SQLException e){
System.out.println(e);
}
return con;
}
}
when i print sql results to the command line it works. expected result is displaying. But in the JLabel nothing happens. Not even an error is occurring. I can't find what's wrong. A little help would be appreciated !
Upvotes: 1
Views: 2295
Reputation: 351
Please try this like -
jlabel.setText("Your Text");
jLabel.setVisisble(true);
Upvotes: 0
Reputation: 306
At first: YOU HAVE TO PLACE THE JLABEL on the Frame:
getContentpane().add(label9);
And after on i recommend to take a look at the following Methods:
rame.setSize(width, height);
Frame.getContentPane();
Container/JComponent.add(Component c);
Component.setLayout(LayountManager manager);
I recommend to watch more vids or read Blogs about Swing bevore continuing. But here is the code u should use:
private void initComponents() {
label9 = new JLabel();
label9.setText("SOME TEXT");
JPanel p = new JPanel();
p.setSize(100, 50);
p.setLayout(new FlowLayout());
p.add(jLabel9);
p.setVisible(true);
setSize(200, 200);
getContentPane().add(p);
}
Try to call
jlabel9.setText("Your readen text!");
jlabel9.repaint();
It will repaint the label and update the content.
Upvotes: 2
Reputation: 2222
You need to locate jLabel9
on the jPanel and it on Frame
. One of the way is to use a constructor. As your class is exended from JFrame, you don't need to use this.add()
or JFrame jf = new JFrame();
private void initComponents() {
jLabel9 = new javax.swing.JLabel();
jLabel9.setBackground(Color.red);
jLabel9.setVisible(true);
jLabel9.setText("Hello");
JPanel jp = new JPanel();
jp.setSize(100, 50);
jp.add(jLabel9);
jp.setVisible(true);
setSize(200, 200);
add(jp);
setVisible(true);
}
or directly on JFrame
private void initComponents() {
jLabel9 = new javax.swing.JLabel();
jLabel9.setBackground(Color.red);
jLabel9.setVisible(true);
jLabel9.setText("Hello");
setSize(200, 200);
add(jLabel9);
// one of other way to use contentpane
// getContantPane().add(jLabel9);
setVisible(true);
}
Upvotes: 1