How can I customize a jDialog

Hi,

I have implemented a silly game. To display who is the winner I have tried JOptionPane but the program freezes instead. Thus I thought to customize my own dialog so the user, togheter with the message, could also choose whether run the game again or exit. Could anyone post a link where I could look up the above issue, please?

Regards

[358 byte] By [MrBrillianta] at [2007-11-26 20:41:46]
# 1
I'd say it would be better for you to find out why it freezes.building your own dialog may not change anything, but if you must... http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html
Michael_Dunna at 2007-7-10 2:00:40 > top of Java-index,Desktop,Core GUI APIs...
# 2
Well using JOptionPane the program will be limited to display just a message to the user. I personally think that the user should be able to decide whether to close the application or not anytime he wants to. What's your opinion about that?Ragards
MrBrillianta at 2007-7-10 2:00:40 > top of Java-index,Desktop,Core GUI APIs...
# 3
there's more than JOptionPane.showMessageDialog(...)sounds likeJOptionPane.showOptionDialog(..) is what you're after
Michael_Dunna at 2007-7-10 2:00:40 > top of Java-index,Desktop,Core GUI APIs...
# 4

here's an example of a common usage - confirm when exiting an app

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Testing

{

public void buildGUI()

{

JFrame f = new JFrame();

f.setSize(400,300);

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

f.setVisible(true);

f.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent we){

String[] options = {"Yes","No","Not Sure"};

int selection = JOptionPane.showOptionDialog(null,"Exit Application?",

"",-1,-1,null,options,"Yes");

if(selection == 0) System.exit(0);

}

});

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

Michael_Dunna at 2007-7-10 2:00:40 > top of Java-index,Desktop,Core GUI APIs...
# 5

Not quite. I was thinking to have a JDialog (triggered by the counter variable) which deliveries a message like

Player 1 WIN! Do you want to play again?

and the two button:

Yeswhich will restart the application

Nowhich will close the application

Regards

MrBrillianta at 2007-7-10 2:00:41 > top of Java-index,Desktop,Core GUI APIs...
# 6

> Not quite.

the principle is the same.

something triggers the display of a JOptionPane.

as you just want YES/NO, a JOptionPane.showConfirmDialog() with YES_NO_OPTION

is what you'd use.

then it's just a matter of checking the return value of the optionPane

if(selection == JOptionPane.YES_OPTION) //reset/restart game

else System.exit(0);

Michael_Dunna at 2007-7-10 2:00:41 > top of Java-index,Desktop,Core GUI APIs...
# 7
To restart the game do I have to userepaint();?
MrBrillianta at 2007-7-10 2:00:41 > top of Java-index,Desktop,Core GUI APIs...
# 8

perhaps the easiest way is to put all of the stuff when you first start the game into

its own method, say reset(), which is called after the gui is constructed

e.g.

sets all variables to their initial state/value

clears any text in textComponents

clears any drawing areas

etc

etc

now, if 'new game' is selected, you just call the reset() method, and you're away

Michael_Dunna at 2007-7-10 2:00:41 > top of Java-index,Desktop,Core GUI APIs...
# 9

cool! it works as it should!!!

Here is the code:

Object[] options = {"Yes","No"};

int select = javax.swing.JOptionPane.showOptionDialog(this, str + " Do you wish to play again?", "Yum!", -1, -1, new javax.swing.ImageIcon(getClass().getResource("/images/Icon.gif")), options, "Yes");

if(select == javax.swing.JOptionPane.YES_OPTION)

reset();

else System.exit(0);

Would you be so kind to explain me what "-1s" are there for?

Regards

MrBrillianta at 2007-7-10 2:00:41 > top of Java-index,Desktop,Core GUI APIs...
# 10

refer api docs for detailed explanation

[url]

http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JOptionPane.html#showOptionDialog

(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)

[/url]

for just yes/no, you are better off using showConfirmDialog().

one problem you may encounter with your current code is the comparison to YES_OPTION.

the return value from showOptionDialog is the button number associated with

its index in the array. it works in the example because YES_OPTION is 0,

and the option "yes" is at 0. Make your array like this (swap them)

Object[] options = {"No","Yes"};

now press 'Yes' and see if you get the same result.

in a confirmDialog, the 'Yes' button will always return YES_OPTION.

Michael_Dunna at 2007-7-10 2:00:41 > top of Java-index,Desktop,Core GUI APIs...
# 11
I don't get it Michael. Could you please write the code?
MrBrillianta at 2007-7-10 2:00:41 > top of Java-index,Desktop,Core GUI APIs...
# 12

use the code in reply #4, and change windowClosing() to this

public void windowClosing(WindowEvent we){

int selection = JOptionPane.showConfirmDialog(

null,"Exit Application?","",JOptionPane.YES_NO_OPTION);

if(selection == JOptionPane.YES_OPTION) System.exit(0);

}

Michael_Dunna at 2007-7-10 2:00:41 > top of Java-index,Desktop,Core GUI APIs...
# 13
I see. But this way I won't be able to add an icon. I have to stick with the standard JOptionPane dialog layout
MrBrillianta at 2007-7-10 2:00:41 > top of Java-index,Desktop,Core GUI APIs...
# 14
you can add an icon - look at the api docs for the overloaded showConfirmDialogs,one takes an icon as an argument, but this might not be positioned where you want,in that case you would have to build-your-own
Michael_Dunna at 2007-7-10 2:00:41 > top of Java-index,Desktop,Core GUI APIs...
# 15
Thanks a lot Michael!!!
MrBrillianta at 2007-7-21 18:01:13 > top of Java-index,Desktop,Core GUI APIs...