Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Problem with Swing (setCaretPosition) after migrating to JDK 1.6

  1. #1
    cholodovskis is offline Member
    Join Date
    Mar 2012
    Rep Power

    Default Problem with Swing (setCaretPosition) after migrating to JDK 1.6


    I'm facing the following problem: I'm migrating a system from JDK version 1.3 to 1.6. The system is a desktop system that uses Swing.
    There is a people register screen that is opened when a given button is clicked. Before migrating everything worked correctly but, after migrating, when I click on the button that would call this screen, it does not open and the following exception is thrown:

    Java Code:
        Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: bad position: 14  
            at javax.swing.text.JTextComponent.setCaretPosition(  
            at company.swing.JTextFieldCPF.<init>(  
            at company.swing.JTextFieldCPF.<init>(  
            at company.swing.JTextFieldCPF.<init>(  
            at company_system..principal.pessoa.fronteira.TelaPessoaFisica.<init>(  
            at company_system..principal.pessoa.fronteira.TelaPessoaFisica.mostraDialogo(  
            at company_system..principal.prestacao.fronteira.TelaCadastroResponsaveis.buttonCadastrarPessoaFisica_actionPerformed(  
            at company_system..principal.prestacao.fronteira.TelaCadastroResponsaveis.access$0(  
            at company_system..principal.prestacao.fronteira.TelaCadastroResponsaveis$3.actionPerformed(  
            at javax.swing.AbstractButton.fireActionPerformed(  
            at javax.swing.AbstractButton$Handler.actionPerformed(  
            at javax.swing.DefaultButtonModel.fireActionPerformed(  
            at javax.swing.DefaultButtonModel.setPressed(  
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(  
            at java.awt.Component.processMouseEvent(  
            at javax.swing.JComponent.processMouseEvent(  
            at java.awt.Component.processEvent(  
            at java.awt.Container.processEvent(  
            at java.awt.Component.dispatchEventImpl(  
            at java.awt.Container.dispatchEventImpl(  
            at java.awt.Component.dispatchEvent(  
            at java.awt.LightweightDispatcher.retargetMouseEvent(  
            at java.awt.LightweightDispatcher.processMouseEvent(  
            at java.awt.LightweightDispatcher.dispatchEvent(  
            at java.awt.Container.dispatchEventImpl(  
            at java.awt.Window.dispatchEventImpl(  
            at java.awt.Component.dispatchEvent(  
            at java.awt.EventQueue.dispatchEventImpl(  
            at java.awt.EventQueue.access$000(  
            at java.awt.EventQueue$  
            at java.awt.EventQueue$  
            at Method)  
            at java.awt.EventQueue$  
            at java.awt.EventQueue$  
            at Method)  
            at java.awt.EventQueue.dispatchEvent(  
            at java.awt.EventDispatchThread.pumpOneEventForFilters(  
            at java.awt.EventDispatchThread.pumpEventsForFilter(  
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(  
            at java.awt.EventDispatchThread.pumpEvents(  
            at java.awt.EventDispatchThread.pumpEvents(  
    While I was debugging I've seen that in JTextComponent.class (of swing), the following code throws an exception in the validation position > doc.getLength(). The doc size is zero.

    Java Code:
          public void setCaretPosition(int position) {  
              Document doc = getDocument();  
              if (doc != null) {  
                if (position > doc.getLength() || position < 0) {  
                  throw new IllegalArgumentException("bad position: " + position);  

    The code that calls the code above is:

    Java Code:
        public JTextFieldCPF(boolean verificarValorCPF, String cpf)  
          this.verificarPerdaFoco = verificarValorCPF;  
          // Adiciona um ouvinte para verificar um evento de foco deste objeto.  
          this.addFocusListener(new FocusAdapter()  
            // Quando o objeto perde o foco, o este método é invocado e repassa o evento para o método  
            // jTextFieldCPF_focusLost, que irá tratar o evento.  
            public void focusLost(FocusEvent ev)  
          cpf = this.getText();  
          posicaoCaracter = cpf.length();  
    I believe that the problem is when it allocates Document, I need to increase its size first, but I'm not being able to solve this problem. I'm a little lost, do you have any suggestion?

    Thanks in advance,

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: Problem with Swing (setCaretPosition) after migrating to JDK 1.6

    Quote Originally Posted by cholodovskis View Post
    Before migrating everything worked correctly but, after migrating, when I click on the button that would call this screen, it does not open and the following exception is thrown:
    Looks like a concurrency issue. Does your code respect Swing's single threaded rule?

    Note that some of the methods documented as thread-safe in the APIs for Java versions up to 1.6 are in fact not thread-safe, and following multiple bug reports the API of Java 1.7 has been corrected to remove the qualification. These include a few of the methods related to setting and getting text of text components, and were never thread-safe because of the possibility of dealing directly with the Document, thus bypassing the methods of the component altogether.

    Ensure that all Swing constructors and methods, whether of view components or models (that includes documents) are called on the EDT and only on the EDT.

    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. migrating from flash to java
    By wassabi in forum New To Java
    Replies: 1
    Last Post: 12-13-2011, 03:07 PM
  2. Migrating From Spring to Struts
    By sumeetdesaeee in forum Jobs Discussion
    Replies: 3
    Last Post: 08-15-2011, 06:55 AM
  3. Replies: 8
    Last Post: 05-24-2011, 11:32 AM
  4. Migrating to a new Oracle version
    By aborgeld in forum JDBC
    Replies: 2
    Last Post: 03-10-2011, 09:22 AM
  5. Migrating Java SOAP app. Help please
    By Fortunato in forum Advanced Java
    Replies: 2
    Last Post: 11-01-2010, 09:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts