4

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?

Ashish
  • 1,943
  • 2
  • 14
  • 17
Leonide
  • 235
  • 3
  • 11
  • You could take advantage of JXLayer as show [here](http://stackoverflow.com/questions/12982863/secure-desktop-mode-effect-for-java-application/12983564#12983564), [here](http://stackoverflow.com/questions/19799451/java-blocking-focus-from-jcomponent/19801009#19801009) and [here](http://stackoverflow.com/questions/19324918/how-to-disable-all-components-in-a-jpanel/19328368#19328368) to block the main UI and the take advantage of the either the frames glass pane or layered pane to show you `JPanel`. This will "lock" the base UI and allow you to float a component on top of it – MadProgrammer Nov 21 '13 at 05:55
  • Take a look at [How to use Root Panes](http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html) for more details... – MadProgrammer Nov 21 '13 at 05:55

2 Answers2

4

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

BilalDja
  • 1,072
  • 1
  • 9
  • 17
  • Can this sort of thing be used in internal dialog? If so, it's probably what I'm looking for. – Leonide Nov 21 '13 at 17:02
  • ofcourse that's why I write it for you. It needs a small modification to work great as a Dialog. If you want more help just ask me ^^ – BilalDja Nov 21 '13 at 17:08
  • 1
    Okay, one more question. If I use one of these as a modal dialog, does it block SwingWorker threads in the background from running? – Leonide Nov 23 '13 at 18:12
  • Can you show an example of how to call one as in internal dialog in a JDesktopPane? Would it need to be in a JOptionPane or JInternalFrame? – Leonide Nov 27 '13 at 09:05
0

Just use a JFrame and use the setUndecorated method

JFrame myFrame = new JFrame("My Frame");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)       
myFrame.setBounds(10, 10, 300, 100);
myFrame.setUndecorated(true);

Then you can add a panel to this which would hold the buttons

Ankit Rustagi
  • 5,539
  • 12
  • 39
  • 70
  • 1
    Make it a modal dialog instead – MadProgrammer Nov 21 '13 at 05:56
  • Don't use set bounds. The frame should be packed so it is sized based on the components added to the dialog. You would also probably want to use the `setLocationRelativeTo(...)` method to position the dialog. – camickr Nov 21 '13 at 06:00
  • Thanks @camickr, i always thought `setBounds` was used to specify the size of the frame and place it. Is it not the case (and why)? If not when is this function supposed to be used ? – Ankit Rustagi Nov 21 '13 at 06:06
  • It can be used, but there are better API's to use to you don't have to make up magic numbers. The frame should be packed to give the frame a proper size based on the preferred size of components added to the frame. Then in the case of a frame you would use `setLocationByPlatform()` to let the OS determine where to display the frame. In the case of a popup dialog you probably want it centered relative the the existing frame so you would use `setLocationRelativeTo(...)`. – camickr Nov 21 '13 at 06:16