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

  • SEARCH
  • Home
  • 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 7414083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:00:08+00:00 2026-05-29T07:00:08+00:00

What I planned to do is when I press Enter key, the application will

  • 0

What I planned to do is when I press Enter key, the application will fire up the button btn_teach, and switch to another tab with textfield focused. Now, when I implement it, it works perfectly if I press the button manually (not pressing Enter). When I actually press Enter, the tab switches back but the text field on that tab is not focused.

tp being JTabbedPane.
My plan is to switch from tab index 1 to tab index 0 and set txt_send focused

    public void actionPerformed(ActionEvent arg0) 
{
    // TODO Auto-generated method stub
        String say = txt_saypane.getText();
        String ans = txt_anspane.getText();
        //this.clear();

        say = say.replace("\n","");
        ans = ans.replace("\n","");

        this.talk(this.botTeach(say,ans), false);

        tp.setSelectedIndex(0);
}

    public void stateChanged(ChangeEvent arg0) 
{
    // TODO Auto-generated method stub
    int sel = tp.getSelectedIndex();
    if(sel == 0)
        txt_send.requestFocusInWindow();
    if(sel == 1)
        txt_saypane.requestFocusInWindow();
}

public void keyPressed(KeyEvent e) 
        {
            // TODO Auto-generated method stub
            if(e.getKeyCode() == KeyEvent.VK_ENTER)
            {

                if(txt_saypane.isFocusOwner() || txt_anspane.isFocusOwner())
                    btn_teach.doClick();
            }
    }

What is the correct way to change focus via KeyListener when switching between tabs in JTabbedPane?

  • 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-05-29T07:00:09+00:00Added an answer on May 29, 2026 at 7:00 am

    For JButton to work on press of the ENTER key you can make that JButton to be your DEFAULT Button on frame, instead of using KeyEvents. You can do this by writing :

    yourFrameObject.getRootPane().setDefaultButton(btn_teach);
    

    Writing this line will also make this JButton work on pressing the ENTER key, without you writing the whole KeyListener part for it. Remove the KeyListeners from this JButton. Once this JButton is the DEFAULT Button, now on Pressing the ENTER key, it will do the work that is written inside it’s actionPerformed() method.

    Don’t use KeyEvents with Swing, that belongs to awt, KeyBinding is what we use with Swing.

    Here is one sample program to help your cause :

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ButtonTest extends JFrame implements ActionListener
    {
        private JTabbedPane tabbedPane;
        private JButton focusButton;
        private JPanel contentPane, tab1, tab2;
        private JTextField textField1, textField2, textField3;
    
        public ButtonTest()
        {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
    
            tab1 = new JPanel();
            textField1 = new JTextField(10);
            textField1.requestFocusInWindow();
            tab1.add(textField1);
    
            tab2 = new JPanel();
            textField2 = new JTextField(10);
            textField3 = new JTextField(10);
            tab2.add(textField2);
            tab2.add(textField3);
    
            contentPane = new JPanel();
            contentPane.setLayout(new BorderLayout());
    
            tabbedPane = new JTabbedPane(JTabbedPane.TOP,
                                                    JTabbedPane.WRAP_TAB_LAYOUT);
            tabbedPane.addTab("TAB 1", null, tab1, "I am TAB 1");
            tabbedPane.addTab("TAB 2", null, tab2, "I am TAB 2");
    
            focusButton = new JButton("CHANGE FOCUS");
            //focusButton.addMnemonic(KeyEvent.VK_ENTER); /* You can Add this Line too
                                                          /* , to make it work. But here
                                                           * you have to press ALT + ENTER.
                                                           */
            getRootPane().setDefaultButton(focusButton);
            focusButton.addActionListener(this);
    
            contentPane.add(tabbedPane, BorderLayout.CENTER);
            contentPane.add(focusButton, BorderLayout.PAGE_END);
            setContentPane(contentPane);
            pack();
            setVisible(true);
        }
    
        public void actionPerformed(ActionEvent ae)
        {
            if (tabbedPane.getSelectedIndex() == 0)
            {
                tabbedPane.setSelectedIndex(1);
                textField3.requestFocusInWindow();
            }
            else if (tabbedPane.getSelectedIndex() == 1)
            {
                tabbedPane.setSelectedIndex(0);
                textField1.requestFocusInWindow();
            }
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new ButtonTest();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The application is planned to be built using ASP.NET, .NET Remoting & MS SQL
I've been planned to develop an Augmented_Reality(AR) application in an android. So I've gone
We've got an application planned that is a very basic 7 page presetation. Our
Edit 2: What I previously planned was probably a bad idea and I now
I just planned to create a blogging application in Java. But i am not
I have planned to write my own light weight MVC for PHP, that will
Scenario: I have a WPF desktop application which will be distributed on different machines
Does the current version or will any planned future version of SQL Server Compact
I have planned to design chat application in flex 3. i need rtmp to
I am writing a text editor and had planned on using the richtextbox control

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.