Hywel Griffiths
Hywel Griffiths

Reputation: 368

JFrame call is opening two JFrames

I've created a class that produces a StartUpWindow object. One of the JButton components closes the JFrame and then calls a new type of frame to be instantiated, AdminMainWindow. However, when the AdminMainWindow is instantiated two frames are opening.

Can anyone see the error in my code?

Main

public class Main {

    public static void main(String[] args) {
        StartUpWindow startUpWindow = new StartUpWindow();
    }
}

StartUpWindow

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

public class StartUpWindow extends JFrame implements ActionListener {

    private JButton admin;
    private JButton captain;
    private JButton grades;

    public StartUpWindow() {
        setTitle("Start Up Menu");
        setButtons();
        setAction();
        setSize(200, 400);
        setVisible(true);
    }

    void setButtons() {

        admin = new JButton("Admin");
        captain = new JButton("Captain");
        grades = new JButton("Grades");
        getContentPane().setLayout(new FlowLayout());
        add(admin);
        add(captain);
        add(grades);
        setAction();
    }

    void setAction() {
        admin.addActionListener(this);
        captain.addActionListener(this);
        grades.addActionListener(this);

    }

    public void actionPerformed(ActionEvent eve) {

        if (eve.getSource() == admin)
            createAdminMainWindow();
        else if (eve.getSource() == captain)
            createCaptainMainWindow();
        else if (eve.getSource() == grades)
            createGradesMainWindow();

    }

    void createAdminMainWindow() {
        this.dispose();
        AdminMainWindow adminMainWindow = new AdminMainWindow();
    }

    void createCaptainMainWindow() {
        this.dispose();
        //this.adminMainWindow = new CaptainMainWindow();
    }

    void createGradesMainWindow() {
        this.dispose();
        //this.adminMainWindow = new GradesMainWindow();
    }
}

AdminMainWindow

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

public class AdminMainWindow extends JFrame implements ActionListener {

    private JMenuItem add;
    private JMenuItem edit;
    private JMenuItem delete;
    private JMenuItem view;
    private JMenuItem viewAll;
    private JTextField jt;

    public AdminMainWindow() {
        setTitle("JMenuBar_test");
        setJMenuBarAndMenuBarItems();
        setAction();
        setJTextField();
        setSize(1400, 600);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    void setJTextField() {
        jt = new JTextField(10);
        add(jt);
    }

    void setJMenuBarAndMenuBarItems() {
        JMenuBar menuBar = new JMenuBar();
        JMenu menu1 = new JMenu("Edit");
        add = new JMenuItem("Add");
        edit = new JMenuItem("Edit");
        delete = new JMenuItem("Delete");
        JMenu menu2 = new JMenu("View");
        viewAll = new JMenuItem("View All Entries");
        view = new JMenuItem("View Entry");
        menu1.add(add);
        menu1.add(edit);
        menu1.add(delete);
        menu2.add(viewAll);
        menu2.add(view);
        menuBar.add(menu1);
        menuBar.add(menu2);
        setJMenuBar(menuBar);
    }


    void setAction() {
        add.addActionListener(this);
        edit.addActionListener(this);
        delete.addActionListener(this);
        viewAll.addActionListener(this);
        view.addActionListener(this);
    }

    JFrame setJTextField(JFrame jFrame) {
        JTextField jt = new JTextField(10);
        jFrame.add(jt);
        return jFrame;
    }

    public void actionPerformed(ActionEvent eve) {
        if (eve.getSource() == add)
            jt.setText("Add");
        else if (eve.getSource() == edit)
            jt.setText("Edit");
        else if (eve.getSource() == delete)
            jt.setText("Delete");
        else if (eve.getSource() == viewAll)
            jt.setText("ViewAll");
        else if (eve.getSource() == view)
            jt.setText("ViewOne");
    }
}

The 'Admin' button is clicked in the first window and it opens one AdminMainWindow JFrame whereas two are being produced.

Upvotes: 3

Views: 554

Answers (1)

Butiri Dan
Butiri Dan

Reputation: 1772

You have added the addActionListener event twice. Remove the setAction(); from the constructor and then your application should behave as expected.

  1. First time is added in the constructor
public StartUpWindow() {
    setTitle("Start Up Menu");
    setButtons();
-- Here -->    setAction();
    setSize(200, 400);
    setVisible(true);
}
  1. Second time is added when the buttons are created and added to the frame
void setButtons() {
    admin = new JButton("Admin");
    captain = new JButton("Captain");
    grades = new JButton("Grades");

    getContentPane().setLayout(new FlowLayout());
    add(admin);
    add(captain);
    add(grades);
-- Here -->    setAction();
}

Upvotes: 4

Related Questions