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?