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 3759930
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:23:35+00:00 2026-05-19T10:23:35+00:00

I have a program that takes a screenshot of my gui. It automatically saves

  • 0

I have a program that takes a screenshot of my gui. It automatically saves the .gif file to the eclipse project directory. What I would like is to have it asking a user where to save the image. Basically so the user can browse the file directory and choose the directory.
Here’s the code I have:

public void actionPerformed(ActionEvent event) {
try{
       String fileName = JOptionPane.showInputDialog(null, "Save file", 
null, 1);

       if (!fileName.toLowerCase().endsWith(".gif")){
         JOptionPane.showMessageDialog(null, "Error: file name must end with \".gif\".",
           null, 1);
       }
       else{             
         BufferedImage image = new BufferedImage(panel2.getSize().width, 
           panel2.getSize().height, BufferedImage.TYPE_INT_RGB);
         panel2.paint(image.createGraphics());
         ImageIO.write(image, "gif", new File(fileName));
         JOptionPane.showMessageDialog(null, "Screen captured successfully.",
null, 1);
       }
}
catch(Exception e){}
  • 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-19T10:23:35+00:00Added an answer on May 19, 2026 at 10:23 am

    I would use a file chooser dialog instead of a JOptionPane. Here is a link for the tutorial.

    Example:
    First of all you have to declare JFileChooser object in your class and initialize it.

    public Class FileChooserExample{
       JFileChooser fc;
       FileChooserExample(...){
          fc = new JFileChooser();// as a parameter you can put path to initial directory to open
          ...
       }
    

    Now create another method:

    private String getWhereToSave(){
        int retVal = fc.showSaveDialog(..);
        if(retVal == JFileChooser.APPROVE_OPTION){
            File file = fc.getSelectedFile();
            return file.getAbsolutePath();
        }
        return null;
    }
    

    This method returns to you the absolute path which user selected. retVal indicates which button was pressed (Save or Cancel). And if it was pressed Save then you handle the selected file.

    Then you have this method you can incorporate this with your code. Instead of this line:

    String fileName = JOptionPane.showInputDialog(null, "Save file", null, 1);
    

    Write:

    String fileName = getWhereToSave();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a program that takes some time creating pdf files i would like
I have a perl program that takes input and output file arguments, and I'd
I have a program that saves an image in a local directory and then
I have this program that takes one argument for the source file and then
I have a program that takes a text file with values for example: 20
I have a program that takes a list of names from a file and
I have a program that takes in a data file of students that have
I have a C# program that takes a legacy report file and maps to
I have to create a program that takes an input file of unsorted numbers
Currently I have a program that takes pictures and saves them as a jpg

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.