Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 312783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:02:52+00:00 2026-05-12T08:02:52+00:00

When I use the JFileChooser in my project that I am working on everything

  • 0

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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:02:52+00:00Added an answer on May 12, 2026 at 8:02 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A typical way to use JFileChooser includes checking whether user clicked OK, like in
Can a JApplet use a JFileChooser so that the user can select a file
Use case: I've just entered insert mode, and typed some text. Now I want
Use case: class MyTask(Task): queue = 'default_queue' def run(self): # do work Normally I
I'm working on an application that visualizes datasets which are stored in directories ending
I've created a JFileChooser which I use to locate a directory for a file
To display all the contents of a folder of .jpg files, should I use
Use Eclipse Classic with ADT plugin. Tried to make project from existing example of
To create an Arabic JFileChooser (RTL) I use the following: MyFileChooser: import javax.swing.JOptionPane; import
I was just wondering: how does Gmail use the Windows/Mac file chooser to upload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.