Re: [Program] FTL Homeworld
kartoFlane wrote:iceburg333 wrote:The problem is, in "if (SpaceDockUI.currentShip != this"), this is an ActionListener
Well, there's a very simple way to work around that. And it only requires a single private variable, set to a certain object...
In case you couldn't figure it out, check this.
Another way to get the outer class's "this" from within an inner class is...
Code: Select all
if (SpaceDockUI.currentShip != ShipSave.this)
Though if I'm doing a lot of outer-self-referencing, I tend to do the "self" private variable trick, or just give the outer class an actionPerformed method (ShipSave extends ... implements ActionListener).
I haven't looked at the code here, but it may be preferable to move the click logic into the class that is constructing all the clickable things. Create a single shared ActionListener there, create each clickable thing, and add the listener, as part of filling the GUI.
For instance a panel with lots of buttons. The panel creates a listener (or itself implements actionPerformed). The panel creates buttons, adds the buttons to itself (the panel), and adds that same listener to all the buttons. When a button is clicked, the listener determines which one specifically got clicked via the ActionEvent's getSource() method (then doing an instanceof check and casting as appropriate).