I need to autofocus a JPanel in my main frame. How do I accomplish this?
Printable View
I need to autofocus a JPanel in my main frame. How do I accomplish this?
I suggest you check out the API for useful methods and give some of them a try to see what they do: JPanel (Java Platform SE 6)
Please define what you mean by autofocus. Do you mean for this JPanel to have focus for listeners? Call requestFocus on the component
Because at the moment I have to press the tab button to focus on my JPanel.
What happened when you tried copeg's suggestion? Did you read the links I posted?
Wow, you're a fast reader! Then I guess it's time to provide an SSCCE that demonstrates the problem and what you tried from those tutorials. Also, please read the link in my signature on asking questions the smart way, as you're making it really hard for people to help you today.
Right, lets try again. I am trying to make the key listener in my main JPanel to focus the keyboard as soon as the program is run.
I gave you two links that should help you with that, and copeg very kindly gave you a suggestion. If you want help, we need more information than "that didn't work". What exactly did you try? Did you read the tutorials? Did you follow them? What about them didn't you understand? Where's your SSCCE? What does "didn't work" even mean? Did you see an exception, some strange behavior, a crash, a leprechaun, something else?
Seriously, I urge you to read the link in my signature on asking questions the smart way. You aren't making it easy to help you, and we have hundreds of other posts we could be answering instead of guessing at what you're talking about.
It's a Short, Self-Contained, Compilable Example. That means it contains just enough code to demonstrate the problem- no extra stuff. But it's still compilable, so we can copy and paste it into our own editors and run it on our computers without doing stuff like adding a main method ourselves.
And what did you try from the tutorials? What happened when you tried copeg's suggestion? Saying "it's not what I'm looking for" when in fact, it's EXACTLY what you're looking for if you would just READ is pretty frustrating and makes it even harder to help you.
Um, no. That wouldn't help me see the code you're running, which is pretty much the point. Plus, in the process of creating an SSCCE, oftentimes you'll figure the problem out yourself.
There are too many other posts here with people who are more willing to follow suggestions, so I might be done trying to help you.
Stop trying to "find" it and just read the tutorials. Do the examples, and play with them to see what the different methods do. And without an SSCCE, I can't even tell you what to do even if I wanted to!
What you're doing right now is like this: Say I approached you and told you that my algorithm is slow and I want to improve it. You'd have to ask me what I meant by slow (seconds? minutes? hours? freezing up the program?), and more importantly, what the code for my algorithm looked like. In the meantime, maybe you would give me an article that talked about code optimization. How would you react if instead of following your suggestions or answering your questions or reading the article, I simply came back with a "no that didn't work"? What didn't work? What did you try? What did you learn from the tutorials?
If you don't start being more forthright, I'm going to assume you're just being a troll on purpose.
Right. This is pretty much my problem: java - Obtaining focus on a JPanel - Stack Overflow
and I will try some of the solutions that are in the comments on that page. If that doesnt work I will come back and bump this thread so you can see it.
You're copying and pasting partial solutions you find on the internet without trying to understand them so you can avoid reading through a simple tutorial. That's a copy-paste programmer. You were unwilling to create an SSCCE so people could advise you on your code. That's a copy-paste programmer.
And you ignored the suggestions you were given, and then argued about it for the rest of the time. That's a troll.
I dont have the time to make a SSCCE and im only copying the solutions because I am relatively new to swing. And I learn from using those solutions on a daily basis. Thats how I learn, everyone learns differently.
Close this thread, even google is more helpful than you are. And I didn't mean to offend you. Just close this thread.
I'm not offended, I'm just pointing out that your approach makes it difficult to help you. It's not a ridiculous request to ask you to provide some example code that demonstrates what you're talking about. And it's not ridiculous to point you to a tutorial that contains the answer you're looking for. Learning how to program takes a lot of time and patience, so if you don't have the time to even narrow your problem down to some code for a stranger to look at for free, then I wish you the best of luck. You might very well find a copy-paste solution to use to solve this basic problem, but I think your approach is going to come back to bite you in the future when you realize all these shortcuts kept you from actually learning anything.