tags:

views:

276

answers:

2

When I use the JFileChooser in my project that i am working on everything works just like it is supposed to work with no problems at all except for one.

when you click "open" in the dialog it changes my background and then the JFileChooser dialog opens up again. could anyone tell me what i need to do to make that not happen??

here's all of my source below..


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class COS extends JPanel implements ActionListener{
    static JFrame f=new JFrame();
    static Image bgImage=null;
    static String message="";
    JButton chbg=new JButton("change background");
    public COS(){
    }
    public void paintComponent(Graphics g){
     if(bgImage!=null){
      g.drawImage(bgImage,0,0,this);
      chbg.setBounds(10,10,150,25);
      chbg.addActionListener(this);
      add(chbg);
     }
     else{
      g.drawString(message,40,40);
     }
    }
    public static void loadbg(){
     try{
      String xmlpath="background.xml";
      DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
      try{
       String fimg="";
       DocumentBuilder db=dbf.newDocumentBuilder();
       Document dom=db.parse(xmlpath);
       dom.getDocumentElement().normalize();
       NodeList ndlst=dom.getElementsByTagName("background");
       Node firstnd=ndlst.item(0);
       if(firstnd.getNodeType()==Node.ELEMENT_NODE){
        Element firstele=(Element)firstnd;
        NodeList firstnamenodelist=firstele.getElementsByTagName("bgimage");
        Element firstnamele=(Element)firstnamenodelist.item(0);
        NodeList firstname=firstnamele.getChildNodes();
        fimg=((Node) firstname.item(0)).getNodeValue();
       }
       getFileImage(fimg);
      } catch(Exception e){
      }
     } catch(Exception e){
      message="File load failed: "+e.getMessage();
     }
    }
    public static void getFileImage(String filein) throws IOException, InterruptedException{
     FileInputStream in=new FileInputStream(filein);
     byte[] b=new byte[in.available()];
     in.read(b);
     in.close();
     bgImage=Toolkit.getDefaultToolkit().createImage(b);
    }
    public void actionPerformed(ActionEvent e){
     Object source=e.getSource();
     JFileChooser jfc=new JFileChooser();
     if(source==chbg){
      int returnVal=jfc.showOpenDialog(null);
      if(returnVal==JFileChooser.APPROVE_OPTION){
       File file=jfc.getSelectedFile();
       String fileone=file.getName();
       changebg(fileone);
      }
     }
    }
    public void changebg(String filein){
     try{
      getFileImage(filein);
      saveDefaultImage(filein);
      repaint();

     } catch(IOException e){
     } catch(InterruptedException ie){
     }
    }
    public void saveDefaultImage(String filein){
     String newdefbg=filein;
     //don't mind this method, i am still working on it...
    }
    public static void main(String[] args){
     COS newcos=new COS();
     loadbg();
     f.setSize(825,640);
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     f.getContentPane().setLayout(null);
     newcos.setBounds(5,5,800,600);
     f.setLocation(10,5);
     f.getContentPane().add(newcos);
     f.setVisible(true);
    }
}
+2  A: 

Simply because you add new action listener with every repaint. Paint method is ONLY for painting and nothing else. You have to rethink your strategy.

eugener
that didn't help, when i take repaint() out from the changebg method the JFileChooser still acts the same exact away.
Lonnie Ribordy
No, he means move the .addActionListener() call to the constructor rather than the paintComponent() method. You keep adding more and more instances of 'this' to the button's actions listeners list and so they are being called sequentially. Only add the listener once per instance.
Chris Boran
oooh, i see, thankyou =]
Lonnie Ribordy
+1  A: 

thank you for everyone's help =] problem completely solved now, though i suspect i'll probably be back asking more soon >.>

Lonnie Ribordy
If there is an answer that helped, you should mark it as accepted. That's usually a pretty good way to say thank you :)http://meta.stackoverflow.com/questions/5234/accepting-answers-what-is-it-all-about
Arnold Spence
haha, thanks.. i'm new to stackoverflow. i wasn't sure how to do it. =]
Lonnie Ribordy