0

I have the following working code with two buttons as event listeners. One button to get and display a Panel panel1 and the other to get and display another Panel panel2 removing the existing displayed panel. I created two actionPerformed methods for each button to carry out each other's tasks. I just want to make one to shorten the code but I don't know how to detect which button in a panel is displaying at compile time. Any help will be appreciated.

//Switching between panels (screens)

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

public class ScreenDemo extends JFrame {
private JPanel panel1, panel2;
private JButton btn1, btn2;
JLabel label1 = new JLabel("Screen 1");
JLabel label2 = new JLabel("Screen 2");

public ScreenDemo() {
    createPanel(); //Created at Line 14
    addPanel(); //Created at Line 28
}

private void createPanel() {
    //Panel for first screen
    panel1 = new JPanel(new FlowLayout());
    btn1 = new JButton("Move to Screen 2");
    btn1.addActionListener(new addScreen1ButtonListener());

    //Panel for second screen
    panel2 = new JPanel(new FlowLayout());
    btn2 = new JButton("Move to Screen 1");
    btn2.addActionListener(new addScreen2ButtonListener());

}

private void addPanel() {
    panel1.add(label1);
    panel1.add(btn1);
    panel2.add(label2);
    panel2.add(btn2);

    add(panel1); //Add the first screen panel

}

class addScreen1ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent ae) {
        getContentPane().removeAll();
        getContentPane().add(panel2);
        repaint();
        printAll(getGraphics()); //Prints all content
    }
}

class addScreen2ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent ea) {
        getContentPane().removeAll();
        getContentPane().add(panel1);
        repaint();
        printAll(getGraphics()); //Prints all content
    }
}

public static void main(String[] args) {
    ScreenDemo screen = new ScreenDemo();
    screen.setTitle("Switching Screens");
    screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    screen.setSize(500, 500);
    screen.setVisible(true);
}
} 
mKorbel
  • 109,525
  • 20
  • 134
  • 319
RichardH
  • 3
  • 1
  • For many components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). Alternately, consider putting the components in a `JTabbedPane`. – Andrew Thompson Mar 07 '14 at 08:58

2 Answers2

4

implement ActionListener for your class, and try this code

       ScreenDemo extends JFrame implements ActionListener
    ...
        btn1.addActionListener(this);
        btn2.addActionListener(this);
    ...
        @override
        public void actionPerformed(ActionEvent ae) {
            if(ae.getSource().equals(btn1)) {
            ...
            }
            else if(ae.getSource().equals(btn1)) {
            ...
            }
        }
Thanh Le
  • 763
  • 4
  • 13
  • 1
    I'd recommend using `ae.getSource().equals(btn1)` instead of the `==` notation, as you are comparing objects, not primitives. – Andrew Gies Mar 07 '14 at 08:34
  • Thanks a lot @Thanh. I modified it already based on your suggestions. – RichardH Mar 07 '14 at 08:37
  • @RichardH Firstly, I think you are still making a separate class to house your `actionPerformed()` method. That works. However the code might be more readable if you make your main class, `ScreenDemo`, implement `ActionListener` in the same fashion. It allows you to use only one class. In that way, you can pass `this` into your action listener method, to look like: `myButton.addActionListener(this);`. `this` is a Java keyword that refers to the instance of the object in which you are using the keyword. Also, it usually isn't a good idea to put code in comments on SO, because it's hard to read. – Andrew Gies Mar 07 '14 at 08:42
  • @RichardH Glad I could help. If you could add your revised (and hopefully working) code in another section below your question, that would be much appreciated by other who come to SO with the same question. Thanks! (And welcome to SO) – Andrew Gies Mar 07 '14 at 08:47
0

you have to implements the class from ActionListener than register JButton like : btn.addActionListener(this);

so close now, You just need to capture the event and get the source from where it is called like:

 public void actionPerformed(ActionEvent ae) {
        if(ae.getSource == btn1){
            //shuffle panel from btn1
        }
        if(ae.getSource == btn2) {
            //shuffle panel from btn2               
        }
}
Sarz
  • 1,970
  • 4
  • 23
  • 43