I am writing a java.swing screensaver using the NetBeans GUI builder. I have recently tried using the FullScreen Exclusive mode in order to make it look better but now my images are not displayed at all.
I am displaying images using a jLabel and the setIcon method and cycling through them using a swing timer.
Here is the code below:
public class AdFrame extends javax.swing.JFrame {
ActionListener changeImage;
Timer timer;
GraphicsDevice thispc;
Window myWindow;
/**
* Creates new form FifthFrame
*/
public AdFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.GridBagLayout());
jPanel1.setOpaque(false);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/btc_gui/newpackage/btc-zg.jpg"))); // NOI18N
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(41, 41, 41)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
getContentPane().add(jPanel1, new java.awt.GridBagConstraints());
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(AdFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(AdFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(AdFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(AdFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
AdFrame ff1 = new AdFrame();
ff1.setExtendedState(JFrame.MAXIMIZED_BOTH);
ff1.setVisible(true);
ff1.thispc = ff1.getGraphicsConfiguration().getDevice();
ff1.myWindow = new Window(ff1);
ff1.thispc.setFullScreenWindow(ff1.myWindow);
ff1.repaint();
String[] filearray = new String[2];
filearray[0] = "/btc_gui/newpackage/btc-zg.jpg";
filearray[1] = "/btc_gui/newpackage/pic2.jpeg";
ff1.changeImage = new ChangeImageListener(ff1.jLabel1,filearray);
ff1.timer = new Timer(5000,ff1.changeImage);
ff1.timer.start();
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
Look at the documentation of setFullScreenWindow
:
Exclusive mode implies:
- […] All other application windows will always appear beneath the full-screen window in the Z-order.
…
In your code you create a new Window
and make that the full screen window:
AdFrame ff1 = new AdFrame();
…
ff1.myWindow = new Window(ff1);
ff1.thispc.setFullScreenWindow(ff1.myWindow);
So this new Window
will be the topmost and your AdFrame
will be beneath that, hence invisible, just as the documentation specifies.
If you want to make your AdFrame
instance the full screen window, you should do exactly that instead of creating another window:
AdFrame ff1 = new AdFrame();
ff1.setUndecorated(true);
ff1.setExtendedState(JFrame.MAXIMIZED_BOTH);
ff1.setVisible(true);
ff1.thispc = ff1.getGraphicsConfiguration().getDevice();
ff1.thispc.setFullScreenWindow(ff1);
Note that I also added an setUndecorated(true)
call as the documentation recommends.