I am getting truble to repaint the JPanel in Gui builder can any body help me please.
here is main class that generate random Numbers in its Constructor
public class Main {
public static int q;
public Main(){
Random randomGenerator = new Random();
for (int idx = 1; idx <= 10; ++idx) {
q = randomGenerator.nextInt(100);
}
}
here is button click event in class A that include JPanel as custom code
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
new Main();
Jpl jpl=new Jpl();
}
Here is a class that add as custome component to class A
public class Jpl extends JPanel {
public Jpl() {
printMe(Main.q);
}
public int printMe(int q) {
removeAll();
for (int i = 0; i <q; i++) {
System.out.println("rinting lable");
String htmlLabel = "<html><font color=\"#A01070\">" + i + " New Lable </font></html>";
JLabel lbl = new JLabel(htmlLabel);
setLayout(new GridLayout(0, 1));
add(lbl, Jpl.RIGHT_ALIGNMENT);
lbl.setForeground(Color.BLUE);
Border border = BorderFactory.createLineBorder(Color.lightGray);
lbl.setBorder(border);
lbl.add(new JSeparator(SwingConstants.HORIZONTAL));
lbl.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
JLabel label = (JLabel) e.getSource();
JOptionPane.showMessageDialog(null, "You Slected");
System.out.println(label.getText() + "NO AKKA is Selected");
}
});
}
revalidate();
repaint();
return 1;
}
One problem: In your ActionListener code, you create a new JPanel (or Jpl) object:
but there is absolutely no way that this is the Jpl object that is displayed in your GUI. Yes it is an object of the same class, but it is a completely new different and distinct object from the one being displayed, and so calling a method on it will have no effect on the displayed Jpl object. The solution is to call your methods only on the displayed object. I cannot tell you how to get a reference to that object since we are not privy to the rest of your code where you display it.
The other problem is that you haven’t asked a question in this post, so I have no idea if my suggestion will help your primary problem (but I do know that it will solve a problem). So I suggest that you ask a proper question so we can better understand the issues and that you’ll likely need to post more code.