I am new to Java in general and in particular with the Swing library. When I experimented with the demo version of Notepad (a standard JDK demo), I got a crash when trying to change text in the editor window. My sample code is:
void Filter(Component f){
if (f instanceof JTextComponent){
JTextComponent textComponent = (JTextComponent) f;
textComponent.setVisible(false);
textComponent.setVisible(true);
textComponent.getText();
textComponent.updateUI();
textComponent.setText("Hello world!");
}else{
RecursiveGet(f);
}
}
void RecursiveGet(Component c){
for (Component f : ((JComponent) c).getComponents()) {
if (f instanceof JComponent) {
Filter(f);
}
}
}
I searched for a JTextComponent instance until I found it and then checked some methods. I think I'm missing something, some details. My environment is JDK 1.7, JRE 7.0, Win7 x64. I will be happy to help. Thank.
Update
I am adding an Exception handler
void Filter(Component f){
if (f instanceof JTextComponent){
JTextComponent textComponent = (JTextComponent) f;
textComponent.setVisible(false);
textComponent.setVisible(true);
textComponent.getText();
textComponent.updateUI();
try {
textComponent.setText("Hello world!");
} catch (Exception e) {
e.printStackTrace();
}
}else{
RecursiveGet(f);
}
}
and get it.
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)
Update 2
The setText method works after adding an exception handler block. But what did I miss?
source
share