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 8122535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:52:03+00:00 2026-06-06T05:52:03+00:00

I’m using a JTabbedPane and want to do some field validation at the moment

  • 0

I’m using a JTabbedPane and want to do some field validation at the moment the user selects another Tab. Potentially, depending on their reply to a messagebox, keep them on the same tab to correct bad data.
I’m implementing the ChangeListener interface and handling stateChanged events however getSelectedIndex() returns the newly selected tab.
Is there easy way to
1) Determine which was the previously selected Tab and
2) Prevent the user from going to the newly selected Tab in the case of data validation failure? A stateChanged event cannot be consumed.
Thanks in advance,

  • 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-06T05:52:04+00:00Added an answer on June 6, 2026 at 5:52 am

    Hmm you could try something like this which is basically adding a ChangeListener to monitor current tab and to keep previous tab selection:

    public class JTabbedPaneExample extends JFrame {
    
        private JTabbedPane tabbedPane;
        private JPanel panel1, panel2, panel3;
        private int currentTabIndex = 0, previousTabIndex = 0;
    
        public JTabbedPaneExample() {
            createAndShowUI();
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    JTabbedPaneExample jTabbedPaneExample = new JTabbedPaneExample();
                }
            });
        }
    
        private void createAndShowUI() {
            setTitle("JTabbedPaneExample");
            setSize(300, 300);
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            addComponentsToContentPane(getContentPane());
            addListeners();
            //pack();
            setVisible(true);
        }
    
        private void addComponentsToContentPane(Container contentPane) {
            tabbedPane = new JTabbedPane();
            panel1 = new JPanel();
            panel2 = new JPanel();
            panel3 = new JPanel();
            tabbedPane.insertTab("tab1", null, panel1, null, 0);
            tabbedPane.insertTab("tab2", null, panel2, null, 1);
            tabbedPane.insertTab("tab3", null, panel3, null, 2);
            contentPane.add(tabbedPane);
        }
    
        private void addListeners() {
            tabbedPane.addChangeListener(new ChangeListener() {
    
                @Override
                public void stateChanged(ChangeEvent ce) {
                    previousTabIndex = currentTabIndex;
                    currentTabIndex = tabbedPane.getSelectedIndex();
                    System.out.println("Current tab is:" + currentTabIndex);
                    System.out.println("Previous tab is:" + previousTabIndex);
                }
            });
        }
    }
    

    EDIT: here is a newer version which will only allow a tab change if the validation variable is set to ‘true’ this is done via a button click just to show the logic:

    public class JTabbedPaneExample extends JFrame {
    
        private JTabbedPane tabbedPane;
        private JPanel panel1, panel2, panel3;
        private int currentTabIndex = 0, previousTabIndex = 0;
        private boolean valid = false;
        private JButton changeVariableBtn;
    
        public JTabbedPaneExample() {
            createAndShowUI();
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    JTabbedPaneExample jTabbedPaneExample = new JTabbedPaneExample();
                }
            });
        }
    
        private void createAndShowUI() {
            setTitle("JTabbedPaneExample");
            setSize(300, 300);
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            addComponentsToContentPane(getContentPane());
            addListeners();
            //pack();
            setVisible(true);
        }
    
        private void addComponentsToContentPane(Container contentPane) {
            tabbedPane = new JTabbedPane();
            panel1 = new JPanel();
            panel2 = new JPanel();
            panel3 = new JPanel();
            changeVariableBtn = new JButton("Set validation to true");
            tabbedPane.insertTab("tab1", null, panel1, null, 0);
            tabbedPane.insertTab("tab2", null, panel2, null, 1);
            tabbedPane.insertTab("tab3", null, panel3, null, 2);
            contentPane.add(tabbedPane, BorderLayout.NORTH);
            contentPane.add(changeVariableBtn, BorderLayout.CENTER);
        }
    
        private void addListeners() {
            tabbedPane.addChangeListener(new ChangeListener() {
    
                private boolean automatedStateChange = false;
    
                @Override
                public void stateChanged(ChangeEvent ce) {
                    //this is used so when we allow the user not to go to the new tab by setting the tabb index to the previous one we dont want our changelistener to fire again as if the user were changing the tabs
                    if (automatedStateChange) {
                        automatedStateChange = false;
                    } else {
                        previousTabIndex = currentTabIndex;
                        currentTabIndex = tabbedPane.getSelectedIndex();
                    }
    
                    if (valid) {
                        System.out.println("Current tab is:" + currentTabIndex);
                        System.out.println("Previous tab is:" + previousTabIndex);
                        System.out.println("Validation succeeded: " + valid);
                        changeVariableBtn.doClick();
                    } else {
                        System.out.println("You need to enter all valid data first!");
                        automatedStateChange = true;
                        tabbedPane.setSelectedIndex(previousTabIndex);
                    }
                }
            });
            changeVariableBtn.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    if (valid) {
                        valid = false;
                        changeVariableBtn.setText("Set validation to true");
                    } else {
                        valid = true;
                        changeVariableBtn.setText("Set validation to false");
                    }
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.