I have a swing GUI with multiple JTabbedPanes; each tab contains two JButtons at the top, then a JTextArea (for user input), and a JTextField at the bottom for a result.
My problem is that I can’t get the JTextArea to gain focus after switching tabs without either clicking in it with the mouse or using the tab key on the keyboard?
I have…
frame.addWindowFocusListener(new WindowAdapter() {
public void windowGainedFocus(WindowEvent e) {
textArea_1.requestFocusInWindow();
…and this works well when the app is first run (the textArea in the first tab has focus) but when I switch to another tabbedpane the first button now has the focus instead of the textArea, and when I switch back to the first tab the textArea has lost focus and once again the first button has focus.
I’ve tried adding a requestFocus to each textArea, and I’ve tried “Bring to front” on each textArea, and I’ve messed around with Focus Traversal but nothing I do seems to make the textArea gain focus on a tab change?
This has had me stumped for a week so any help will be gratefully received?
Add a ChangeListener to your JTabbedPane. Here’s the general idea:
[Sorry, I used JTextFields instead of JTextAreas since I had an old stub laying around – but the idea is the same.]