Save variable value and send outside ActionListener and return problem!
Ok. Here is my code. I getText from textField to variable baza,and I need to save this value outside ActionListener and return. But here...System.out.println("Spolja: "+baza); i got null value. So my return baza is null. Can someone help me?
String baza;
public String adresa()
{
unesiB.setText("Potvrdi");
unesiB.setBorder(BorderFactory.createRaisedBevelBorder());
unesiB.setForeground(MyConstants.blueColor);
unesiB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
baza=CustomerIDFTF.getText()开发者_如何学C;
System.out.println("Unutra: " +baza);
}
});
unesiB.setHorizontalAlignment(SwingConstants.CENTER);
unesiB.setBounds(new Rectangle(150, 90, 130, 30));
panel.add(unesiB);
System.out.println("Spolja: "+baza);
return baza;
}
That's because baza
is not being set until the user performs some action.
The only place you actually assign a value to the variable is inside your definition of actionPerformed
for the anonymous ActionListener
object. This of course will not be set until the user performs whatever action - which probably can't even happen during the invocation of this method, unless you have some really bizzare concurrency issues.
It looks like you're trying to combine a setup piece of code with a getter piece of code. I reccommend seperating them properly - and you may want to review the level of separation in the rest of your code, too, as it looks like you're probably violating MVC (Specifically, cross-contamination model and view code).
For a more immediate fix, though, you could simply initialize baza
to blank.
精彩评论