Search code examples
javaswingjpanelpaintcomponentjinternalframe

JInternalFrame image bag


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.


Solution

  • 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);
                }
            }
        }
    }