Jak zrobić, aby program przy wyłączaniu sprawdzał czy wprowadzono zmiany do tekstu wcześniej zapisanego (także pytał czy zapisać, gdy sami tego nie zrobimy). To znaczy ma działać dokładnie tak jak notatnik czy inny edytor tekstu.
Konkretnie chodzi mi o metodę windowClosing.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StreamTokenizer;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Notatnik extends JFrame implements ActionListener, WindowListener{
JMenuItem nowy=new JMenuItem("Nowy"), zapisz=new JMenuItem("Zapisz"), zapiszjako=new JMenuItem("Zapisz jako"), wczytaj=new JMenuItem("Wczytaj"), wyjscie=new JMenuItem("Exit");
JPanel panel=new JPanel();
JPanel panel2=new JPanel();
JMenuBar menuBar=new JMenuBar();
JMenu opcje=new JMenu("Opcje");
JTextArea area=new JTextArea(35, 60);
JScrollPane scrollArea=new JScrollPane(area,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JComboBox cb;
JCheckBox cb1,cb2;
JButton B, BB, BBB;
JTextField pole;
Color color1=Color.black, col1=Color.black, color2=Color.white, col2=Color.white;
String plik="";
JFileChooser fc;
int w=16;
File f=new File(plik);
public Notatnik() throws HeadlessException {
super();
setTitle("Notatnik");
setSize(700,700);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(BorderLayout.CENTER,panel);
add(BorderLayout.NORTH,panel2);
JRootPane root=getRootPane();
root.setJMenuBar(menuBar);
menuBar.add(opcje);
opcje.add(nowy);
opcje.add(zapisz);
opcje.add(zapiszjako);
opcje.add(wczytaj);
opcje.add(wyjscie);
panel.add(scrollArea, BorderLayout.CENTER);
area.setLineWrap(true);
nowy.addActionListener(this);
zapisz.addActionListener(this);
zapiszjako.addActionListener(this);
wczytaj.addActionListener(this);
wyjscie.addActionListener(this);
Guziki(this);
addWindowListener(this);
}
public Notatnik(GraphicsConfiguration arg0) {
super(arg0);}
public Notatnik(String arg0) throws HeadlessException {
super(arg0);}
public Notatnik(String arg0, GraphicsConfiguration arg1) {
super(arg0, arg1);}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
new Notatnik();
}
@Override
public void actionPerformed(ActionEvent e) {
Object source=e.getSource();
if(source==nowy){
area.setText("");
area.setBackground(Color.white);
area.setForeground(Color.black);
}
if(source==zapisz){
Save();
}
if(source==zapiszjako){
Saveas();
}
if(source==wczytaj){
OpenFile();
}
if(source==wyjscie){
String tekst="";
if(plik!="") {
try {
StreamTokenizer token=new StreamTokenizer(new FileReader(plik));
while(token.nextToken()!=StreamTokenizer.TT_EOF)
tekst=tekst+token.sval + " ";
}
catch(IOException ex) {
JOptionPane.showMessageDialog(this, "Błąd2.");
System.exit(1);
}
}
if(tekst==area.getText())
System.exit(1);
else {
int a = JOptionPane.showConfirmDialog(this,"Czy zapisac zmiany?","Wyjscie", JOptionPane.YES_NO_CANCEL_OPTION);
if(a==1)
System.exit(1);
if(a==0) {
if(plik!="")
Save();
else
Saveas();
System.exit(1);
}
}
}
//dla guziczkow
if(source == BB){
w=Integer.parseInt(pole.getText());
}
int Charakter=Font.PLAIN;
String styl="Agency FB";
if(cb1.isSelected())
Charakter=Font.BOLD;
else if(cb2.isSelected())
Charakter=Font.ITALIC;
else if(cb1.isSelected() && cb2.isSelected())
Charakter=Font.ITALIC + Font.BOLD;
if(source==cb)
styl=cb.getSelectedItem().toString();
Font czcionka=new Font(styl,Charakter,w);
area.setFont(czcionka);
}
public void Save() {
f=new File(plik);
if(plik!=null){
try {
BufferedWriter bfw=new BufferedWriter(new FileWriter(f.getAbsolutePath()));
bfw.write(area.getText());
bfw.close();
}
catch (IOException ex) {
Saveas();
}}
else return;
}
public void Saveas()
{
fc=new JFileChooser(".");
int i=fc.showSaveDialog(this);
if(i==JFileChooser.APPROVE_OPTION) {
f=fc.getSelectedFile();
plik=f.getAbsolutePath();
try {
BufferedWriter bfw=new BufferedWriter(new FileWriter(f.getAbsolutePath()));
bfw.write(area.getText());
bfw.close();
}
catch (IOException ex) {
JOptionPane.showMessageDialog(this, "Błąd.");
System.exit(4);
}
}
}
public void OpenFile(){
fc = new JFileChooser(".");
if (JFileChooser.APPROVE_OPTION==fc.showOpenDialog(this))
try
{
f=fc.getSelectedFile();
setTitle(f.getAbsolutePath());
plik=f.getAbsolutePath();
BufferedReader br = new BufferedReader(new FileReader(f));
String temp="";
while (br.ready())
{
temp+=br.readLine()+"\n";
}
area.setText(temp);
}
catch (IOException ex)
{
System.out.println("Brak pliku");
}
}
public void Guziki(JFrame frame) {
String[] Czcionka = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
cb=new JComboBox(Czcionka);
cb.addActionListener(this);
panel2.add(cb);
BB=new JButton("OK");
BB.addActionListener(this);
pole=new JTextField(2);
panel2.add(pole);
panel2.add(BB);
cb1=new JCheckBox("Pogrubienie");
cb2=new JCheckBox("Kursywa");
cb1.addActionListener(this);
cb2.addActionListener(this);
panel2.add(cb1);
panel2.add(cb2);
B=new JButton("KOLOR");
//Anonimowa klasa wewnetrzna
B.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
col1=color1;
color1=JColorChooser.showDialog(null, "wybierz kolor", color1);
if(color1==null)color1=col1;
area.setForeground(color1);
}
});
panel2.add(B);
BBB=new JButton("TLO");
//Anonimowa klasa wewnetrzna
BBB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
col2=color2;
color2=JColorChooser.showDialog(null, "wybierz kolor", color2);
if(color2==null)color2=col2;
area.setBackground(color2);
}
});
panel2.add(BBB);
setVisible(true);
}
public void windowClosing(WindowEvent arg0) {
String tekst="";
if(plik!="") {
try {
StreamTokenizer token=new StreamTokenizer(new FileReader(plik));
while(token.nextToken()!=StreamTokenizer.TT_EOF)
tekst=tekst+token.sval + " ";
}
catch(IOException ex) {
JOptionPane.showMessageDialog(this, "Błąd2.");
System.exit(1);
}
}
if(tekst==area.getText())
System.exit(1);
else {
int a = JOptionPane.showConfirmDialog(this,"Czy zapisac zmiany?","Wyjscie", JOptionPane.YES_NO_CANCEL_OPTION);
if(a==1)
System.exit(1);
if(a==0) {
if(plik!="")
Save();
else
Saveas();
System.exit(1);
}
}
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
}