I have 2 classes, one of which also has a nested class:

1. Home.java (extends JApplet)

2. Away.java (extends JPanel) has nested class, private class ButtonListener implements ActionListener

Now in Home.java I have a Vector ( called bankaccount) which i pass to the Constructor of Away.java ( I declared the vector again there as an instance by Private Vector bankaccount;

My problem is that in ButtonListener I am trying to access the Vector so that i can modify it when a button has been clicked, and i thought i could just use the commands of bankaccount.add() and bankaccount.get() directly, however it seems like ButtonListener cannot access the Vector, even though i passed it along to the Constructor of the Away class.

Any suggestions on how i get access the Vector from the ButtonListenere class?

Here is a simplified version of what i am trying to do:

public class Home extends JApplet
{
private Away away;
private Vector bankaccount;
 
public void init()
    {
     bankaccount = new Vector();
     away = new Away(bankaccount);
    }
 
 }
 
 
public class Away extends JPanel
{
private Vector bankaccount;
JTextArea text1= new JTextArea(50,50);
 
public Away(Vector bankaccount)
{
   //declare buttons,textarea,setLayout,etc
 
button1.addActionListener (new ButtonListener()); 
//i can set  text1.setText(( String)bankaccount.get(0));
// here to access bankaccount, and it shows up fine, but i want
// do it in from the class below 
}
 
private class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{	
                     //trying to access bankaccount here by doing:
                    // but it is not doing anything
                    text1.setText(( String)bankaccount.get(0));
}
}

what sort of errors do you get, or nothing happens at all?

i have a feeling that your ButtonListener is not using the objects created in Away (the ones you want it to, anyway).

Yea im not getting any error, its just that bankaccount.get(0) isn't appearing in the TextArea

ok, so if you did something like text1.setText("random text"), does this produce anything into the TextArea?

my guess is not, and so, as I said you have different instances of your objects.

Yes if i put in some text it does produce the text in TextArea

ah, damn, guess i am wrong (you put that code within your ButtonListener right?).

well from the behaviour you have explained, (String)bankaccount.get(0) must be evaluating to an empty String.

In the constructor of Away you need to copy the bankaccount that's passed in to the local private variable bankaccount:

this.bankaccount = bankaccount;

ps Java naming convention says it should be called bankAccount

Thanks James that fixed it.

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.