views:

118

answers:

2

I am trying to construct a basic control that will display an undecorated JFrame right below it when a button is clicked. I am trying to mimic drop down type functionality, but with my own Frame instead of a panel. My component contains a class member of the JFrame derived control that I would like it to show. In certain situations, when setVisible is called, the contents of this JFrame are not painted. This appears to be happening when I am trying to display the JFrame on my left most monitor, which uses negative x coordinates (my primary monitor is the middle monitor). The strange thing is this issue appears only on my Windows 7 machine, but not on an XP machine.

Here is a very basic sample that demonstrates the problem. As you can see, it is very basic sample that should simply hide and display the DropFrame. I have omitted the code from initComponents, in this case all it does it add a button to each frame and the necessary ActionListeners for each button.

Code:

public class NewJFrame extends javax.swing.JFrame {
private javax.swing.JButton jButton2;
private DropFrame f = new DropFrame();

/** Creates new form NewJFrame */
public NewJFrame() {
    initComponents();
}

private void initComponents() {
    //Create button and add it to the frame...
    pack();
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)        {                                         
    SwingUtilities.invokeLater(new Runnable() 
    {
        public void run() {
            Point p = jButton2.getLocationOnScreen();
            f.setLocation(p.x, p.y + 25);
            f.setVisible(true);
        }
    });

}                                        

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
} }

public class DropFrame extends javax.swing.JFrame {
private javax.swing.JButton jButton1;  

/** Creates new form NewJFrame1 */
public DropFrame() {
    initComponents();
}

private void initComponents() {
    //Create button and add to frame...
    pack();
}                   

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)    {                                         
    this.setVisible(false);
} }                                                  

The problem does not exist if I create a new DropFrame on each button click, as opposed to reusing and setting the visibility of the same Frame, but this is not desired. Any ideas on why my DropFrame is not painted sometimes?

A: 

Here are a couple of things to look at:

#1 What version of the JVM are you running? Sun make a lot of changes to the graphics pipeline in the different releases of 1.6. Pre update 10 releases behave quite differently from later releases. (Note: Firefox 3.6+ requires update 10 or higher for applets to work.)

#2 In your init components add these lines:

    Point location = getLocation();
    setLocation(new Point(0, 0));
    setLocation(location);
    pack();

The call to setLocation() eventually Component.notifyNewBounds(boolean resized, boolean moved), which traverses the component hierarchy setting each component's bounds.

By default this is done "lazily" which seems to cause Java some issues when calculating where a component is (or where it should be). The above code forces the bounds to be computed up front.

#3 If you're running u10 or higher, start your application with the argument

-Dsun.java2d.d3d=false

This disables the DirectX pipeline. If that makes the problem go away, update your display drivers.

Devon_C_Miller
I am using 1.6 u18.Forcing bounds calculation does not seem to work, however disabling Direct3D does! I suppose it is time to update my drivers to see if they fix the issue.Thanks for the help!
MMann
A: 

I've had some similar problems with a JOGL application. The main window had the JOGL rendering on a JPanel with Java2D overlay, and some gui elements too. Those gui elements were buttons for opening some additional JFrame dialogs, which showed up, but they were empty - white... after a random number of resizing te content showed up...

My problems were gone when after I've deleted this argument: -Dsun.java2d.opengl=True

The strange thing is, this argument speeds up my JOGL rendering 2-3 times, on Ubuntu 10.04, openSUSE 11.3, some solaris, and Win 7 too, and it doesn't messes up the JFrame dialogs anywhere, only on Win 7. Maybe it's because my Radeon 3870 ( I have the newest drivers )

scarab