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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:38:22+00:00 2026-06-15T09:38:22+00:00

I’m a java beginner and I tried making a basic program that will delete

  • 0

I’m a java beginner and I tried making a basic program that will delete a certain file in the temp files in Windows. It did delete the file without a problem when I hadn’t implemented the JPanel & JFrame but I haven’t had any luck since. It is supposed to delete the file when the “Delete for sure” jbutton is pressed and exit the program when the “exit” jbutton is pressed. All it does right now is bring up the GUI and nothing else. Not even system out prints. Here is the code:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;


    /**
    * Created with IntelliJ IDEA.
    * User: Andrew
    * Date: 12/4/12
    * Time: 7:09 PM
    * To change this template use File | Settings | File Templates.
    */

    public class DeleteFile {

    public static void main (String args[]) throws IOException {
        frame.setVisible(true);
        frame.setName(boxname);
        frame.setSize(100, 150);
        frame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        button1.setText(buttontext);
        button1.setVisible(true);
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
        class Action1 implements ActionListener {
            public void actionPerformed (ActionEvent e) {
                deleteFile();
                JLabel label = new JLabel("Deletion was successful");
                JPanel panel = new JPanel();
                panel.add(label);
            }
        }
        class Action2 implements ActionListener {
            public void actionPerformed (ActionEvent e) {

            }
            public void windowEvent (WindowEvent e) {
                System.exit(0);
            }
        }

        JPanel panel = new JPanel();
        frame.add(panel);
        JButton button = new JButton("Delete for sure?");
        panel.add(button);
        button.addActionListener (new Action1());
        panel.setName(boxname);

        JButton button2 = new JButton("Exit");
        panel.add(button2);
        button2.addActionListener (new Action2());

       // JLabel label = new JLabel(filePath);
       // panel.add(label);




    }






    static String buttontext = "Delete file for sure?";
    static String boxname = "Trepix Temp File Deleter";
    static String filePath = "C:\\Users\\Andrew\\AppData\\Local\\Temp\\CamRec0\\cursor-1.ico";
    static JFrame frame = new JFrame();
    static JButton button1 = new JButton();
    static JPanel panel = new JPanel();







    public static boolean fileIsValid() {
        File file = new File(filePath);

        if (file.exists()) {
            return true;


        } else {
            return false;
        }

    }

    public static void deleteFile() {
        if (fileIsValid() == true) {
            File file = new File(filePath);
            file.delete();

        }
    }




}
  • 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-06-15T09:38:23+00:00Added an answer on June 15, 2026 at 9:38 am
        class Action1 implements ActionListener {
            public void actionPerformed (ActionEvent e) {
                deleteFile();
                JLabel label = new JLabel("Deletion was successful");
                JPanel panel = new JPanel();
                panel.add(label);
            }
        }
    

    The panel object is never placed in any container that is part of a hierarchy leading to a top-level window. In other words, it’s not placed in anything that is sitting in either a JFrame or JDialog, and so it will never be displayed.

        class Action2 implements ActionListener {
            public void actionPerformed (ActionEvent e) {
    
            }
            public void windowEvent (WindowEvent e) {
                System.exit(0);
            }
        }
    

    It makes no sense to place this windowEvent method in an ActionListener, since that is part of a WindowListener, something completely different. Why not simply call System.exit(0); in the actionPerformed(...) method?

    Also your code shouldn’t have any static fields or methods as that is antithetical to object-oriented programming.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace

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.