Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Add image in panel using swings

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 68
    Comment on it

    Hi All,

    To add image as background image in JPanel ,JLabel is used for example:-

            setLayout(new BorderLayout());
            try {                
                image =  ImageIO.read(new File("D:\\New folder (2)\\fish.jpg"));
    
             } catch (IOException ex) {
                  // handle exception...
                 System.out.println("error");
             }
            JLabel picLabel = new JLabel(new ImageIcon(image));
            picLabel.setLayout(null);
            picLabel.setLocation(0, 0);
            picLabel.setSize(750, 100);
            picLabel.setVisible(true);
    
            add(picLabel,BorderLayout.NORTH);
    

    In this first i assigned border layout to parent container then add JLabel in BorderLayout.NORTH which means at top of parent controller.

    Using same you can assign image to JButton as follow:-

           JButton button = new JButton(new ImageIcon(buttonIcon));
            button.setBorder(BorderFactory.createEmptyBorder());
            button.setContentAreaFilled(false);
            button.setBounds(5,72,24,24);
            picLabel.add(button);
    

    Now i added JButton with image. SetBound method is used to give location and size to JButton

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: