strong textpackage xcada; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.ResultSet; import javax.swing.JButton; import javax.swing.JLabel;
public class MyNew3 extends javax.swing.JFrame { private int q; private String r; private String data[ ]={ }; private String tagNo; private String findTagNo[ ]={ }; private JButton button1[]; private int m=0; private String t3=""; private String str5=""; private String t2="";
public MyNew3( String t2,String t3,int q) { initComponents(); this.q=q; this.t2=t2; this.t3=t3; StringThread th = new StringThread (); th.start(); } MyNew3() { }
private JLabel createlabel(int i,String tagNo) { final String tagNo1=tagNo; System.out.println(""+i); JLabel label1= new JLabel();
this.button1[i]=new JButton(); this.jPanel1.add(label1);//***Here is error coming..*** // System.out.println("I m not here"); System.out.println("I m here"); jPanel1.add(button1[i]); label1.setText(data[i]); button1[i].setText("Update"); label1.setBounds(110,100+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i, 375, 20); button1[i].setBounds(110,120+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i+i, 75, 15); label1.setFont(new Font("Tahoma", Font.BOLD, 14)); button1[i].addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { MyNew4 form4=new MyNew4(tagNo1); } }); return label1; //return button1; } public void display() { try { Connection con=(Connection) ConnectionFactory.getConnection(); String sql1="SELECT * FROM people"; PreparedStatement ps2=(PreparedStatement) con.prepareStatement(sql1); ResultSet rs2=ps2.executeQuery(); System.out.println(rs2); while(rs2.next()) { this.str5+=rs2.getString("Type")+":"+rs2.getString("Temp")+"Tag Number :"+rs2.getString("Tag")+"Unit :"+rs2.getString("Unit")+"\n#"; data=str5.split("#"); m++; } } catch(Exception e) { } JLabel label[]=new JLabel[m]; this.button1=new JButton[m]; System.out.println(""+m); for(int i=0;i<m;i++) { this.findTagNo=data[i].split("Tag Number :"); this.findTagNo=findTagNo[1].split("Unit :"); label[i]=createlabel(i,findTagNo[0]); } }
} class StringThread extends Thread {
StringThread()
{ super();
}
@Override public void run ( )
{
while(true) { try { new MyNew3().display(); // System.out.println("hi11"); this.sleep(1000); }
catch(Exception e)
{
System.out.println("Hi"); System.out.println(e.toString());
}
}
}
} sir, i m facing problem in Dynamically update the label. when i m changing the the value in database , its dynamically not updated prblem is jPanel.add(label11); and also not setting the bounds properly... plz resolve it