• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

using same actionlistener exit button not working

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
using tool and breakpoint when you click button.
 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic