Sailanarmo
Sailanarmo

Reputation: 1181

How to use JDialog inside a JFrame from another Package?

I'm trying to get a Message Box that explains the rules of my game to populate from a menu bar. I can get it to populate correctly when I use a JFrame, however, when I click Ok or the X, it still stays open outside of my game. And I have to close it again in order to use the game again. I read that I don't want to use JFrame that I would need to use JDialog but I am having trouble with it.

I'm trying to figure out how to use a JDialog and get it centered inside a JFrame from another package. I don't think I'm using JDialog correctly as I cannot get it to populate in the first place. My main game runs from a different package called simonish, and has it's own class called MainGame. And in MainGame there is a private variable JFrame frame.

public class MainGame implements MouseListener, ActionListener{
    private JFrame frame = new JFrame("Simonish");
    private MenuBar menuBar = new MenuBar();
}

Of course, this isn't all the code but this is inside the package Simonish.

Now I have another package called Component, and inside I have a class called MenuBar: public class MenuBar extends JMenuBar implements MenuListener, ActionListener{

JMenuBar menuBar;
JMenu settings, stats, help;
JMenuItem chooseColor, chooseMode, highScoreList, history, about, rules;

public MenuBar() {

    //adding help to the menu bar
    help = new JMenu("Help");
    help.addMenuListener(this);
    this.add(help);

    //sub categories for help
    about = new JMenuItem("About");
    about.addActionListener(this);
    help.add(about);

    rules = new JMenuItem("Rules");
    rules.addActionListener(this);
    help.add(rules);


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(about)){
            new PopupMenu();
        }
        if(e.getSource().equals(rules)){
            new RulesPopup();
        }
    }
}

I know, a lot of missing stuff, but trying to keep it short.

And in the same Component folder, I have a class called PopUpMenu, it's a terrible name that I will call PopUpAbout, but here is the code.

public class PopupMenu {

    JFrame Popup;

    public PopupMenu(){
        Popup = new JFrame();
        Popup.pack();
        Popup.setVisible(true);


        JOptionPane.showMessageDialog(Popup, "Version 2\n\n" + "Added new features: \n\n" + "Color Customization\n" + "Different Game Modes\n" +
            "Menu Bar\n" + "Images\n" + "Sound Effects\n" + "History of Score", "About", JOptionPane.PLAIN_MESSAGE);
    }
}

So there lies my problem, I was trying to mess around in the class PopUpMenu, but I can't seem to get a JDialog to work at all. I have tried changing those to JDialog, and changing the arguments inside, but I can't get it to populate, or get it to populate inside the main game.

Does it need to be called inside the MainGame? Or does it need to be called in PopUpMenu?

Upvotes: 0

Views: 1256

Answers (2)

rdonuk
rdonuk

Reputation: 3956

You can add/remove components to JDialogs as in JFrames. Look example below, you can change the design as you want.

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class JDialogDemo extends JFrame {

    public JDialogDemo() {

        final MyDialog dialog = new MyDialog(this);

        JButton button = new JButton("Show Dialog");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dialog.pack();
                dialog.setLocationRelativeTo(JDialogDemo.this);

                dialog.setVisible(true);
            }
        });

        setLayout(new FlowLayout());
        add(button);

        setSize(400, 400);
        setVisible(true);
    }

    public static void main(String[] args) {
        new JDialogDemo();
    }
}

class MyDialog extends JDialog {
    public MyDialog(Frame owner) {
        super(owner, true);

        setTitle("About");

        add(new JLabel("<html>Version 2<br><br>" + "Added new features: <br><br>" + "Color Customization<br>"
                + "Different Game Modes<br>" + "Menu Bar<br>" + "Images<br>" + "Sound Effects<br>"
                + "History of Score"));

        JButton okButton = new JButton("Ok");
        okButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });

        add(okButton, BorderLayout.SOUTH);
    }

}

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347184

The problem is, you're creating a second JFrame, which you're not disposing when you're done with it...

public class PopupMenu {

    JFrame Popup;

    public PopupMenu(){
        Popup = new JFrame();
        Popup.pack();
        Popup.setVisible(true);


        JOptionPane.showMessageDialog(Popup, "Version 2\n\n" + "Added new features: \n\n" + "Color Customization\n" + "Different Game Modes\n" +
            "Menu Bar\n" + "Images\n" + "Sound Effects\n" + "History of Score", "About", JOptionPane.PLAIN_MESSAGE);
    }
}

JOptionPane doesn't need a Component reference to work (although it can help), so you don't actually need the JFrame

public class PopupMenu {
    public PopupMenu(){
        JOptionPane.showMessageDialog(null, "Version 2\n\n" + "Added new features: \n\n" + "Color Customization\n" + "Different Game Modes\n" +
            "Menu Bar\n" + "Images\n" + "Sound Effects\n" + "History of Score", "About", JOptionPane.PLAIN_MESSAGE);
    }
}

would solve your general problem.

You could also pass a reference to some parent component, which is displayed on your parent window

public class PopupMenu {
    public PopupMenu(JComponent parent){
        JOptionPane.showMessageDialog(parent, "Version 2\n\n" + "Added new features: \n\n" + "Color Customization\n" + "Different Game Modes\n" +
            "Menu Bar\n" + "Images\n" + "Sound Effects\n" + "History of Score", "About", JOptionPane.PLAIN_MESSAGE);
    }
}

which you could then call using something like...

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource().equals(about)){
        new PopupMenu(this);
    }
    if(e.getSource().equals(rules)){
        new RulesPopup();
    }
}

Upvotes: 1

Related Questions