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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:08:08+00:00 2026-05-30T02:08:08+00:00

There is a problem in my application when trying to set focus on a

  • 0

There is a problem in my application when trying to set focus on a JEditorPane using the tab key. I did not understand why at first, but I manage to make a small test case that demonstrates the issue.

public class JEditorPaneFocusTest
{
    public static void main(String... args) throws Exception
    {
        JFrame jFrame = new JFrame();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container container = jFrame.getContentPane();
        container.setLayout(new BorderLayout());

        container.add(new JTextField(), BorderLayout.NORTH);

        JEditorPane editorPane = new JEditorPane();
        editorPane.setEditorKit(new HTMLEditorKit());
        editorPane.setText("<html><body>Hello World</body></html>");
        container.add(editorPane, BorderLayout.CENTER);

        jFrame.setSize(new Dimension(400, 400));
        jFrame.setVisible(true);
    }
}

(Tested on Windows 7 and Mac OS X Lion.) The application will start with focus on the JTextField. Using the tab key won’t set focus to the JEditorPane. But if you comment the setText line, it seems to work…

Any idea?

  • 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-30T02:08:09+00:00Added an answer on May 30, 2026 at 2:08 am
    • short answer

    delay this event by wraping to the invokeLater()

    • longer answer

    Focus / Focus Subsystem is asynchronous, in most cases hard to manage/timing this/these even(s), for better understanding about this issue you have to read linked tutorials,

    example about Focus issue

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.font.TextAttribute;
    import java.math.RoundingMode;
    import java.text.NumberFormat;
    import java.util.Map;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    import javax.swing.event.*;
    
    public class TextAttributeSTRIKETHROUGH {
    
        private JFrame frame = new JFrame();
        private JPanel pnl = new JPanel();
        private JLabel focusLabel = new JLabel(" focusLost Handle ");
        private JFormattedTextField formTextField;
        private JLabel docLabel = new JLabel(" document Handle ");
        private JFormattedTextField formTextField1;
        private NumberFormat formTextFieldFormat;
        private double amount = 10000.00;
        private Map attributes;
    
        @SuppressWarnings("unchecked")
        public TextAttributeSTRIKETHROUGH() {
            formTextFieldFormat = NumberFormat.getNumberInstance();
            formTextFieldFormat.setMinimumFractionDigits(2);
            formTextFieldFormat.setMaximumFractionDigits(2);
            formTextFieldFormat.setRoundingMode(RoundingMode.HALF_UP);
    
            focusLabel.setFont(new Font("Serif", Font.BOLD, 14));
            focusLabel.setForeground(Color.blue);
            focusLabel.setPreferredSize(new Dimension(120, 27));
            formTextField = new JFormattedTextField(formTextFieldFormat);
            formTextField.setValue(amount);
            formTextField.setFont(new Font("Serif", Font.BOLD, 22));
            formTextField.setForeground(Color.black);
            formTextField.setBackground(Color.yellow);
            formTextField.setPreferredSize(new Dimension(120, 27));
            formTextField.setHorizontalAlignment(SwingConstants.RIGHT);
            formTextField.addFocusListener(new FocusListener() {
    
                @Override
                public void focusGained(FocusEvent e) {
                    formTextField.requestFocus();
                    formTextField.setText(formTextField.getText());
                    formTextField.selectAll();
                }
    
                public void focusLost(FocusEvent e) {
                    //Runnable doRun = new Runnable() {
    
                    //@Override
                    //public void run() {
                    double t1a1 = (((Number) formTextField.getValue()).doubleValue());
                    if (t1a1 < 1000) {
                        formTextField.setValue(amount);
                    }
                    //}
                    // };
                    //SwingUtilities.invokeLater(doRun);
                }
            });
    
            docLabel.setFont(new Font("Serif", Font.BOLD, 14));
            docLabel.setForeground(Color.blue);
            docLabel.setPreferredSize(new Dimension(120, 27));
    
            formTextField1 = new JFormattedTextField(formTextFieldFormat);
            formTextField1.setValue(amount);
            formTextField1.setFont(new Font("Serif", Font.BOLD, 22));
            formTextField1.setForeground(Color.black);
            formTextField1.setBackground(Color.yellow);
            formTextField1.setPreferredSize(new Dimension(120, 27));
            formTextField1.setHorizontalAlignment(SwingConstants.RIGHT);
            formTextField1.addFocusListener(new FocusListener() {
    
                @Override
                public void focusGained(FocusEvent e) {
                    formTextField1.requestFocus();
                    formTextField1.setText(formTextField1.getText());
                    formTextField1.selectAll();
                }
    
                @Override
                public void focusLost(FocusEvent e) {
                }
            });
            formTextField1.getDocument().addDocumentListener(docListener);
    
            attributes = (new Font("Serif", Font.BOLD, 24)).getAttributes();
            attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
    
            pnl = new JPanel();
            pnl.setBorder(new EmptyBorder(2, 2, 2, 2));
            pnl.setLayout(new GridLayout(2, 2));
            pnl.add(focusLabel);
            pnl.add(formTextField);
            pnl.add(docLabel);
            pnl.add(formTextField1);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(pnl, BorderLayout.CENTER);
            frame.setLocation(200, 200);
            frame.pack();
            frame.setVisible(true);
            formTextFieldFocus1();
        }
        //
        private DocumentListener docListener = new DocumentListener() {
    
            @Override
            public void changedUpdate(DocumentEvent documentEvent) {
                printIt(documentEvent);
            }
    
            @Override
            public void insertUpdate(DocumentEvent documentEvent) {
                printIt(documentEvent);
            }
    
            @Override
            public void removeUpdate(DocumentEvent documentEvent) {
                printIt(documentEvent);
            }
    
            private void printIt(DocumentEvent documentEvent) {
                DocumentEvent.EventType type = documentEvent.getType();
                double t1a1 = (((Number) formTextField1.getValue()).doubleValue());
                if (t1a1 < 1000) {
                    Runnable doRun = new Runnable() {
    
                        @Override
                        public void run() {
                            formTextField1.setFont(new Font(attributes));
                        }
                    };
                    SwingUtilities.invokeLater(doRun);
                } else {
                    Runnable doRun = new Runnable() {
    
                        @Override
                        public void run() {
                            formTextField1.setFont(new Font("Serif", Font.BOLD, 22));
                        }
                    };
                    SwingUtilities.invokeLater(doRun);
                }
            }
        };
    
        private void formTextFieldFocus1() {
            Runnable doRun = new Runnable() {
    
                @Override
                public void run() {
                    formTextField1.grabFocus();
                    formTextField1.requestFocus();
                    formTextField1.setText(formTextField1.getText());
                    formTextField1.selectAll();
                }
            };
            SwingUtilities.invokeLater(doRun);
        }
    
        private void formTextFieldFocus() {
            Runnable doRun = new Runnable() {
    
                @Override
                public void run() {
                    formTextField.grabFocus();
                    formTextField.requestFocus();
                    formTextField.setText(formTextField.getText());
                    formTextField.selectAll();
                    formTextFieldFocus1();
                }
            };
            SwingUtilities.invokeLater(doRun);
        }
    
        public static void main(String args[]) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    TextAttributeSTRIKETHROUGH fl = new TextAttributeSTRIKETHROUGH();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a swings application but there is one problem in that which
I've problem with exception dialogs: I'm using RemObjects SDK for Client/Server -application. When there's
I'm essentially trying to make a DNS proxy application using Qt4. If I set
I'm trying to set up a web application for the first time on Amazon
I'm trying to build a PhoneGap-based application using Xcode 4. Since there's no PhoneGap
I have an application set up using jaas module for login. There are ajax
My application will take a set of files and sign them. (I'm not trying
I have problem with my application. I have table report, there are 2 column
When facing the problem of validating a property in a JSF2 application there are
Friend of mine has a problem :). There is an application written in Visual

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.