I am seeing some odd behavior with Swing components for which the background alpha value is set to a transparent value. Subsequent calls to setText() on the transparent component are superimposing the new text over the old text, rather than replacing it. The only workaround I've found is to call setVisible(false) and then setVisible(true) on the containing JFrame, which results in a slightly annoying flicker.
See the following attached example with a JTextArea (alpha value 0) placed on a JFrame. Initial text is set to "original text", then after 2 seconds the text area's message is set to "modified text", and I'm able to observe the odd super-imposing.
Any thoughts? Better workarounds?
(For reference, the observed behavior is on OS X, JDK 1.5)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class TextAreaSuperimpose
{
public static void main( String[] args )
{
JFrame myFrame = new JFrame ( ) ;
myFrame.setPreferredSize ( new Dimension ( 300, 200 ) ) ;
myFrame.setMaximumSize ( new Dimension ( 400, 300 ) ) ;
myFrame.setResizable ( false ) ;
JPanel content = new JPanel ( ) ;
content.setLayout ( new BorderLayout ( ) ) ;
content.setBackground ( new Color ( 0, 0, 0, 0 ) ) ;
JTextArea myTextArea = new JTextArea ( "" ) ;
myTextArea.setEditable ( false ) ;
myTextArea.setBackground ( new Color ( 0, 0, 0, 0 ) ) ;
content.add ( myTextArea, BorderLayout.CENTER ) ;
myFrame.add ( Box.createHorizontalStrut ( 10 ), BorderLayout.WEST ) ;
myFrame.add ( Box.createHorizontalStrut ( 10 ), BorderLayout.EAST ) ;
myFrame.add ( Box.createVerticalStrut ( 10 ), BorderLayout.NORTH ) ;
myFrame.add ( Box.createVerticalStrut ( 10 ), BorderLayout.SOUTH ) ;
myFrame.add ( content, BorderLayout.CENTER ) ;
myFrame.pack ( ) ;
myTextArea.setText( "original text" ) ;
myFrame.invalidate ( ) ;
myFrame.setVisible ( true ) ;
try
{
Thread.sleep( 2000 ) ;
}
catch ( InterruptedException e )
{
// NOOP
}
myTextArea.setText( "modified text" ) ;
myFrame.invalidate() ;
myFrame.setVisible ( true ) ;
}
}