Search code examples
javaswingjoptionpane

JOptionPane Internal Dialog without title bar or close button?


I'm working on a game as a practice project. When it's first loaded, the user is prompted to login or create a new player. If the user selects to create a new player, what I want to happen is for the new-player-creation form to be displayed (the main game board is a JDesktopPane, and ideally, it would be in a JPanel on the top layer of it), and everything else pause until the user fills out and submits this form.

Since other user input tasks within the game are done with JOptionPane.showInternalOptionDialog, I figured this might do the job, as I'm looking for a JPanel that acts like a modal dialog. However, I want something that doesn't have a title bar or close button, just OK and Cancel buttons. Is there a way to show an internal dialog without these? Or some other way to have a JPanel act like a modal dialog?


Solution

  • You can use the JDialog Class

    This is an example, try to inspire from it :

    import java.awt.FlowLayout;
    
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class BDialog extends JDialog {
    
        private JButton okBtn = new JButton("OK");
        private JButton cancelBtn = new JButton("Cancel");
        private JLabel messageLbl = new JLabel("This is a message");
    
        public BDialog() {
            super(new JFrame(), true);
            setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
            setUndecorated(true);
            setLayout(new FlowLayout(FlowLayout.CENTER));
            add(messageLbl);
            add(okBtn);
            add(cancelBtn);
            pack();
            setLocationRelativeTo(null);
            setVisible(true);
        }
    
        public static void main(String[] args) {
            new BDialog();
        }
    
    }
    

    if that helps you make it solved please