I got this as home exercise but I have no clue how to solve it… (Just the access part is kind of difficult to figure out)
So, I have two classes: “Top” and “Main”. The Top class looks like this: (Short explanation: The Top class’ variable “pane” will be used in the class Main as the top panel, Main extends JFrame and has a BorderLayout)
public class Top extends JPanel implements ActionListener {
//public JPanel pane;
public JButton red, green, blue, white, black;
public Top() {
//pane = new JPanel(); //Useless, as Top is already a JPanel
this.setLayout(new FlowLayout(FlowLayout.CENTER));
this.setBackground(new Color(0xc9c9c9)); //A gray background
red = new JButton("Red");
red.setBackground(Color.red);
red.addActionListener(this);
this.add(red);
green = new JButton("Green");
green.setBackground(Color.green);
green.addActionListener(this);
this.add(green);
blue = new JButton("Blue");
blue.setBackground(Color.blue);
blue.addActionListener(this);
this.add(green);
white = new JButton("White");
white.setBackground(Color.white);
white.addActionListener(this);
this.add(white);
black = new JButton("Black");
black.setForeground(Color.white);
black.setBackground(Color.black);
black.addActionListener(this);
this.add(black);
}
public void actionPerformed(ActionEvent e) {
Main main = (Main)e.getSource();
if (e.getSource() == this.red)
main.setCENTER(1);
if(e.getSource() == this.green)
main.setCENTER(2);
if(e.getSource() == this.blue)
main.setCENTER(3);
if(e.getSource() == this.white)
main.setCENTER(4);
if(e.getSource() == this.black)
main.setCENTER(5);
}
}
The Top class is being used in Main and everytime each of the buttons is pressed the CENTER-area’s background of the Main class changes. Therefore I have programmed a small function setCENTER, which changes the background. the code for it looks like this (The code is in the Main class):
public void setCENTER(int var){
switch(var){
case 1: pane.setBackground(Color.red);
break;
case 2: pane.setBackground(Color.green);
break;
case 3: pane.setBackground(Color.blue);
break;
case 4: pane.setBackground(Color.white);
break;
case 5: pane.setBackground(Color.black);
break;
}
}
So far the content is displayed properly (the top panel in the Main window and the Main window itself), but everytime I try to change the background by pressing on one of the 5 buttons in the top panel, I get errors and the first error points me to this line of the actionPerformed-method in the Top class:
Main main = (Main)e.getSource();
I tried replacing it with “Main main = new Main();”. It worked, the background changes but everytime I pressed it it opened a new window (which is clearly not how it should behave).
EDIT: I forgot that Top is already a JPanel, so using another JPanel inside the class is pretty senseless. (Edited Source)
In the general case, you’ll need to follow the chain up the hierarchy, calling getParent(), until you find the parent you want. If you are looking for a specific class, through some generics magic you can write a utility to do this:
So, if you want to find the FooPanel that is an ancester of the Button that fired the event, you’d say