3

I am trying to code a program with multiple screens, however, I do not want to use tabbed panes. I have looked at using multiple JPanels with the card layout and the methods are simply not working. What I need to be able to do is load the new JPanel when a button is clicked. Here is my code:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class IA extends JFrame {

    private JPanel contentPane;
    private JPanel home;
    private JPanel clients;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    IA frame = new IA();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public IA() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new CardLayout(0, 0));

        JPanel home = new JPanel();
        contentPane.add(home, "name_714429679706141");
        home.setLayout(null);

        JButton btnClients = new JButton("Clients");
        btnClients.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                home.setVisible(false);
                clients.setVisible(true);
            }
        });
        btnClients.setBounds(160, 108, 89, 23);
        home.add(btnClients);

        JPanel clients = new JPanel();
        contentPane.add(clients, "name_714431450350356");
        clients.setLayout(null);

        JButton btnHome = new JButton("Home");
        btnHome.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                clients.setVisible(false);
                home.setVisible(true);
            }
        });
        btnHome.setBounds(169, 107, 89, 23);
        clients.add(btnHome);
    }

}
Trey Collier
  • 129
  • 2
  • 11

3 Answers3

1

The problem is that you have duplicate variables home and clients .

The folllowing is your modified code to fix that, with comments on the changed lines (five lines total) :

import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class IA extends JFrame {

    private final JPanel contentPane;
    //   private final JPanel home;  // REMOVED
    //   private JPanel clients;  // REMOVED

    /**
     * Launch the application.
     */
    public static void main(final String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    IA frame = new IA();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public IA() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new CardLayout(0, 0));

        final JPanel home = new JPanel();
        contentPane.add(home, "name_714429679706141");
        home.setLayout(null);

        final JPanel clients = new JPanel();  // MOVED UP
        contentPane.add(clients, "name_714431450350356");  // MOVED UP
        clients.setLayout(null);  // MOVED UP

        JButton btnClients = new JButton("Clients");
        btnClients.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                home.setVisible(false);
                clients.setVisible(true);
            }
        });
        btnClients.setBounds(160, 108, 89, 23);
        home.add(btnClients);

        JButton btnHome = new JButton("Home");
        btnHome.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                clients.setVisible(false);
                home.setVisible(true);
            }
        });
        btnHome.setBounds(169, 107, 89, 23);
        clients.add(btnHome);
    }

}
Arnaud
  • 17,229
  • 3
  • 31
  • 44
0

I would take a look at this post, however I have a feeling you'll need to use a actionlistener to get this done... Java Swing. Opening a new JPanel from a JButton and making the buttons pretty I would of left this as a comment but apparently you need 50 rep for that...

This link might be more helpful.. How to open a new window by clicking a button

Sentinel
  • 98
  • 1
  • 11
0

When the following code is invoked the clients variable equals to null.

JButton btnClients = new JButton("Clients");
btnClients.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        home.setVisible(false);
        clients.setVisible(true);
    }
});

Write this:

JPanel clients = new JPanel();
contentPane.add(clients, "name_714431450350356");
clients.setLayout(null);
JButton btnHome = new JButton("Home");
btnHome.setBounds(169, 107, 89, 23);
clients.add(btnHome);

before you add the Action Listener

Andrii Bugai
  • 156
  • 4