I'm receiving the error "local variable box is accessed from within inner class; needs to be declared final". That seems alright, but I don't really think it's the best solution, so I was hoping maybe someone else can help me out. Here is my code:
public void showPublisherBox(JComboBox box) {
if (publisherBox == null) {
publisherBox = new AddPublisherForm();
publisherBox.setLocationRelativeTo(this);
}
publisherBox.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
this.populatePublishers(box);
}
private void populatePublishers(JComboBox box){
box.setModel(db.getPublishers());
}
});
publisherBox.setVisible(true);
}
Publisher form is just a new JFrame that opens up and takes in some information, and when it's closed I want the JComboBox to be repopulated via setting the model from my db.getPublishers() method.
So is there a better way to do what I'm doing here, or am I going to have to declare something as final?
Thanks
Since you're not modifying box
in the outer code, using final
is exactly the correct way to solve this problem. It's a promise to yourself (and to the compiler) that the value of box
won't change in the enclosing scope. The compiler will tell you if you break that promise.
There is no compile time or runtime cost to using final
.
When an anonymous inner class (such as your WindowAdapter
) refers to variables in enclosing scopes (like 'box'), Java requires that they be declared final
.
One way to avoid having to declare box
as final
would be to make your WindowAdapter
a named class, that accepts the box
as a constructor parameter:
public void showPublisherBox(JComboBox box) {
if (publisherBox == null) {
publisherBox = new AddPublisherForm();
publisherBox.setLocationRelativeTo(this);
}
publisherBox.addWindowListener(new MyWindowClosingHandler(box, db));
publisherBox.setVisible(true);
}
// ...
class MyWindowClosingHandler extends WindowAdapter {
private JComboBox box;
MyWindowClosingHandler(JComboBox box, DB db) {
this.box = box;
this.db = db;
}
public void windowClosing(WindowEvent we) {
populatePublishers();
}
private void populatePublishers(){
box.setModel(db.getPublishers());
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With