revalidate JPanel in GUI bUilder
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");
}
开发者_Go百科 });
}
revalidate();
repaint();
return 1;
}
One problem: In your ActionListener code, you create a new JPanel (or Jpl) object:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Jpl jpl=new Jpl();
int a= jpl.printMe(4);
}
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.
It is not clear what you are asking, so I'm just going to post comments about problems in your code.
printMe()
returns an int
, but it is always the same value, so it has no meaning. Just make the method void
.
String htmlLabel = "New Label+ i";
This is always going to produce the same string and will not give you the value of i
. Instead you want String htmlLabel = "New Label" + i;
You are not doing any thing with the JLabel
s you are making in the loop, so each object will no longer be referenced after the current iteration ends. In continuation of this, your MouseListener
will never be triggered because the label you are adding it to will never appear in the GUI.
In addition to the issues others have mentioned:
You are creating the labels, but you are not adding them to the JPanel. Try this:
JLabel lbl = new JLabel(htmlLabel);
this.add(lbl);
精彩评论