I have to click my window before interacting with my game - java

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

Related

Java: Close Specific JFrame Window

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.

Pass custom code to JOptionsPane (X)

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, ...);

Simulate keyboard press in different window

I need to simulate keyboard button press in different window (media player to be specific).
I have the window name.
Is there a way to do that?
I'm sorry that i don't have anything to work with but i've been searching for anwswer for ages and still found nothing.
Try using the Robot class.
An example is here to find.

JOptionPane vs. JDialog

This is a crosspost to the thread in Javaranch (includes some images): http://www.coderanch.com/t/567472/GUI/java/Optimal-solution-creating-multiple-dialog
I'm trying to develop a simple swing desktop application where I imagine alot of different dialog's jumping around to fetch user input. Would need to present labels, textfields, passwordfields, combobxes, checkboxes etc in various dialog windows.
For example: creating the database firsthand, creating the first admin account, adding users, changing user accounts etc.
I have an understanding that JOptionPane is used to create simple quick & easy modal dialog's. I would really like to know why one would choose one over another in this case. Which one is more preferable to use: JOptionPane vs. JDialog
Also I could use some pointers how one should appropriately design and implement this.
Thank you.
Here's a statement I found on the Java website that says one key point about the difference between the two.
How to make Dialogs
A Dialog can be modal. When a modal Dialog is visible, it blocks user input to all other windows in the program. JOptionPane creates JDialogs that are modal. To create a non-modal Dialog, you must use the JDialog class directly.
So it sounds like you would use JOptionPane if you want a user to have to make a choice and close the box before returning to the main screen. If you use a JDialog box, then they can just click around it and get back to the main screen without making a choice. For example, say you wanted to make a user choose the number of results before clicking submit, you wouldn't want them to be able to click around that window and click submit. You would use JOptionPane to force them to select a value first before going back to submit.
Check out http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html it pretty much has everything you would need.
As i understand it, JOptionPane is great for what it can do, but you can't really change the functionality beyond that (not easily). JDialog is better to inherit from if you want to create your own custom Dialogs.

JAVA-I don't want clicking on my app make other apps lose focus

Wow, what a stupid question you might say. But is it possible? I have a mouse move event in a Jpanel and it works even when the application is not in focus, now can I have something like that for the clicking event or something similar. And most importantly I don't want other apps (even something like the main menu) to lose focus when i click on my app.
I thought of the system's event queue but i'm not sure where that would lead me to.
Thanks in advance.
[EDIT - the purpose]
I want to create an app that mimics the users interactions with the system in a later time. for example a user takes the mouse and clicks and writes and my app will mimic that in say 2 hours time. ofcourse i would need a system hook for the outer events but i wanted to avoid os-dependant code so i basically capture the screen, take it to my app,for example the user clicks on an icon in the captured picture and then for making that come to life, i translate the coordinates to the real icon and click it (with a Robot) and in this way i can capture the user's events in my own app. the problem occurs when the user clicks on the main menu or right clicks (he's doing that in my app, and my app does that to the system so my app is in between) and ofcourse the real main menu will lose focus when the user tries to click on one of it's items.
sorry for my english.
I'm still not sure I follow what you are trying to do. But the concept of an app getting focus when you click on it is fundamental to the GUI and I suspect rather difficult to get around.
I just found this:
Focusable Windows
To support palette windows and input methods, client code can prevent a Window from becoming the focused Window. By transitivity, this prevents the Window or any of its descendants from becoming the focus owner. Non-focusable Windows may still own Windows that are focusable. By default, every Frame and Dialog is focusable. Every Window which is not a Frame or Dialog, but whose nearest owning Frame or Dialog is showing on the screen, and which has at least one Component in its focus traversal cycle, is also focusable by default. To make a Window non-focusable, use Window.setFocusableWindowState(false).
In this doucment http://java.sun.com/j2se/1.5.0/docs/api/java/awt/doc-files/FocusSpec.html
That sounds like it might do what you want.

Categories