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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:10:01+00:00 2026-06-13T12:10:01+00:00

This is a follow on question from Display and interact with an HTML form

  • 0

This is a follow on question from Display and interact with an HTML form in a Swing application. I’ve copied the code and run it successfully while in Eclipse (Indigo) but for some mysterious reason it’s stopped working. I was able to run it several times in a row, but now I’m lucky if I can get it to work at all.

Running it with JUnit under debug, it I’ve single stepped up to thejavax.swing.SwingUtilities.invokeLater(new Runnable() { line; The next step jumps straight to the System.in.read() line effectively skipping the display logic.

I suspect its got something to the invokeLater method. From my research, invokeLater puts its Runnable() on the dispatch queue and the Runnable will be run whenever the VM gets around to it. There’s some mention that if a Runnable() experiences and exception it won’t “unwind”, which I assume means that it won’t close and will end up blocking the queue. I suspect that one of my invocations has stopped and is clogging the queue but have no idea why or how to clear the queue.

Questions:
Is there any way to clear the queue? I’ve stopped and restarted Eclipse several times thinking that would clear the dispatch queue, but no luck.

Is it possible that as far as Java is concerned the JFrame is displaying, but it’s not actually showing up on the screen so I can hit the submit button? What would cause this? Why would it work several times in a row, then suddenly stop?

Below is the relavant code.

JUnit stub:

@Test
public final void testTestForms()  {
    // https://stackoverflow.com/questions/6145405/display-and-interact-with-an-html-form-in-a-swing-application
    Navigate n = new Navigate();
    try {
        n.testForms();
        n= null;  // in desperate attempt to garbage collect
    } catch (IOException e) {
        System.out.println("Error invoking n.testForms()");
        e.printStackTrace();
        n= null;
    }
    n = null;

}

The subject under study:

public class Navigate  {
@Test
public void testForms() throws IOException {
    System.out.println("in testForms()");
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            System.out.println("in run()");
            javax.swing.JFrame jf = new javax.swing.JFrame();
            jf.setSize(300, 300);
            jf.setVisible(true);
            jf.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

            JTextPane textPane = new JTextPane();
            textPane.setContentType("text/html");
            textPane.setEditable(false);
            textPane.setText("<html>" + "<body>" + "<form action=\"#\">"
                    + "<input name=\"input1\" type=\"text\" />"
                    + "<input name=\"input2\" type=\"text\" /><br/>"
                    + "<input name=\"cb1\" type=\"checkbox\" /><br/>"
                    + "<input name=\"rb1\" type=\"radio\" /><br/>"
                    + "<input type=\"submit\" value=\"go\" />" + "</form>"
                    + "</body>" + "</html>");

            jf.getContentPane().setLayout(
                    new BoxLayout(jf.getContentPane(), BoxLayout.Y_AXIS));

            jf.getContentPane().add(textPane);

            HTMLEditorKit kit = (HTMLEditorKit) textPane.getEditorKit();
            kit.setAutoFormSubmission(false);
            textPane.addHyperlinkListener(new HyperlinkListener() {
                public void hyperlinkUpdate(HyperlinkEvent e) {
                    if (e instanceof FormSubmitEvent) {
                        System.out.println(((FormSubmitEvent) e).getData());
                    }
                }
            });
        }
    }
);
    // try to catch exceptions that could plug the queue?
    try {
        System.in.read();
    } catch (IOException e) {
    System.out.println("Error with System.in.read()");
        e.printStackTrace();
        throw new IOException(e);
    }
    try {
        finalize();  // another desperate attempt
    } catch (Throwable e) {
        e.printStackTrace();
    }
}
}
  • 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-13T12:10:02+00:00Added an answer on June 13, 2026 at 12:10 pm

    Is it possible that as far as Java is concerned the JFrame is
    displaying, but it’s not actually showing up on the screen so I can
    hit the submit button? What would cause this? Why would it work
    several times in a row, then suddenly stop?

    Yes because you call setVisible(true) on JFrame instance before all components have been added.

    There are also other anomalies in your code which could cause problems:

    • Dont call JFrame#setVisible(true) before all components have been added to JFrame
    • Dont call JFrame#setSize(..) rather call JFrame#pack() just before setting JFrame visible
    • Your SwingUtilities.invokeLater(..) should not be nested in the JFrame class itself rather make it so that you surround the creation of the JFrame instance in SwingUtilitites.invokeLater(...)
    • Dont call System.in.read() this will block the EDT thread and your UI will freeze
    • Dont rely on calling a method to create the JFrame instance (testForms()), make sure the JFrame instance is created within the classes constructor.

    Here is an exmple I made:

    enter image description here

    import javax.swing.BoxLayout;
    import javax.swing.JTextPane;
    import javax.swing.event.HyperlinkEvent;
    import javax.swing.event.HyperlinkListener;
    import javax.swing.text.html.FormSubmitEvent;
    import javax.swing.text.html.HTMLEditorKit;
    
    public class JavaApplication26 {
    
        public static void main(String[] args) {
    
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    //create new instance of JFrame
                    new Navigate();
                }
            });
        }
    }
    
    class Navigate {
    
        public Navigate() {
            initComponents();
        }
    
        private void initComponents() { //this is a constructor
    
            javax.swing.JFrame jf = new javax.swing.JFrame();
            jf.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    
            JTextPane textPane = new JTextPane();
            textPane.setContentType("text/html");
            textPane.setEditable(false);
            textPane.setText("<html>" + "<body>" + "<form action=\"#\">"
                    + "<input name=\"input1\" type=\"text\" />"
                    + "<input name=\"input2\" type=\"text\" /><br/>"
                    + "<input name=\"cb1\" type=\"checkbox\" /><br/>"
                    + "<input name=\"rb1\" type=\"radio\" /><br/>"
                    + "<input type=\"submit\" value=\"go\" />" + "</form>"
                    + "</body>" + "</html>");
    
            HTMLEditorKit kit = (HTMLEditorKit) textPane.getEditorKit();
            kit.setAutoFormSubmission(false);
            textPane.addHyperlinkListener(new HyperlinkListener() {
                @Override
                public void hyperlinkUpdate(HyperlinkEvent e) {
                    if (e instanceof FormSubmitEvent) {
                        System.out.println(((FormSubmitEvent) e).getData());
                    }
                }
            });
    
            //add components
            jf.getContentPane().setLayout(new BoxLayout(jf.getContentPane(), BoxLayout.Y_AXIS));
            jf.getContentPane().add(textPane);
    
            jf.pack();//pack
            jf.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow up from my previous question I have this code basically
This is a follow up question from Calling constructor in return statement . This
This is a follow-on question from the one I asked here . Can constraints
This is a follow up from a question of mine that was just answered
This is a follow-on from this question, in which I was trying to suppress
This is a follow-on from a previous SO question Anchoring CSS Repeating Background Image
This is a follow on from my previous question although this is about something
This question is a follow on from this one ... I am binding to
As a follow on from this question I'm building a custom server control to
This is a follow on from my last question relating to plotting timestamps in

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.