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