简体   繁体   中英

Button not creating new file when pressed in Java Swing application

So this is just a window with a button, when that button is pressed a new internal window opens with three textboxes and an ok button. when the ok button in the internal window is pressed, it should create a new file, but it's not, does anybody know why?

import javax.swing.*;
import java.io.*;
import java.util.*;
import java.awt.BorderLayout;
import java.awt.event.*;

public class tuna extends JFrame {

    private JButton button1, ok;
    private JTextField nameOfSong, artist;
    private JTextArea lyrics;
    JDesktopPane desktop;
    JInternalFrame internalFrame;

    Formatter x;

    public tuna() {
        super("iLyrics");

        desktop = new JDesktopPane();
        getContentPane().add(desktop, BorderLayout.CENTER);

        button1 = new JButton("Add a Song");
        getContentPane().add(button1, BorderLayout.NORTH);

        thehandler handler = new thehandler();
        button1.addActionListener(handler);

    }

    private class thehandler implements ActionListener{
        public void actionPerformed(ActionEvent event){
            if(event.getSource()==button1)
                internalGui();
        }
    }

    public void internalGui(){
        // Add a Song

        JInternalFrame internalFrame = new JInternalFrame(
                "Internal Frame", true, true, true, true);
        internalFrame.setBounds(200, 200, 200, 200);
        desktop.add(internalFrame, JLayeredPane.DEFAULT_LAYER);
        // desktop.add(internalFrame);
        final JTextField nameOfSong = new JTextField("song");
        final JTextField artist = new JTextField("artist");
        final JTextArea lyrics = new JTextArea("lyrics");
        JButton ok = new JButton("Ok");
        internalFrame.add(nameOfSong, BorderLayout.BEFORE_FIRST_LINE);
        internalFrame.add(artist, BorderLayout.AFTER_LINE_ENDS);
        internalFrame.add(lyrics, BorderLayout.CENTER);
        internalFrame.add(ok, BorderLayout.SOUTH);
        ok.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        try {
                            FileWriter dir = new FileWriter("test.txt");
                            BufferedWriter buffer = new BufferedWriter(dir);
                            buffer.write(nameOfSong.getText());
                            buffer.newLine();
                            buffer.write(artist.getText());
                            buffer.newLine();
                            buffer.write(lyrics.getText());
                            buffer.close();
                        } catch (Exception z) {
                            // TODO Auto-generated catch block
                            System.err.println("Error: " + z.getMessage());
                        }

                    }
                });
        internalFrame.setVisible(true);
    }
}

but it's not, does anybody know why?

It probably does create it, in the current working directory - may not be what you want. Check there (if using IDE, see launch properties for directory), or try with an absolute path first "c:\\\\tmp\\\\myfile.txt", "/home/myhome/myfile.txt"..

Follow up Q from comment:

i want the name of the file to be the text from nameOfSong.getText()... new FileWriter("nameOfSong.getText().txt"), it wont make

new FileWriter(nameOfSong.getText() + ".txt" ) 

as per another comment.

I'll bet that it does create the file, but that you may be looking in the wrong place for it. Do you know where it's writing the file?

Put this line of code in your program to find out:

System.out.println(System.getProperty("user.dir"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM