Start New Discussion within our Software Development Community

Hi everybody,
I am new at GUI and I need your help..
I have a JPanel, named "holdAll", layout of which is set to BorderLayout. I have implemented all other JPanels in different class files. For example, I have TopPanel, LeftPanel, etc. as shown below

LeftPanel myLeft = new LeftPanel();
holdAll.add(myLeft, BorderLayout.WEST);


RightPanel myRight = new RightPanel();
holdAll.add(myRight, BorderLayout.EAST);


TopPanel myTop = new TopPanel();
holdAll.add(myTop, BorderLayout.NORTH);


MiddlePanel myMiddle = new MiddlePanel();
holdAll.add(myMiddle, BorderLayout.CENTER);


BottomPanel myBottom = new BottomPanel();
holdAll.add(myBottom, BorderLayout.SOUTH);

That works well but I have difficulties in ActionListeners.. For example, in my TopPanel class I have the code below:

public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnInsert) {
int returnVal = fc.showOpenDialog(fc);


if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
myImage newImage = new myImage(file);
System.out.println(newImage.myHashCode());
}
}
}

Here, if I have upload new image, then I have to update the listImage in the LeftPanel class.. But, I could not access the lstImage object in the LeftPanel class from the actionListener event in the TopPanel class.

What should I do? Is my design poor?

I'm not quite sure but maybe you could access an object from within its panel by creating accessor methods in your panel classes such as..

public class MyTopPanel extends JPanel
{
   private JButton myButton;
   // other code here ...
   // accesssor method for myButton
   public JButton getMyButton() {
      return this.myButton;
   }
}

then use topPanel.getMyButton() to retrieve the button object, if I understood you correctly. (topPanel is an instance of the class MyTopPanel.)

This article has been dead for over six months. Start a new discussion instead.