Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Pressing ALT obscures components when using Windows L&F

Thread view: 
ub - 11 Apr 2007 00:12 GMT
I have a JFrame that displays a big JLabel in the background and a
JCheckBox in the front.

When using the Windows L&F and I press the Alt-key the JLabel is
repainted but not the JCheckBox in front of it. As a consequence the
checkbox disappears. Forcing a repaint (e.g. by resizing the frame)
will make the checkbox re-appear.

When not using the Window L&F everything is fine.

Any suggestion how to work around this problem?

ub

Here the sourcecode:
-----
import java.awt.Color;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UnsupportedLookAndFeelException;

public class AltKeyRepaint {
    public static void main(String args[]) throws ClassNotFoundException,
            InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
        // Use the System (i.e. "Windows") L&F
        javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager
                .getSystemLookAndFeelClassName());

        // The frame to display the sample
        JFrame frame = new JFrame("Frame");
        frame.setSize(250, 200);
        frame.getContentPane().setLayout(null);

        // A big colored label in the background
        JLabel label = new JLabel("A sample label in the background");
        label.setBackground(Color.ORANGE);
        label.setOpaque(true);
        label.setBounds(5, 5, 200, 150);

        // A check box on top of the background label
        JCheckBox chkBox = new JCheckBox("a check box on top");
        chkBox.setBounds(30, 30, 150, 20);

        // Add the label and check box to the frame
        frame.getContentPane().add(chkBox);
        frame.getContentPane().add(label);

        // display it
        frame.show();

        // Now pressing the ALT key will repaint the label, but not the
        // check box, i.e. the check box disappears.
        // Forcing a repaint (e.g. by resizing the frame) will make the
check
        // box re-appear.

        // Without the Windows L&F (first statement of function) everything
        // will be fine.
    }
}
Smith, Derek - 11 Apr 2007 01:21 GMT
so dont use windows l&f

>I have a JFrame that displays a big JLabel in the background and a
> JCheckBox in the front.
[quoted text clipped - 59 lines]
> }
> }
Joe Blow - 11 Apr 2007 02:12 GMT
duh

> so dont use windows l&f
>
[quoted text clipped - 61 lines]
>> }
>> }
Lew - 11 Apr 2007 13:24 GMT
> so dont use windows l&f

Please do not top-post.

Signature

Lew

Smith, Derek - 11 Apr 2007 01:22 GMT
so dont do it then

>I have a JFrame that displays a big JLabel in the background and a
> JCheckBox in the front.
[quoted text clipped - 59 lines]
> }
> }
Smith, Derek - 11 Apr 2007 01:27 GMT
so dont use the windows L&F
>I have a JFrame that displays a big JLabel in the background and a
> JCheckBox in the front.
[quoted text clipped - 59 lines]
> }
> }
Andrew Thompson - 11 Apr 2007 02:01 GMT
>so dont use ..

- Punctuation in words like don't?
- Restraint when posting 4 of the same reply?
- Inline trimming posting in replies?

None of those things will solve the OP's stated problem,
which relates mostly to poor use of layouts.

(When I get home later, I will test some altered source
that should solve the *stated* problem.)

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Smith, Derek - 11 Apr 2007 01:34 GMT
so dont use it.

>I have a JFrame that displays a big JLabel in the background and a
> JCheckBox in the front.
[quoted text clipped - 59 lines]
> }
> }
Andrew Thompson - 11 Apr 2007 03:40 GMT
>I have a JFrame that displays a big JLabel in the background and a
>JCheckBox in the front.

The JCB is actually 'above' the JL, here.

>When using the Windows L&F and I press the Alt-key the JLabel is
>repainted but not the JCheckBox in front of it. As a consequence the
>checkbox disappears. Forcing a repaint (e.g. by resizing the frame)
>will make the checkbox re-appear.
>
>When not using the Window L&F everything is fine.

I did not see the stated problem, either way, but
noticed the very fragile code being used.

>Any suggestion how to work around this problem?

Use layouts.  This might not be exactly what you
want, it might require a nested layout to get the
full effect, but it is perhaps a starting point..

<sscce>
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.UnsupportedLookAndFeelException;

public class AltKeyRepaint {
   public static void main(String args[]) throws ClassNotFoundException,
           InstantiationException, IllegalAccessException,
           UnsupportedLookAndFeelException {
       // Use the System (i.e. "Windows") L&F
       // ie only if the user is Window'd
       // otherwise it will be the Mac. or *nix style PLAF's
       javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager
               .getSystemLookAndFeelClassName());

       // The frame to display the sample
       JFrame frame = new JFrame("Frame");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       // this is probably not only the source of the
       // current problems, but a dozen others that
       // have either not been seen, or not manifested yet.
       // Do not use 'null' layouts unless you..
       //  a) know what your doing, (and potentially also..)
       //  b) are designing a custom layout,
       // frame.getContentPane().setLayout(null);
       JPanel p = new JPanel(new BorderLayout());
       p.setBackground(Color.ORANGE);

       // A big colored label in the background
       JLabel label = new JLabel("A sample label in the CENTER",
           JLabel.CENTER);
       label.setOpaque(false);
       // play with the padding numbers,
       // to get the right effect..
       label.setBorder( new EmptyBorder(30,15,50,15) );

       // A check box on top of the background label
       JCheckBox chkBox = new JCheckBox("a check box in NORTH");
       chkBox.setOpaque(false);
       chkBox.setBorder( new EmptyBorder(20,5,20,5) );

       // Add the label and check box to the frame
       p.add(chkBox, BorderLayout.NORTH);
       p.add(label, BorderLayout.CENTER);

       frame.getContentPane().add(p);

       // display it
       // deprecated in Java 1.2/1.5
       // frame.show();

       // this cause everything to become the
       // preferred size, which might be different
       // on different platforms, PLAF's, font sizes,
       // Java versions, or screen resolutions.
       frame.pack();
       frame.setLocationRelativeTo(null);
       frame.setVisible(true);
   }
}
</sscce>

HTH

Signature

Andrew Thompson
http://www.athompson.info/andrew/

ub - 11 Apr 2007 13:30 GMT
The code I presented is the smallest code sample I could come up to
show the problem I ran into. Using the "null" layout was the simplest
way to demonstrate the issue. Actually a custom-made LayoutManager is
used in the real application but shows exactly the same behaviour as
the "null" layout.

The "real" application is part of an "AWT to Swing" porting project.
In that application the components are created dynamically, based on
some "form definitions" that define the location and size of the
individual components. Because of the amount and size of these
definitions (>8000, typically more than 10 components in each form)
rewriting the layout is not really an option.

I did some more tests and it looks like the issue also depends on the
graphic card. When using a DVI output the button disappears, but on a
VGA output everything is fine. So it may be a driver issue. Does that
makes sense?

ub

> >I have a JFrame that displays a big JLabel in the background and a
> >JCheckBox in the front.
[quoted text clipped - 87 lines]
>
> Message posted via JavaKB.comhttp://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1
Andrew Thompson - 11 Apr 2007 13:43 GMT
Please refrain from top-posting in future.  I
find it most confusing.
<http://www.physci.org/codes/javafaq.html#toppost>

>The code I presented is the smallest code sample I could come up to
>show the problem I ran into.

The problem did not display here.

>..Using the "null" layout was the simplest
>way to ...

..introduce further problems that obscure the issue?

>..demonstrate the issue. Actually a custom-made LayoutManager is
>used in the real application but shows exactly the same behaviour as
>the "null" layout.

The first thing usually done in a custom layout manager
(as mentioned in my post) is to setLayout(null).  If the
author of that custom layout showed as little
understanding of the complexities of creating a
layout as was demonstrated in the posted example,
it is just as broken as the posted code.

>...rewriting the layout is not really an option.
(snip..)

Sounds as though you are up sh*t creek, without
a paddle, if you are limited as to how to approach
solutions.  Good luck with it.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Real Gagnon - 11 Apr 2007 13:45 GMT
> I did some more tests and it looks like the issue also depends on the
> graphic card. When using a DVI output the button disappears, but on a
> VGA output everything is fine. So it may be a driver issue. Does that
> makes sense?

Try to disable the direct draw feature with :

java -Dsun.java2d.noddraw=true <class>

Bye.
Signature

Real Gagnon  from  Quebec, Canada
* Java, Javascript, VBScript and PowerBuilder code snippets
* http://www.rgagnon.com/howto.html
* http://www.rgagnon.com/bigindex.html

ub - 12 Apr 2007 00:37 GMT
> Try to disable the direct draw feature with :
>
> java -Dsun.java2d.noddraw=true <class>

Thanks for the tip.

I tried it but nothing changed.

But I "upgraded" the JRE from 1.4.2_6 to 1.5 and the problem is gone.
So it looks like it was related to a bug in the JRE that was fixed
after 1.4.2.

Thanks to everybody for your support,

ub


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.