Window won't paint.
I have a JFrame with a JPanel and a single label in it. If I call it from within another JFrame, the contents of the window are transparent - paintComponent does not seem to get called. If I add it's own main, and call it outside the other UI, it works fine.
The code below is for full screen mode, but I have tried it as a small window and decorated with same results - the JPanel component does not seemt o get drawn.
package org.ed4becky.fpa.ui.album;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.ed4becky.fpa.ui.image.Portrait;
public class MyFrame extends JFrame {
private static final long serialVersionUID = 1L;
public MyFrame(BufferedImage img) {
super();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel jp = new JPanel();
jp.setLayout(new BoxLayout(jp,BoxLayout.Y_AXIS));
if(img == null)
{
JLabel jl = new JLabel("Hello");
jp.add(jl);
}
jp.add(new Portrait(img));
setContentPane(jp);
setUndecorated(true);
setVisible(true);
GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().setFullScreenWindow(this);
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
} catch (final ClassNotFoundException e) {
e.printStackTrace();
} catch (final InstantiationException e) {
e.printStackTrace();
} catch (final IllegalAccessException e) {
e.printStackTrace();
} catch (final UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
final MyFrame thisClass = new MyFrame(null);
thisClass.setVisible(true);
thisClass.requestFocus();
}
});
}
}
# 2
I'll repost with formatting (And stripped of irrelevant code).
Calling main, all is well. Calling from with another JFrame, it comes up blank:
package org.ed4becky.fpa.ui.album;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MyFrame extends JFrame {
private static final long serialVersionUID = 1;
public MyFrame(BufferedImage img) {
super();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel jp = new JPanel();
jp.setLayout(new BoxLayout(jp,BoxLayout.Y_AXIS));
JLabel jl = new JLabel("Hello");
jp.add(jl);
setContentPane(jp);
setVisible(true);
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
} catch (final ClassNotFoundException e) {
e.printStackTrace();
} catch (final InstantiationException e) {
e.printStackTrace();
} catch (final IllegalAccessException e) {
e.printStackTrace();
} catch (final UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
final MyFrame thisClass = new MyFrame(null);
thisClass.setVisible(true);
thisClass.requestFocus();
}
});
}
}
The 'parent' frame (last method, showSlides())...
package org.ed4becky.fpa.ui.album;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.Collection;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.ed4becky.fpa.beans.Image;
import org.ed4becky.fpa.ui.image.FullFrame;
import org.ed4becky.fpa.ui.image.Portrait;
import org.ed4becky.fpa.ui.image.SpringUtilities;
public class SlideShowUI extends JFrame implements ActionListener {
protected static Log log = LogFactory.getLog(SlideShowUI.class.getName());;
private static final long serialVersionUID = 1;
private JPanel jContentPane = null;
private JPanel jButtonPanel = null;
private JButton jShowButton = null;
private JButton jCancelButton = null;
private JPanel jInputPanel = null;
private JLabel jLabelCycle;
private JLabel jLabelTimeInterval;
private JTextField jTextFieldTimeInterval;
private JCheckBox cycleChk;
private Collection<Image> images = null;
/**
* This method initializes jButtonPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJButtonPanel() {
if (jButtonPanel == null) {
jButtonPanel = new JPanel();
jButtonPanel.setLayout(new BoxLayout(getJButtonPanel(),
BoxLayout.X_AXIS));
jButtonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10,
10));
final JLabel jSpacer3 = new JLabel();
jSpacer3.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10));
final JLabel jSpacer2 = new JLabel();
jSpacer2.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10));
final JLabel jSpacer1 = new JLabel();
jSpacer1.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10));
jButtonPanel.add(jSpacer1);
jButtonPanel.add(getJShowButton(), null);
jButtonPanel.add(jSpacer2);
jButtonPanel.add(getJCancelButton(), null);
jButtonPanel.add(jSpacer3);
}
return jButtonPanel;
}
/**
* This method initializes jSaveButton
*
* @return javax.swing.JButton
*/
private JButton getJShowButton() {
if (jShowButton == null) {
jShowButton = new JButton();
jShowButton.setText("Start Show");
jShowButton.addActionListener(this);
}
return jShowButton;
}
private JButton getJCancelButton() {
if (jCancelButton == null) {
jCancelButton = new JButton();
jCancelButton.setText("Cancel");
jCancelButton.addActionListener(this);
}
return jCancelButton;
}
/**
* This method initializes jInputPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJInputPanel() {
if (jInputPanel == null) {
jLabelTimeInterval = new JLabel();
jLabelTimeInterval.setText("Seconds between slides: ");
jLabelTimeInterval.setMaximumSize(new Dimension(125, 16));
jLabelTimeInterval.setPreferredSize(new Dimension(125, 16));
jLabelTimeInterval.setAlignmentX(Component.RIGHT_ALIGNMENT);
jLabelCycle = new JLabel();
jLabelCycle.setText("Cycle at end");
jLabelCycle.setMaximumSize(new Dimension(75, 16));
jLabelCycle.setPreferredSize(new Dimension(75, 16));
jLabelCycle.setAlignmentX(Component.RIGHT_ALIGNMENT);
final SpringLayout layout = new SpringLayout();
jInputPanel = new JPanel();
jInputPanel.setLayout(layout);
jInputPanel.setBorder(BorderFactory
.createEmptyBorder(10, 10, 0, 10));
jInputPanel.add(jLabelTimeInterval, null);
jInputPanel.add(getJTextFieldTimeInterval(), null);
jLabelTimeInterval.setLabelFor(jTextFieldTimeInterval);
cycleChk = new JCheckBox();
jInputPanel.add(jLabelCycle, null);
jInputPanel.add(cycleChk, null);
SpringUtilities.makeCompactGrid(jInputPanel, 2, 2, // rows, cols
6, 6, // initX, initY
1, 1); // xPad, yPad
}
return jInputPanel;
}
private Component getJTextFieldTimeInterval() {
if (jTextFieldTimeInterval == null) {
jTextFieldTimeInterval = new JTextField();
jTextFieldTimeInterval.setMaximumSize(new Dimension(75, 20));
jTextFieldTimeInterval.setPreferredSize(new Dimension(75, 20));
}
return jTextFieldTimeInterval;
}
/**
* This is the default constructor
*/
public SlideShowUI(Collection<Image> images) {
super();
this.images = images;
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setContentPane(getJContentPane());
this.setSize(new Dimension(400, 250));
setPreferredSize(new Dimension(400, 250));
this.setLocation(new Point(200, 50));
setTitle("Slide Show");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BoxLayout(getJContentPane(),
BoxLayout.Y_AXIS));
jContentPane.add(getJInputPanel(), null);
jContentPane.add(getJButtonPanel(), null);
}
return jContentPane;
}
public void actionPerformed(ActionEvent e) {
log.debug("Entering actionPerformed (Add) " + e.getActionCommand());
final Object c = e.getSource();
boolean returnParent = true;
if (c == jShowButton) {
showSlides();
returnParent = false;
} else if (c == jCancelButton) {
executeCancelAction();
}
if (returnParent) {
setVisible(false);
}
log.debug("Leaving actionPerformed " + e.getActionCommand());
}
private void showSlides() {
int seconds = 0;
String tmpSecs = jTextFieldTimeInterval.getText();
if(tmpSecs != null
&& tmpSecs.trim().length() != 0)
{
seconds = Integer.parseInt(tmpSecs);
}
do {
for (Image img : this.images) {
log.debug("Showing " + img.getDescr());
MyFrame frame = new MyFrame(img.getImage());
try {
Thread.sleep(1000 * seconds);
} catch (InterruptedException e) {
break;
}
if(!frame.isVisible())
break;
frame.dispose();
}
} while(cycleChk.isSelected());
}
private void executeCancelAction() {
}
}