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
    Posts
    1
    Rep Power
    0

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

    Hello,

    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(JTextComponent.java:1650)  
            at company.swing.JTextFieldCPF.<init>(JTextFieldCPF.java:130)  
            at company.swing.JTextFieldCPF.<init>(JTextFieldCPF.java:98)  
            at company.swing.JTextFieldCPF.<init>(JTextFieldCPF.java:75)  
            at company_system..principal.pessoa.fronteira.TelaPessoaFisica.<init>(TelaPessoaFisica.java:45)  
            at company_system..principal.pessoa.fronteira.TelaPessoaFisica.mostraDialogo(TelaPessoaFisica.java:92)  
            at company_system..principal.prestacao.fronteira.TelaCadastroResponsaveis.buttonCadastrarPessoaFisica_actionPerformed(TelaCadastroResponsaveis.java:145)  
            at company_system..principal.prestacao.fronteira.TelaCadastroResponsaveis.access$0(TelaCadastroResponsaveis.java:139)  
            at company_system..principal.prestacao.fronteira.TelaCadastroResponsaveis$3.actionPerformed(TelaCadastroResponsaveis.java:105)  
            at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)  
            at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)  
            at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)  
            at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)  
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)  
            at java.awt.Component.processMouseEvent(Component.java:6288)  
            at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)  
            at java.awt.Component.processEvent(Component.java:6053)  
            at java.awt.Container.processEvent(Container.java:2041)  
            at java.awt.Component.dispatchEventImpl(Component.java:4651)  
            at java.awt.Container.dispatchEventImpl(Container.java:2099)  
            at java.awt.Component.dispatchEvent(Component.java:4481)  
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)  
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)  
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)  
            at java.awt.Container.dispatchEventImpl(Container.java:2085)  
            at java.awt.Window.dispatchEventImpl(Window.java:2478)  
            at java.awt.Component.dispatchEvent(Component.java:4481)  
            at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)  
            at java.awt.EventQueue.access$000(EventQueue.java:84)  
            at java.awt.EventQueue$1.run(EventQueue.java:602)  
            at java.awt.EventQueue$1.run(EventQueue.java:600)  
            at java.security.AccessController.doPrivileged(Native Method)  
            at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)  
            at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)  
            at java.awt.EventQueue$2.run(EventQueue.java:616)  
            at java.awt.EventQueue$2.run(EventQueue.java:614)  
            at java.security.AccessController.doPrivileged(Native Method)  
            at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)  
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)  
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)  
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)  
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)  
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)  
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)  
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    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);  
    			}  
                caret.setDot(position);  
              }  
          }

    The code that calls the code above is:

    Java Code:
        public JTextFieldCPF(boolean verificarValorCPF, String cpf)  
        {  
          
          super(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)  
            {  
              jTextFieldCPF_focusLost(ev);  
            }  
          });  
          cpf = this.getText();  
          posicaoCaracter = cpf.length();  
          this.setCaretPosition(posicaoCaracter);  
          this.setHorizontalAlignment(JTextField.LEFT);  
        }
    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 Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    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.

    db
    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
  •