Note that there are some explanatory texts on larger screens.

plurals
  1. POJava JFileChooser opens back up after clicking open in the chooser dialog
    text
    copied!<p>When I use the <code>JFileChooser</code> 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.</p> <p>when you click "open" in the dialog it changes my background and then the <code>JFileChooser</code> dialog opens up again. could anyone tell me what i need to do to make that not happen??</p> <p>here's all of my source below..</p> <pre><code> 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); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload