How to use JDialog inside a JFrame from another Package? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to use JDialog inside a JFrame from another Package? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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 2nn" + "Added new features: nn" + "Color Customizationn" + "Different Game Modesn" +
            "Menu Barn" + "Imagesn" + "Sound Effectsn" + "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?

Answer

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);
    }

}
We are here to answer your question about How to use JDialog inside a JFrame from another Package? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji