user3202098
user3202098

Reputation: 33

Running the GUI from another class

I created my GUI in a class "ClientGUI.java" and tried to create an instance in another class ("Client.java"). The GUI simply consists of two buttons ("Forward" and "Backward") of which only one button is present at a time. At first the GUI seems to work fine as the frame is displayed with one button. However once the button is pushed it just disappears and is not replaced by the second one. By setting breakpoints I found out that the correct ActionListener function is called and the first button removed but not the second one added.

The GUI class "ClientGUI.java":

package GUI;

import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JButton;
import java.net.MalformedURLException;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ClientGUI extends JPanel implements ActionListener {

    private static JButton btnForward = new JButton("Forward"),
            btnBackward = new JButton("Backward");

    public static void main(String[] args) {        
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    createAndShowGUI();
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }

    public ClientGUI() throws MalformedURLException {
        setLayout(new BorderLayout());

        add(btnForward, BorderLayout.CENTER);

        btnForward.addActionListener(this);
        btnBackward.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {        
        if (e.getSource() == btnForward) {
            remove(btnForward);

            add(btnBackward, BorderLayout.CENTER);

            revalidate();
            repaint();
        }

        else if (e.getSource() == btnBackward) {
            remove(btnBackward);

            add(btnForward);

            revalidate();
            repaint();
        }
    }

    private static void createAndShowGUI() throws MalformedURLException {
        JFrame frame = new JFrame("ClientGUI");

        frame.setMinimumSize(new Dimension(500, 400));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new ClientGUI());

        frame.setSize(500, 400);
        frame.setVisible(true);
    }
}

And the class "Clients.java" from which i want to use the GUI:

import java.net.*;
import GUI.ClientGUI;

public class Client {

    public static void main(String[] args) {
        Client client = new Client();
    }

    Client() {
        String[] args = {"ggg", "vvv"};

            try {
                new ClientGUI().main(args);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
}

Thanks for your help.

Upvotes: 1

Views: 6540

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208944

  1. Don't ever call the main method. The main method is for the JVM to know the entry point for the program.
  2. Don't use two main methods. Whichever is the entry point program, have the main method only in that one
  3. Just use a constructor. When the first GUI calls the second one, everything in the constructor will be constructed.

Here's a revamped version. It works.

  • I got rid of the main from ClientGUI
  • I called ClientGUI.createAndShowGUI() from the main in the Client class.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Client {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Client();
            }
        });
    }

    Client() {
        String[] args = {"ggg", "vvv"};

        try {
            ClientGUI.createAndShowGUI();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

class ClientGUI extends JPanel implements ActionListener {

    private static JButton btnForward = new JButton("Forward"),
            btnBackward = new JButton("Backward");



    public ClientGUI() throws MalformedURLException {
        setLayout(new BorderLayout());

        add(btnForward, BorderLayout.CENTER);

        btnForward.addActionListener(this);
        btnBackward.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnForward) {
            remove(btnForward);

            add(btnBackward, BorderLayout.CENTER);

            revalidate();
            repaint();
        } else if (e.getSource() == btnBackward) {
            remove(btnBackward);

            add(btnForward);

            revalidate();
            repaint();
        }
    }

    public static void createAndShowGUI() throws MalformedURLException {
        JFrame frame = new JFrame("ClientGUI");

        frame.setMinimumSize(new Dimension(500, 400));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new ClientGUI());

        frame.setSize(500, 400);
        frame.setVisible(true);
    }
}

Upvotes: 5

Related Questions