• Post Reply Bookmark Topic Watch Topic
  • New Topic

using same actionlistener exit button not working

 
malini keshar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is my prog..for selecting & displaing image.the code is given bellow.
rest of all is running pefectally.but EXIT button is not working Insted it shows that dialog box to choose the image.

import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.awt.image.*;
public class PictureFrame extends JFrame implements ActionListener
{


Image img;

JButton getPictureButton;
JButton exitButton;

public static void main(String[] args)
{
//System.out.println("Hello World!");
new PictureFrame();
}
public PictureFrame()
{
this.setSize(300,300);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//clickListener cl = new clickListener();
JPanel picPanel=new PicturePanel();
this.add(picPanel,BorderLayout.CENTER);

JPanel buttonPanel = new JPanel();
getPictureButton = new JButton("Select Cover Image");
getPictureButton.addActionListener(this);
buttonPanel.add(getPictureButton);


exitButton=new JButton("Exit");
exitButton.addActionListener(this);
buttonPanel.add(exitButton);

this.add(buttonPanel,BorderLayout.SOUTH);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String file=getImageFile();
if (file!=null)
{
Toolkit kit =Toolkit.getDefaultToolkit();
img=kit.getImage(file);
}
else if(e.getSource() == exitButton)
{
System.exit(0);
}

}


private String getImageFile()
{
JFileChooser fc = new JFileChooser();
//fc.set
int result=fc.showOpenDialog(null);
File file=null;
if (result==JFileChooser.APPROVE_OPTION)
{
file=fc.getSelectedFile();
return file.getPath();
}else
return null;
}

private class PicturePanel extends JPanel
{
public void paint(Graphics g)
{

if(img!=null)
{
g.drawImage(img,0,0,this);
}
}
}
private class ImageFilter extends javax.swing.filechooser.FileFilter
{
public boolean accept(File f)
{
if (f.isDirectory())
return true;
String name=f.getName();
if(name.matches(".*((.jpg)|(.gif)|(.png))"))
return true;
else
return false;
}
public String getDescription()
{
return "Image files(*.jpg,*.gif.*.png)";
}


}
}
please help me in this.this is very urgent.
 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like you need to rework your actionPerformed method. I'd suggest adding some System.out.println methods and see what is really going on in that method. This will tell you whether what you think is happening is really happening.
 
Kia Phia Ben
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using tool and breakpoint when you click button.
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
If you take a closer look at the actionPerformed() method then you will notice that your if-else structure is not proper. Work on it again.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!