Results 1 to 6 of 6
Thread: problem with JLabel.setText();
- 10-08-2010, 06:36 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 20
- Rep Power
- 0
problem with JLabel.setText();
I have two classes:
Class1, Class2
Class1 has a Jbutton and class2 has a Jlabel
//////////////////////////////////////////////////////////////////////////
What am tryng to do is when I press Jbutton of class1, a method of class2 should be called to change the text of Jlabel.
My Code is:
code1:
////////Class1 Code////////
///////Class2 Code///////Java Code:public class Class1 extends javax.swing.JFrame implements actionListner { public Class1() { this.setLayout(new FlowLayout()); this.add(new Class2()); this.add(b1); b1.addActionListner(); } JButton b1 = new Jbutton(); Class2 clas2 = new Class2(); public void actionPerformed(ActionEvent e) { clas2.set(); }
The problem is that its not uppdating the text on JLabel...Java Code:public class Class2 extends javax.swing.JFrame implements actionListner { public Class2() { this.setLayout(new FlowLayout()); this.add(L1); } JLabel L1 = new JLabel("Original Text"); public void set(){ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { jLabel1.setText("Text Changed"); } } }); }
(system.out.println(L1.getText()) shows updated text)
How can I fix it please help!!!
- 10-08-2010, 06:57 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
please post a compilable code - your code makes no sense :)
Last edited by eRaaaa; 10-08-2010 at 07:03 AM.
- 10-08-2010, 07:06 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 20
- Rep Power
- 0
Thanks eRaaaaa for reply :)... is it ok now?Java Code:///////Class1 Code/////// import java.awt.FlowLayout; import java.awt.event.ActionListener; import javax.swing.*; public class Class1 extends javax.swing.JFrame implements actionListner { public Class1() { this.setLayout(new FlowLayout()); this.add(clas2); this.add(b1); b1.addActionListner(); } JButton b1 = new Jbutton(); Class2 clas2 = new Class2(); public void actionPerformed(ActionEvent e) { clas2.set(); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); } } /////Class2 Code/////// import javax.swing.* public class Class2 extends JPanel implements actionListner { public Class2() { this.setLayout(new FlowLayout()); this.add(L1); } JLabel L1 = new JLabel("Original Text"); public void set(){ SwingUtilities.invokeLater(new Runnable() { public void run() { jLabel1.setText("Text Changed"); } } }); }
- 10-08-2010, 07:14 AM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
not really :)
- what is "actionListner" ? (ActionListener?)
- JButton b1 = new Jbutton(); ( = new JButton() !!!)
- b1.addActionListner(); (addActionListener(this) ?? )
- JLabel L1 and jLabel1.setText ....
- Class2 does not implement ActionListener :)
- what is NewJFrame ??
do you mean:
Java Code:import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; public class Class1 extends javax.swing.JFrame implements ActionListener { public Class1() { this.setLayout(new FlowLayout()); this.add(clas2); this.add(b1); b1.addActionListener(this); } JButton b1 = new JButton(); Class2 clas2 = new Class2(); public void actionPerformed(ActionEvent e) { clas2.set(); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Class1().setVisible(true); } }); } }:confused: :eek:Java Code:import java.awt.EventQueue; import java.awt.FlowLayout; import javax.swing.JLabel; import javax.swing.JPanel; public class Class2 extends JPanel { public Class2() { this.setLayout(new FlowLayout()); this.add(L1); } JLabel L1 = new JLabel("Original Text"); public void set() { EventQueue.invokeLater(new Runnable() { @Override public void run() { L1.setText("Text Changed"); } }); } }
- 10-08-2010, 02:15 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 20
- Rep Power
- 0
Sooooorry! eRaaaa... yes you are right,
I was trying to summarize the problem, but in hurry I made mistakes in editing (I was getting late to college) ... But you predicted the right code :) ..
Sorry againLast edited by nonabhai; 10-08-2010 at 06:46 PM.
- 10-09-2010, 04:44 AM #6
Member
- Join Date
- Mar 2010
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
If statement and JLabel.setText() problem
By peterhabe in forum New To JavaReplies: 5Last Post: 07-25-2010, 10:11 AM -
setText() problem
By Jozo in forum Java AppletsReplies: 4Last Post: 04-27-2010, 05:29 AM -
Jlabel update problem
By fantasyme in forum AWT / SwingReplies: 3Last Post: 04-14-2010, 05:10 AM -
setText() problem
By jls7168 in forum New To JavaReplies: 2Last Post: 02-20-2009, 10:34 PM -
JLabel + GUI problem
By tonyelaltaico in forum Java AppletsReplies: 5Last Post: 02-03-2009, 01:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks