0

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?

Edited by happygeek: fixed formatting

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by bops
0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.