Results 1 to 2 of 2
  1. #1
    shanthi is offline Member
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default Unable to load an image on JPanel

    Hi All...I am facing a problem of getting an image loaded on JFrame instead of JPanel..Can anyone please help me out in this regard ...below is my code...Thanks in Advance...


    package steg;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import javax.imageio.*;
    import java.awt.image.*;

    public class Layout extends JFrame implements ActionListener
    {
    JPanel pane1=new JPanel();
    JButton load=new JButton("Load");
    String filename;
    BufferedImage image = null;
    Layout()
    {

    JMenuBar menubar=new JMenuBar();
    setJMenuBar(menubar);

    JMenu file=new JMenu("File");
    JMenu edit=new JMenu("Edit");
    JMenu browse=new JMenu("Browse");
    JMenu help=new JMenu("Help");

    menubar.add(file);
    menubar.add(edit);
    menubar.add(browse);
    menubar.add(help);


    pane1.setLayout(new BorderLayout(20,20));


    JPanel p2 = new JPanel();
    p2.setLayout(new FlowLayout());

    JButton embed=new JButton("Embed");
    JButton encrypt=new JButton("Encrypt");
    JButton ok=new JButton("OK");
    JButton editP=new JButton("Edit");
    JButton save=new JButton("Save");
    JButton cancel=new JButton("Cancel");

    p2.add(load);
    p2.add(embed);
    p2.add(encrypt);

    p2.add(editP);
    p2.add(ok);
    p2.add(save);
    p2.add(cancel);

    load.addActionListener(this);
    Container c = getContentPane();
    c.setLayout(new BorderLayout());
    c.add(pane1, BorderLayout.CENTER);
    c.add(p2, BorderLayout.SOUTH);



    }
    public static void main(String a[])
    {
    Layout lay= new Layout();
    lay.setTitle("Steganography");
    lay.setSize(500, 500);
    lay.setVisible(true);



    }

    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()== load)
    {

    int confirm=JOptionPane.showConfirmDialog(pane1, "Please load an image");
    if(confirm == JOptionPane.YES_OPTION)
    {
    System.out.println("In Dailog");
    JFileChooser fileChooser = new JFileChooser();
    int ret = fileChooser.showDialog(null, "Open file");
    if(ret == JFileChooser.APPROVE_OPTION)
    {
    File selfile=fileChooser.getSelectedFile();
    try
    {
    image=ImageIO.read(selfile);
    pane1.add(new Load(image));
    }
    catch( IOException io ){
    io.printStackTrace();
    }
    }

    }
    }
    }

    class Load extends JPanel
    {
    BufferedImage bi;

    Load(BufferedImage img)
    {
    bi=img;
    System.out.println("inside");
    }
    @Override
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    g.drawImage(bi, 0, 0, this);
    }
    }

    }

  2. #2
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Why does it have to be in a JFrame instead of the JPanel? And why are you drawing the image manually? Why not just load it in an ImageIcon, then add that to the JFrame.getContentPane() or JPanel?

Similar Threads

  1. Load image to jPanel
    By DJIT in forum AWT / Swing
    Replies: 7
    Last Post: 12-27-2010, 01:48 PM
  2. Load website on a JPanel.
    By Masochist in forum New To Java
    Replies: 2
    Last Post: 05-11-2010, 08:44 PM
  3. Unable to render printing the complete JPanel
    By Y. Progammer in forum New To Java
    Replies: 6
    Last Post: 03-12-2010, 03:18 PM
  4. Unable to load Oracle Driver
    By shantanu.rathore in forum NetBeans
    Replies: 1
    Last Post: 02-12-2010, 09:34 AM
  5. Replies: 1
    Last Post: 06-19-2009, 11:47 AM

Posting Permissions

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