Results 1 to 2 of 2
- 03-12-2012, 04:01 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 1
- Rep Power
- 0
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:
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: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)
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:
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?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); }
Thanks in advance,
- 03-12-2012, 08:32 PM #2
Re: Problem with Swing (setCaretPosition) after migrating to JDK 1.6
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
migrating from flash to java
By wassabi in forum New To JavaReplies: 1Last Post: 12-13-2011, 03:07 PM -
Migrating From Spring to Struts
By sumeetdesaeee in forum Jobs DiscussionReplies: 3Last Post: 08-15-2011, 06:55 AM -
Migrating from Java version 1.5 update 6 to version1.6
By pink123 in forum New To JavaReplies: 8Last Post: 05-24-2011, 11:32 AM -
Migrating to a new Oracle version
By aborgeld in forum JDBCReplies: 2Last Post: 03-10-2011, 09:22 AM -
Migrating Java SOAP app. Help please
By Fortunato in forum Advanced JavaReplies: 2Last Post: 11-01-2010, 09:20 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks