Hi,

I am having problems in accessing the value of a string variable (string1 here).

It is working in one way when i put it inside constructor:-

public class Demo1 extends JFrame implements ActionListener, ItemListener
public string string1;
Demo1 {
Action a1 = new AbstractAction("Some Action")
        {
            public void actionPerformed(ActionEvent e)
            {
              System.out.println(string1);
}
}
} // End of constructor Demo1
.....
class Class2 implements TreeSelectionListener
    {
public void valueChanged(TreeSelectionEvent event) {
// assigning some value to string 1

}
}

Hi,

I am having problems in accessing the value of a string variable (string1 here).

It is working in one way when i put it inside constructor:-
CODE

public class Demo1 extends JFrame implements ActionListener, ItemListener
public string string1;
Demo1 {
Action a1 = new AbstractAction("Some Action")
{
public void actionPerformed(ActionEvent e)
{
System.out.println(string1);
}
}
} // End of constructor Demo1
.....
class Class2 implements TreeSelectionListener
{
public void valueChanged(TreeSelectionEvent event) {
// assigning some value to string 1

}
}


But it doesn't work the other was ? How can I make it work this way ? Please suggest

public class Demo1 extends JFrame implements ActionListener, ItemListener
public string string1;
Demo1 {

..............................
} // End of constructor Demo1

public JPanel MyPanel() {
Action a1 = new AbstractAction("Some Action")
        {
            public void actionPerformed(ActionEvent e)
            {
              System.out.println(string1);
}
}
}
.....
class Class2 implements TreeSelectionListener
    {
public void valueChanged(TreeSelectionEvent event) {
// assigning some value to string1

}
}

Can anybody please suggest what should i do to access the variable ?

Thanks in advance

I would normally declare it like just after the main class, like this:

public class Demo1 extends JFrame implements ActionListener, ItemListener
Demo1 {
[B]String string1 = "here";[/B]
..............................


public JPanel MyPanel() {
Action a1 = new AbstractAction("Some Action")
        {
            public void actionPerformed(ActionEvent e)
            {
              System.out.println(string1);
}
}
}
.....
class Class2 implements TreeSelectionListener
    {
public void valueChanged(TreeSelectionEvent event) {
// assigning some value to string1

}
}

} // End of constructor Demo1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.