Soy nuevo en Java en general y, en particular, en la biblioteca Swing. Cuando experimenté con la demo del Bloc de notas (demostración estándar de JDK), tuve un bloqueo al intentar cambiar el texto en la ventana del editor. Mi código de ejemplo:JTextArea/JTextComponent setText provoca crash
void Filter(Component f){
if (f instanceof JTextComponent){
JTextComponent textComponent = (JTextComponent) f;
textComponent.setVisible(false); //Works
textComponent.setVisible(true); //Works
textComponent.getText(); //Works
textComponent.updateUI(); //Works
textComponent.setText("Hello world!"); //Crashes
}else{
RecursiveGet(f);
}
}
void RecursiveGet(Component c){
for (Component f : ((JComponent) c).getComponents()) {
if (f instanceof JComponent) {
Filter(f);
}
}
}
me buscó por ejemplo JTextComponent, hasta que me encontré con él y luego probado algunos métodos. Creo que me falta algo, algunos detalles. Mi entorno JDK 1.7, JRE 7.0, Win7 x64. Estaré contento de tener alguna ayuda. Gracias.
actualización agrego gestor de excepciones
void Filter(Component f){
if (f instanceof JTextComponent){
JTextComponent textComponent = (JTextComponent) f;
textComponent.setVisible(false); //Work
textComponent.setVisible(true); //Work
textComponent.getText(); //Work
textComponent.updateUI(); //Work
try {
textComponent.setText("Hello world!"); //Crash
} catch (Exception e) {
e.printStackTrace();
}
}else{
RecursiveGet(f);
}
}
y obtén esta ..
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.synth.SynthContext.getPainter(Unknown Source)
at javax.swing.plaf.synth.SynthTextAreaUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JViewport.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown S
ource)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$700(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Actualización 2 método de trabajo setText después de añadir el bloque de gestión de excepciones. Pero, ¿qué extraño?
¿Hay errores detectados? – Brainbot
¿Qué quieres decir con 'crash'? ¿Lanza una excepción? ¿Y si si, que? –
Quiero decir que la aplicación Notepad se congeló y no responde a mi manipulación. – Darius