Results 1 to 2 of 2
  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    122
    Rep Power
    0

    Default How to add a JPanel with a background image to an already set-up GUI

    I am trying to set a picture in the background of my GUI. I had already made the GUI with the all the required buttons and labels. So I was trying to separately make a class which extends JPanel and add a picture to a panel by overriding the paintComponent() method, and then added it to a frame (I did not set the default layout of the frame, so it was the default...), and it worked very fine. Here is the code:

    Java Code:
     private BufferedImage image;
    
        public ParentPanel() {
            try {
                image = ImageIO.read(getClass().getResource("/com/dev_nna/scheduler/UI/1405188.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }                      
        
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
        }
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(1000, 1000);
        }
        
    	}
    Then I needed to add this panel with background picture to the background frame of my already made frame with all the buttons on it.The layout of the parent frame of my GUI was BorderLayout(50, 50), panels (with buttons) were added to it n the north and south positions.

    Then I tried to add a JPanel with the background image to the parent frame (of my GUI), (I wanted it to elapse the entire frame, and come under all the buttons and controls, which were only present in the north and south positions of the border layout), and then the panels holding the buttons to the north and south postions of this panel, everything disappeared.


    If I don't do anything except adding this panel with the background image to the parent frame, it only takes up the place not taken up by the two panels on the frame (Border Layout)


    So please guide me on how to add this panel to the parent frame of my GUI such that it elapses the entire parent frame's background, and the panels containing the buttons should sit on it.

    IMPORTANT PARTS OF CODE:-
    Java Code:
    //TOP LEFT PANEL
      JPanel topleftpanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
    	    parentPanel_top.add(topleftpanel);
    	    topleftpanel.setOpaque(false); 
    
    // TOP RIGHT PANEL
    JPanel toprightpanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
    	    parentPanel_top.add(toprightpanel);
    	    toprightpanel.setOpaque(false); 
    
    // TOP PANEL CONTAINING TOP LEFT AND TOP RIGHT PANELS
    JPanel parentPanel_top = new JPanel(new GridLayout(1, 2));
    	    getContentPane().add(parentPanel_top, BorderLayout.NORTH);
    	    parentPanel_top.setOpaque(false); 
    
    
    
    // PANEL WITH BUTTONS AT THE BOTTOM
    public ParentFrame() {
    		getContentPane().setLayout(new BorderLayout(50, 50));
    		
    		JPanel parentPanel_bottom = new JPanel();
    		getContentPane().add(parentPanel_bottom, BorderLayout.SOUTH);
    		parentPanel_bottom.setOpaque(false); 
    		
    
    
    // MAIN: PARENT FRAME CREATED HERE. new ParentPanel() is the one with the background image
    public static void main(String[] args){
    		ParentFrame frame1= new ParentFrame();
    		frame1.getContentPane().add(new ParentPanel());  //  <--- add it here
            frame1.setTitle("Scheduler 1.0 beta");
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame1.pack();
            frame1.setLocationRelativeTo(null);
            frame1.setVisible(true);
    	}
    Attached Thumbnails Attached Thumbnails How to add a JPanel with a background image to an already set-up GUI-urm.png   How to add a JPanel with a background image to an already set-up GUI-urm.png  
    Last edited by Zarah; 04-30-2014 at 01:33 PM.

  2. #2
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    122
    Rep Power
    0

    Default Re: How to add a JPanel with a background image to an already set-up GUI

    This problem is resolved. I had to add the panels containing the buttons to the panel containing the background picture and that panel to the JFrame.

Similar Threads

  1. how to set background image to jpanel
    By sharmapiyush in forum New To Java
    Replies: 17
    Last Post: 04-10-2014, 03:16 PM
  2. JPanel with background image
    By pflerman in forum AWT / Swing
    Replies: 3
    Last Post: 09-22-2011, 10:47 PM
  3. Can JDesktopPane can have a background JPanel?
    By chyrl in forum AWT / Swing
    Replies: 13
    Last Post: 10-22-2010, 07:56 AM
  4. background color with jpanel
    By hannerz06 in forum New To Java
    Replies: 6
    Last Post: 03-31-2010, 04:25 AM
  5. how to put a background image in a JPanel
    By yanipao in forum New To Java
    Replies: 5
    Last Post: 10-21-2009, 03:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •