how to write in a Jlabel from a new class ?

  • Thread starter Damiano Michael
  • Start date
D

Damiano Michael

First i create a Jframe and put a Jlabel on it.

Second i create a class called "Square" with a method called
"Send_To_Label".

When i write something like this in the "Send_To_Label" method :
jLabel1.setText("Hello");

I get "cannot resolve symbole jLabel1".

How to write into a label from a different class where it is declared ?

Thanks.

Michael.
 
S

Sudsy

Damiano said:
First i create a Jframe and put a Jlabel on it.

Second i create a class called "Square" with a method called
"Send_To_Label".

When i write something like this in the "Send_To_Label" method :
jLabel1.setText("Hello");

I get "cannot resolve symbole jLabel1".

How to write into a label from a different class where it is declared ?

Why don't you just pass it in the constructor? Something like this:

Square square = new Square( jLabel1 );

Now your object has a reference. The code could look something like
this:

public class Square {
private JLabel label;
public Square( JLabel label ) {
this.label = label;
}
public void Send_To_Label() {
label.setText( "Hello" );
}
}

Everything in java works on object references. You can't expect
to use object names like global variables in VB.
 
H

Herman Timmermans

Damiano said:
First i create a Jframe and put a Jlabel on it.

Second i create a class called "Square" with a method called
"Send_To_Label".

When i write something like this in the "Send_To_Label" method :
jLabel1.setText("Hello");

I get "cannot resolve symbole jLabel1".

How to write into a label from a different class where it is declared ?

Thanks.

Michael.

Hi Michael,
depends on the usage of access modifiers. Access modifiers control which
classes can use what features (the class itself, member variables, methods
and constructors).
So if you are using default (i.e. do not specify any access modifier), then
only classes that are in the package may access default features of classes
that are in the package.
So use a package to solve your issue, like this dummy code ....

package mytestcase;
class myFrame extends JFrame{
//put your label JLabel1
......}
class Square {
//send method
}

Or you could use access modifiers.
Hope this helps,
Brgds Herman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top