Why are my JFrame contents not being painted sometimes?
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 Dro开发者_运维技巧pFrame 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?
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.
精彩评论