Need help with updating label
I am trying to update the text on the label through the use of the actionListener, however no matter what i do i can't get it to work.
Here is the relevent code
Code Java:
public JPanel panel(){
JPanel panel = new JPanel();
panel.add(label());
panel.setPreferredSize(new Dimension(100,100));
panel.add(button());
return panel;
}
public JLabel label(){
JLabel label = new JLabel();
label.setText("Some Updating text: "+i);
return label;
}
public JButton button(){
JButton button = new JButton();
button.setPreferredSize(new Dimension(20,20));
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
i = i+1;
label().repaint();
}
});
return button;
}
Any help on this issue will be appreciated.
Re: Need help with updating label
You must set the value of the JLabel if you wish to update it by calling setText
Re: Need help with updating label
Could you give me some example code on how to do this?
Re: Need help with updating label
Quote:
Originally Posted by
Neacel
Could you give me some example code on how to do this?
You already have example code in your label method. Just call the same setText method in your actionPerformed with the new value of i (just changing the value of i does not inherently change the value shown in the JLabel)
Re: Need help with updating label
Code Java:
public JButton button(){
JButton button = new JButton();
button.setPreferredSize(new Dimension(20,20));
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
i = i+1;
label().setText("Some Updating text: "+i);
label().repaint();
}
});
return button;
}
This is what i have written, but it still doesn't update
Re: Need help with updating label
I got it working... i added
public JLabel label1;
then modified the JLabel Method and actionPerformed to include label1.