Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9102149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:18:38+00:00 2026-06-17T01:18:38+00:00

I have a swing GUI with multiple JTabbedPane s; each tab contains two JButtons

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:18:39+00:00Added an answer on June 17, 2026 at 1:18 am

    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.]

    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class JTabbedPaneDemo3 implements Runnable
    {
      JTextField txtFoo;
      JTextField txtBar;
      JTabbedPane tabbedPane;
    
      public static void main(String[] args)
      {
        SwingUtilities.invokeLater(new JTabbedPaneDemo3());
      }
    
      public void run()
      {
        txtFoo = new JTextField(10);
        final JPanel pnlFoo = new JPanel();
        pnlFoo.add(new JButton("Button 1"));
        pnlFoo.add(new JLabel("Foo"));
        pnlFoo.add(txtFoo);
    
        txtBar = new JTextField(10);
        final JPanel pnlBar = new JPanel();
        pnlBar.add(new JButton("Button 3"));
        pnlBar.add(new JLabel("Bar"));
        pnlBar.add(txtBar);
    
        tabbedPane = new JTabbedPane();
        tabbedPane.addTab("Tab 1", pnlFoo);
        tabbedPane.addTab("Tab 2", pnlBar);
    
        tabbedPane.addChangeListener(new ChangeListener()
        {
          public void stateChanged(ChangeEvent e)
          {
            Component comp = tabbedPane.getSelectedComponent();
            if (comp.equals(pnlFoo))
            {
              txtFoo.requestFocusInWindow();
            }
            else if (comp.equals(pnlBar))
            {
              txtBar.requestFocusInWindow();
            }
          }
        });
    
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(460, 200);
        frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    
        txtFoo.requestFocusInWindow();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two applications. One is a Java thick client Swing GUI for controlling
I have a Swing GUI where I am restricting the user registration so that
i have a simple swing gui where there are two text fields one for
I have two JList on a swing GUI. Now I want that when a
I have made a java swing GUI. Now I want to display a static
I am creating one GUI in swing Java. I have to use one button
I have to setup a basic JAVA application including a GUI based on swing.
I am using swing to create my GUI. J have a JFrame containing one
I am trying to create a GUI using java swing. From there I have
I'm developing Swing application, and everything works fine usually. But I have an GUI

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.