I am trying to make a save button but I don't know how to do it. I will explain a little bit about my program. When someone start the program it shows a JOptionPane and the user need to write a nickname and then when he clicks on OK button it saves his nickname in a text.file. This is a part from my code.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.*;
import javax.swing.event.*;
public class SummerExamProject extends JFrame {
JButton Exit, About, Question1, Question2, Question3, Question4;
JPanel panel, panel2, panel3, panel4 ;
public SummerExamProject(){
initUI();
}
public void initUI(){
setTitle("Java");
setSize(500, 450);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JFrame frame = new JFrame();
JFrame frame1 = new JFrame("");
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
getContentPane().add(panel);
panel.setLayout(new GridLayout(0,3,8,9));
String code = JOptionPane.showInputDialog(frame1, " Enter a nickname ", "Nickname needed", JOptionPane.WARNING_MESSAGE);
If you want to store nickname to file you can use PrintWriter. Lets say you saved the nickname in
String
variable called nickname: