Components that interact with eachother
i've been programming a level editor for the past few days, and have come across something that doesn't seem right to me. i have a JFrame on which i instantiate two JPanels. One as a statusbar, which displays the selected image, and another which holds tiles. Each tile has a corresponding number. i have an ActionListener which sees which tile the user clicks on and then proceeds to set the statusbars' image. right now i do this by calling
Code :
MapEditor.statusBar.setImage(cTile,iCol);
MapEditor being the JFrame and statusBar being the statusbar that is instantiated in the MapEditor class.
this seems like really bad code to me yet i can not find a better way to do this.
any tips would help :D thanks.
Re: Components that interact with eachother
I'm guessing MapEditor is a static reference to the class name?
A simple way to avoid any problems with a second MapEditor object being created is to move your statusBar variable from static to instanced. Then, pass the reference to the statusBar panel to your ActionListener's constructor and have your ActionListener remember that reference. Then simply make the change on that object and it will be updated in the correct statusBar (so long as you didn't create a new statusBar without updating your ActionListener)
Code :
public class MyActionListener implements ActionListener
{
private JPanel statusBar;
MyActionListener(JPanel statusBar)
{
this.statusBar = statusBar;
}
public void actionPerformed(ActionEvent e)
{
// your code for figuring out what to perform
this.statusBar.setImage(cTile,iCol);
}
}
Re: Components that interact with eachother
thank you that solved my problem perfectly