Jennifer83
Jennifer83

Reputation: 5

How to use an ActionListener to make a JPanel Visible

I tried to make the first JPanel disappear and the second JPanel visible with the click of a JButton. So far i only get the first JPanel to show and after clicking the JButton the Frame gets empty. I also tried to do it with composition so i dont have to extend classes. So my bad understanding of how composition works might be the problem. I looked into it alot but couldnt find a proper solution for my problem.

First JPanel class:

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;

public class Panel1 {

JPanel firstscreenpanel = new JPanel();
JButton jButton1 = new JButton();
    
Panel1() {
    
    jButton1.setBounds(300,300,400,200);
    jButton1.setBackground(Color.BLACK);
    jButton1.setVisible(true);
    jButton1.addActionListener(new ActionListener() {

    Panel2 test = new Panel2();
            
    public void actionPerformed(ActionEvent e) {
            
        
            firstscreenpanel.setVisible(false); 
            test.secondscreenpanel.setVisible(true);
            
            
            }
    });
    
    
}

public Component panelone() {
    
    firstscreenpanel.setSize(1280, 1024);
    firstscreenpanel.setLayout(null);
    firstscreenpanel.setBackground(Color.BLUE);
    firstscreenpanel.add(jButton1);
    firstscreenpanel.setVisible(true);
    return firstscreenpanel;
            
    }

}

Second JPanel class:

import java.awt.Color;
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JPanel;

public class Panel2 {

public JPanel secondscreenpanel = new JPanel();
public JButton jButton2 = new JButton();



Panel2() {
    
    jButton2.setBounds(100,100,400,200);
    jButton2.setBackground(Color.BLACK);
    jButton2.setVisible(true);
            
}
    
public Component paneltwo() {
    
    secondscreenpanel.setSize(1280, 1024);
    secondscreenpanel.setLayout(null);
    secondscreenpanel.add(jButton2);
    secondscreenpanel.setBackground(Color.RED);
    secondscreenpanel.setVisible(false);
    return secondscreenpanel;
            
    }
    }

JFrame Class:

import javax.swing.JFrame;

public class Frame1 {


public JFrame frame1 = new JFrame();
Panel1 panel1 = new Panel1();
Panel2 panel2 = new Panel2();

Frame1() {
    
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setState(JFrame.MAXIMIZED_BOTH);
    frame1.setSize(1280, 1024);
    frame1.setLayout(null);
    frame1.add(panel1.panelone());
    frame1.add(panel2.paneltwo());
    frame1.setVisible(true);
    
            
    }
    }

Main Class:

    public class MainClass {

        
    private void showGUI() {

    Frame1 jframe = new Frame1();
                        
    }
    
    public static void main(String[] args) {
        
        final MainClass main = new MainClass();
        
        javax.swing.SwingUtilities.invokeLater(new Runnable()

          {
            public void run() {
                main.showGUI();

            }
        
         });
        
      }

        }

Upvotes: 0

Views: 874

Answers (4)

isaace
isaace

Reputation: 3429

You can fix your program by passing a reference of your frame to your panel1. See my example below.

Frame class

import javax.swing.*;

public class Frame1 {

    private JFrame frame = new JFrame();
    private Panel1 panel1;
    private Panel2 panel2;

    Frame1() {
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setState(JFrame.MAXIMIZED_BOTH);
         frame.setSize(1280, 1024);
         frame.setLayout(null);
         panel1 = new Panel1(this);
         frame.add(panel1.getPanel());
         panel2 = new Panel2();
         frame.add(panel2.getPanel());
         frame.setVisible(true);
    }

    public Panel2 getPanel2() {
        return panel2;
    }

}

Panel1 class

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

public class Panel1 {
    private JPanel panel = new JPanel();
    private Frame1 frame1;
    private JButton jButton1 = new JButton();

    public Panel1(Frame1 frame1) {
        this.frame1 = frame1;
        panel.setSize(1280, 1024);
        panel.setLayout(null);
        panel.setBackground(Color.BLUE);
        panel.add(jButton1);
        panel.setVisible(true);
        jButton1.setBounds(300,300,400,200);
        jButton1.setBackground(Color.BLACK);
        jButton1.setVisible(true);
        jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                panel.setVisible(false);
                frame1.getPanel2().getPanel().setVisible(true);
            }
        });
    }

    public JPanel getPanel() {
         return panel;
    }
}

Panel2 class

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

public class Panel2 {
     private JButton jButton2 = new JButton();
     private JPanel panel = new JPanel();

    public Panel2() {
        panel.setSize(1280, 1024);
        panel.setLayout(null);
        panel.add(jButton2);
        panel.setBackground(Color.RED);
        panel.setVisible(false);

        jButton2.setBounds(100,100,400,200);
        jButton2.setBackground(Color.BLACK);
        jButton2.setVisible(true);
    }

    public JPanel getPanel() {
        return panel;
    }
}

Upvotes: 0

hfontanez
hfontanez

Reputation: 6188

This is not the best implementation, but it is simple enough for you to follow. I modified your code to create a frame containing your original two panels (although those panel classes are not necessary - as I explained in a comment on your posted solution), and a button to toggle visibility on the panels. I am using a regular JButton and not a JToggleButton also not the best use of the class, but simply for you to understand.

The Action Listener is added to the button on the frame. Notice that my action listener does not create new instances of anything. That was part of the original problem. Since the button is a member of the frame class like the panels 1 and 2, it has access to them directly. SO, in the listener, all I need to do is "toggle" the visibility of each of the panels.

public class Frame1 extends JFrame {
    private Panel1 panel1 = new Panel1();
    private Panel2 panel2 = new Panel2();
    private JPanel btnPanel = new JPanel();
    private JButton button = new JButton("Toggle");

    public Frame1() {
    
        button.addActionListener(new ActionListener() {
        
            @Override
            public void actionPerformed(ActionEvent arg0) {
                boolean visible = panel1.isVisible();
                panel1.setVisible(!visible);
                panel2.setVisible(visible);
            
            }
        });

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setState(JFrame.MAXIMIZED_BOTH);
        setSize(1280, 1024);
        btnPanel.setSize(400, 100);
        btnPanel.add(button);
        setLayout(null);
        add(panel1);
        add(panel2);
        add(btnPanel);
    }
}

public class Panel1 extends JPanel {

    public Panel1() {
        setBounds(100,100,400,200);
        setBackground(Color.RED);
        setVisible(true);
    }
}

public class Panel2 extends JPanel {

    public Panel2() {
        setBounds(100,100,400,200);
        setBackground(Color.BLACK);
        setVisible(false);
    }
}

public class MainClass {

    private void showGUI() {

        Frame1 jframe = new Frame1();
        jframe.setVisible(true);
    }

    public static void main(String[] args) {

        final MainClass main = new MainClass();
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                main.showGUI();

            }

        });
    }
}

Upvotes: 1

Jennifer83
Jennifer83

Reputation: 5

I solved my problem by adding "public static" to "Panel2 panel2 = new Panel2();"

And then i just used:

"Frame1.panel2.secondscreenpanel.setVisible(true);"

inside the JButton ActionListener.

Its now working but i guess thats a bad way of doing it. Because i heard that using to much static isnt that good. But i dont now why yet.

Upvotes: 0

user14971157
user14971157

Reputation:

I did not check the whole code (too bug, too many empty lines) but stopped at Panel2 test = new Panel2();

is this instance being added to some visible component? if not it will never be displayed.

Note: using a null layout manager is often not recommended, use a CardLayout or even a JTabbedPane to switch components - see tutorial A Visual Guide to Layout Managers

Upvotes: 1

Related Questions