I asked a similar question earlier in the week but now the problem is back in my face and I really need to get a solution so hopefully someone can help me.
My question is pretty simple. If I have a JOptionPane can I pass custom directions to the (X) in the top corner? I currently have custom instruction passed if the user presses "Cancel", and "Ok" by default will close the window. I want pressing the (X) to execute System.exit(0).
My program is basically a long chain of JOptionPane's. I am aware creating a custom JFrame and full GUI would have been the better way to go but at the time I didn't expect the project to amount to much so it began with JOptionPane's and that is where I am at currently.
I find it rather annoying that the "X" button is treated the same as "Ok" (Closes the window). I don't think it's possible for me to set custom instruction but if so how do I do it?
Do I need to set a custom if statement for "Ok" to close the window and then say "else" program closes? That way if the user clicks anything other than "ok" or "cancel" the program quits (I am assuming that the "X" would be the only other option).
JFrame myFrame = new JFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myframe.setTitle("");
JOptionPane.show...Dialog(myFrame, ...);
Related
I'm making a somewhat simple text game for my cs course. The majority of the game is played out in the console window, however, at a certain if the user wins or loses the game, a JOptionPane message dialog window shows up and says that the user has won or lost
My problem is that the JOptionPane is showing up on the layer the furthest back of all my other windows and applications open. In all my other programs like this, it has always appeared in the front.
I've found a temporary fix for this however I was wondering if anyone could give me a definitive reason as to why this may be happening to avoid the problem in the future
I'm not exactly sure why exactly this happens but for anyone who finds this in the future, this is why I was having this problem.
In my program, I had certain sections that used input from the console window (so scanners) and then displayed JOptionPanes based off of certain requirements of the game (i.e winning losing, etc).
For whatever reason, whenever you are taking user input from the scanner class then opening a JOptionPane, the JOptionPane will always open in the furthest section back from all your open tabs which is what was happening to me.
As a workaround for this bug, I did the following:
Declare a JDialog and setAlwaysOnTop to true:
final JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);
Every time I made a JOptionPane open, instead of writing null as the first parameter I put the name of the JDialog so it looked something like this:
final JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(dialog, "You win!);
With this, my JOptionPanes always popped up in front of the console even when using the scanner class.
I hope everyone is doing well.
I've built a hangman game with a swing gui and everything works well enough, HOWEVER I am trying to make a popup show up by constructing a new JFrame object when the user wins or loses with a "you lose" message or what have you. No problem, but I want a specific window to close when activating the button listener on the popup, or when the 'x' is clicked. Assume my program has 3 windows up, and I only want to close 2 of them with one click.
I tried stuff in the area of
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
But that specific one closes all the windows. If you want to see more specific sample code, I would happy to provide it, but it didn't seem necessary for this question.
Either way, I can't figure out how to do this. Is this possible using Swing?
Thank you so much in advance. You guys are always so helpful.
I have written an application in Java that has a JFrame with options. I now want a certain action to be executed when the user has confirmed the dialog with "Ok". I was recommended to add a return value to JFrame.
Unfortunately, I don't have any experience and need some help. Here are the details.
I want to extend JFrame so that I can have an Enum "DialogResult" when closing a JFrame like in the .Net Framework. Well, the Enum is no problem. My problem is to replicate the ShowDialog method from WinForms of the .Net Framework working in Java for the class JFrame.
Below is an example code in C#:
// DlgOptions : Form
DlgOptions dlg = new DlgOptions();
if(dlg.ShowDialog() == DialogResult.Ok)
{
// do something only when "Ok" was clicked
}
Here's a link from MSDN with the behavior I want to replicate:
https://msdn.microsoft.com/de-de/library/c7ykbedk(v=vs.110).aspx
How can I best implement this? Thanks in advance.
EDIT:
I changed my DlgOptions class so it doesn't extend JFrame but JDialog. I also added an enumeration DialogResult and added a public property of this type in my DlgOptions. But I still have a problem. When I use this code:
// executed when user clicked a JMenuItem in a JMenuBar
DlgOptions dlg = new DlgOptions();
dlg.setModal(true);
dlg.setVisible(true);
if(dlg.DialogResult == DialogResult.OK)
{
// do something
}
the program continues running before the user closed the modal dialog. What can I do?
EDIT 2:
My JDialog contains two JButtons; one to confirm changes that were made and one to abort changing the preferences for the program. I have several JCheckBoxes the user can check or uncheck.
So a JOptionPane would not be what I want / need (as far as I know). That's why I need a modal JDialog. But my Java code above doesn't work as I want it to. I read on a German website of a Java book that a JDialog set to modal with
setModal(true);
would cause the program to wait until the dialog is closed. The problem is, that the code continues too early.
I have a simple game and when I run the program I have to click on the window before the game will accept user input.
When I play games like The Binding of Isaac they accept user input on the main menu without me ever clicking them.
Is there a way to set the focus of my keyboard to my game without clicking it first? There was another question on this: Have to click before pressing key , but it was left unanswered.
Call window.requestFocus(); after calling main.start() so you override any other focus request done in the meanwhile
If you have JFrame, or something like this (something inherited from java.awt.Component), you can try:
window.requestFocus();
Link to javadoc
EDIT:
In case of JFrame, I have found this question:
How to focus a JFrame?
One of the answer is the same as I advice
This can help you
I am currently working on a Java application using a JFrame. What I want, is to have it when someone tries to close the window, it will make a JOptionPane appear asking: "Are you sure you want to exit 3DWorld?" and then have two buttons at the bottom saying Yes and No. Any help would be great!
Thanks.
See Closing an Application for some basic information and some helper classes.