Question

I have been searching for hours and still haven't found what I need, nor any tutorials/help guides/forums to point me in the right direction.

I have two separate classes that I need to implement an action listener. I'm able to go to one class from the other, but cannot figure out a way to get back to the main class. The main class is using a CardLayout to display the second class.

Is there any good tutorials, or any one that can help/give suggestions to this problem of mine? Thanks

What I have ended up with:
Main Class -

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyGui extends JFrame {
    //CardLayout
    static CardLayout cardLayout;
    static JPanel cards = new JPanel();

    private void actionPerformed(ActionEvent evt) {
        String cmd = evt.getActionCommand();
        if(cmd.equals("HOME")) {
            cardLayout.show(cards, "Main GUI");
        }
    }

    //main Panel
    JPanel mainP = new JPanel();
    //north panel
    TitleBar tb = new TitleBar();
    JPanel top = new JPanel();
    //JLabel title = new JLabel("Title Bar", JLabel.CENTER);

    //center panel
    JButton widgetBox = new JButton("<--   Touch Screen Widget Area   -->");

    //east panel
    JPanel east = new JPanel();
    JPanel picPanel = new JPanel(new GridLayout(1,1));
    JButton pic = new JButton("Pic goes here");
    JPanel settingsPanel = new JPanel(new GridLayout(1,2));
    JButton appButton = new JButton("Apps");
    JButton settingButton = new JButton("Settings");

    //south panel
    JPanel south = new JPanel();
    JTextField rssFeed = new JTextField("RSS FEED");

    //gui panels
    MyGui2 widgPanel;
    MyGui3 appPanel;
    MyGui4 setPanel;


    public MyGui() {        
        //main layout
        super();
        BorderLayout main = new BorderLayout();
        mainP.setLayout(main);

        //gui init
        widgPanel = new MyGui2();
        appPanel = new MyGui3();
        setPanel = new MyGui4();

        //CardLayout
        final JFrame frame = new JFrame();
        JPanel contentPane = (JPanel) frame.getContentPane();
        cards.setLayout(cardLayout = new CardLayout());

        cards.add("Main GUI", mainP);
        cards.add("Settings GUI", setPanel);
        cards.add("App GUI", appPanel);
        cards.add("Widget GUI", widgPanel);
        cardLayout.show(cards, "Main GUI");
        contentPane.add(cards);

        //north panel
        BorderLayout header = new BorderLayout();
        top.setLayout(header);
        top.add(tb);
        mainP.add(top, BorderLayout.NORTH);

        //center panel
        widgetBox.setEnabled(true);

        widgetBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                Object source = evt.getSource();

                if(source == widgetBox) {
                    frame.setSize(325, 500);
                    cardLayout.show(cards, "Widget GUI");
                }
            }

        });
        mainP.add(widgetBox, BorderLayout.CENTER);

        //east panel
        BoxLayout eastPanel = new BoxLayout(east, BoxLayout.Y_AXIS);
        east.setLayout(eastPanel);
        picPanel.add(pic);
        pic.setEnabled(false);
        pic.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                rssFeed.setText("Add Picture");
            }

        }
        );
        picPanel.setPreferredSize(new Dimension(175, 150));

        //app button action
        settingsPanel.add(appButton);
        appButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                //appPane
                Object source = evt.getSource();
                if(source == appButton) {
                    cardLayout.show(cards, "App GUI");
                }
            }
        });

        //settings button action
        settingsPanel.add(settingButton);
        settingButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                //setPane
                Object source = evt.getSource();
                if(source == settingButton) {
                    cardLayout.show(cards, "Settings GUI");
                }
            }
        });

        east.add(picPanel);
        east.add(settingsPanel);
        mainP.add(east, BorderLayout.EAST);

        //south panel
        BorderLayout footer = new BorderLayout();
        south.setLayout(footer);
        south.add(rssFeed);
        rssFeed.setEditable(false);
        rssFeed.setHorizontalAlignment(JTextField.CENTER);
        mainP.add(south, BorderLayout.SOUTH);


        //setLookAndFeel();
        frame.setResizable(false);
        frame.setSize(500, 325);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setVisible(true);
    }

    private void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(
                    "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
                    );
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException exc) {
            //ignore error
        }
    }

    public static void main(String[] args) {
        MyGui gui = new MyGui();
    }
}

second class -

bottom.setLayout(footer);
        bottom.add(homeButton);
            //home button action
        homeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
            System.out.println("Go Back Home");
            }
        });
Was it helpful?

Solution

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();
    }
});

OTHER TIPS

You should make sure that Second has a reference to an instance of Main so that he can notify an event to it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top