0

I have a program, with JFrame and JInternalFrames inside. So, when I try to set background with this code:

 BufferedImage myImage;
 myImage = ImageIO.read(new File("C:/5/JavaLibrary2/background.jpg"));
 ImagePanel Image = new ImagePanel(myImage);
 frame.setContentPane(Image);

My JInternalFrames just gone. So, see a short video with debug

frame.setContentPane(Image); just delete my JInternal windows.

JohnDow
  • 1,242
  • 4
  • 22
  • 40
  • 1
    *"So, see a short video.."* Does it have John Cleese in it? Otherwise, no thanks. For better help sooner, post an [SSCCE](http://sscce.org/). Also, what is your question? – Andrew Thompson Nov 12 '12 at 14:06
  • Who is JC? BTW, my problem >>> My JInternalFrames just gone – JohnDow Nov 12 '12 at 14:08
  • See [John Cleese](http://en.wikipedia.org/wiki/John_Cleese) (funny guy). – Andrew Thompson Nov 12 '12 at 14:11
  • Video with debug mean VIDEO WITH DEBUG – JohnDow Nov 12 '12 at 14:15
  • 1
    have you tried `frame.add(Image);` becuase you are overwriting the old contentPane which held the `JInternalFrame`s when you call: `frame.setContentPane(Image); `. BTW Java variable naming conventions state variables should begin with lower case only. i.e `imagePanel` – David Kroukamp Nov 12 '12 at 14:20
  • 2
    Perhaps I should point out, I was being sarcastic. A non-sarcastic way to put that is. "Few people will follow external links, and I (one of the few who will occasionally follow links) don't have enough bandwidth to spare, to watch a video in order to help. For better help sooner, post an [SSCCE](http://sscce.org/)." – Andrew Thompson Nov 12 '12 at 14:20
  • To David Kroukamp : No effect,program work perfect, but doesnt change background. – JohnDow Nov 12 '12 at 14:23
  • 1
    See also this [example](http://stackoverflow.com/a/8090328/230513). – trashgod Nov 12 '12 at 15:09
  • 1
    @VladislavIl'ushin after that try than calling `revalidate()` and `repaint()` on `JFrame` instance. – David Kroukamp Nov 12 '12 at 18:27
  • @DavidKroukamp nope. Nothing :( – JohnDow Nov 12 '12 at 18:39
  • 3
    as @AndrewThompson said please post an [SSCCE](http://sscce.org) thats the only way now – David Kroukamp Nov 12 '12 at 18:40

1 Answers1

1

I have no issue.

enter image description here

I've used both a JLabel and custom painting routines.

I've created and setup the internal frames BEFORE adding them to the desktop,

I've added the internal frame to the desktop and THEN changed their content panes as well as throwing the update to the end of the EDT to ensure that the main frame is visible.

The problem must be in some part of your code you're not showing us.

public class TestInternalFrame {

    public static void main(String[] args) {
        new TestInternalFrame();
    }

    public TestInternalFrame() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JDesktopPane desktop = new JDesktopPane();

                final JInternalFrame frame1 = new JInternalFrame("Image on Label");
//                frame1.setContentPane(new LabelImagePane());
//                frame1.pack();
//                frame1.setLocation(0, 0);
                frame1.setVisible(true);
                desktop.add(frame1);

                final JInternalFrame frame2 = new JInternalFrame("Painted Image");
//                frame2.setContentPane(new ImagePane());
//                frame2.pack();
//                frame2.setLocation(frame1.getWidth(), 0);
                frame2.setVisible(true);
                desktop.add(frame2);


                JFrame frame = new JFrame("I Haz Images");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(desktop);
                frame.setSize(800, 400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        frame1.setContentPane(new LabelImagePane());
                        frame1.pack();
                        frame1.setLocation(0, 0);
                        frame2.setContentPane(new ImagePane());
                        frame2.pack();
                        frame2.setLocation(frame1.getWidth(), 0);
                    }
                });

            }
        });
    }

    public class LabelImagePane extends JPanel {

        public LabelImagePane() {
            setLayout(new BorderLayout());
            JLabel label = new JLabel();
            add(label);

            try {
                label.setIcon(new ImageIcon(ImageIO.read(new File("..."))));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public class ImagePane extends JPanel {

        private BufferedImage image;

        public ImagePane() {
            try {
                image = ImageIO.read(new File("..."));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return image == null ? super.getPreferredSize() : new Dimension(image.getWidth(), image.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (image != null) {
                int x = (getWidth() - image.getWidth()) / 2;
                int y = (getHeight() - image.getHeight()) / 2;
                g.drawImage(image, x, y, this);
            }
        }
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366