lately we deceided to try using Overlayables for userfeedback, when something is wrong within a textfield. All was fine, until we installed them on dialogs with GridBagLayout (which is the case for most of our dialogs). The Icon doesnt get displayed anymore, unless its activated by default. I prepared a small example to give you an impression of what I am talking about:
- Code: Select all
/**
* Example: weird behaviour for Overlays within a GridBagLayout.
*/
public class OverlayWeirdBehaviourExample extends JPanel
{
public OverlayWeirdBehaviourExample()
{
// If any other Layout is used it works as well.
setLayout( new GridBagLayout() );
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets( 10, 10, 10, 10 );
JLabel correctIcon =
new JLabel( OverlayableUtils.getPredefinedOverlayIcon( OverlayableIconsFactory.ATTENTION ) );
correctIcon.setToolTipText( "Error ..." );
correctIcon.setCursor( Cursor.getPredefinedCursor( Cursor.HAND_CURSOR ) );
final DefaultOverlayable overlayTextField =
new DefaultOverlayable( new OverlayTextField( "Text Field", 10 ), correctIcon,
SwingConstants.NORTH_EAST );
//Without setting it to false, it also works on gridbaglayout!
overlayTextField.setOverlayVisible( false );
add( overlayTextField, c );
JButton button = new JButton( "change state" );
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
c.insets = new Insets( 10, 10, 10, 10 );
button.addActionListener( new ActionListener()
{
@Override
public void actionPerformed( ActionEvent e )
{
if ( overlayTextField.getOverlayComponents()[ 0 ].isVisible() )
{
overlayTextField.setOverlayVisible( false );
System.out.println( "false" );
}
else
{
overlayTextField.setOverlayVisible( true );
System.out.println( "true" );
}
}
} );
add( button, c );
}
public static void main( String[] args )
{
JFrame frame = new JFrame( "OverlayWeirdBehaviourExample" );
frame.add( new OverlayWeirdBehaviourExample() );
frame.pack();
frame.setLocationByPlatform( true );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible( true );
}
}
Switching the LayoutManager to any other LayoutManager and the Example works. It also works fine (even within GridBagLayout), when I comment out the line:
- Code: Select all
overlayTextField.setOverlayVisible( false );
But since we dont want the icon to be shown, when everything is fine, that aint an option. Has anyone an idea of what we do wrong?
Thank you very much in advance,
ymene