You can create a method in MyGui
that will direct the entire Application to the Home
:
public static void showHome()
{
cardLayout.show(cards, "Main GUI");
}
And within SecondClass
use this method in actionPerformed
as follows:
homeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
MyGui.showHome();
}
});
EDIT
In above code, there is still a bit of coupling between two classes.
There is another way out to achieve the same task and to eradicate the coupling issue:
Within MyGui
while you are creating the SecondClass
object pass the reference of MyGui
current object to it as follows within constructor as follows:
public MyGui() {
...
widgPanel = new MyGui2(this);
....
}
Create non-static method showHome
within MyGui
:
public void showHome()
{
cardLayout.show(cards, "Main GUI");
}
And change the MyGui2
class as follows:
class MyGui2{
MyGui mg;//create an instance variable of MyGui
MyGui2(MyGui mg)
{
this.mg = mg;
//Do all other stuffs here.
}
And within actionPerformed
you can can proceed as follows:
homeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
mg.showHome();
}
});