Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with transparent Swing components, setText() superimposes

843805Apr 10 2007 — edited Apr 11 2007
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 ) ;
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 9 2007
Added on Apr 10 2007
9 comments
830 views