| |

VerySource

 Forgot password?
 Register
Search
View: 1355|Reply: 11

Save my notepad!

[Copy link]

2

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-3-18 09:00:02
| Show all posts |Read mode
I wrote a simple notepad using JAVA. Can anyone tell me how to determine whether the content in JTextArea has changed?
Reply

Use magic Report

3

Threads

10

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-6-20 15:30:01
| Show all posts
change event
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-6-27 12:30:01
| Show all posts
Add an ActionListener
Reply

Use magic Report

2

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-6-28 14:00:01
| Show all posts
Can you be more specific, is it ChangeListener, but there is no addChangeListener in JTextArea
Reply

Use magic Report

0

Threads

21

Posts

19.00

Credits

Newbie

Rank: 1

Credits
19.00

 Ireland

Post time: 2020-6-29 19:30:01
| Show all posts
The java.awt.TextArea could be monitored for changes by adding a TextListener for TextEvents. In the JTextComponent based components, changes are broadcasted from the model via a DocumentEvent to DocumentListeners. The DocumentEvent gives the location of the change and the kind of change if desired. The code fragment might look something like:

    DocumentListener myListener = ??;
    JTextArea myArea = ??;
    myArea.getDocument().addDocumentListener(myListener);

api original

http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JTextArea.html
Reply

Use magic Report

2

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-7-2 16:00:01
| Show all posts
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;


class NotepadJFrame extends JFrame {
JScrollPane jsp;
JTextArea jta;
boolean motified = false;
String currentFileName = null;
The
NotepadJFrame() {
setProp();
addComponent();
this.setVisible(true);
}
The
private void setProp() {
this.setSize(500, 400);
this.setLocation(200, 100);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setTitle("My Notepad");
this.setJMenuBar(new NoteMenuBar(this));
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
close();
}
});
}
The
private void addComponent() {
jsp = new JScrollPane();
jta = new JTextArea();
jsp.getViewport().add(jta);
this.getContentPane().add(jsp, BorderLayout.CENTER);
jta.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
motified = true;
}
public void removeUpdate(DocumentEvent e) {
motified = true;
}
public void changedUpdate(DocumentEvent e) {
motified = true;
}
});
}
The
void close() {
if(motified) {
int rst = JOptionPane.showConfirmDialog(null, "Do you want to save");
switch(rst){
case JOptionPane.YES_OPTION:
save();
break;
case JOptionPane.NO_OPTION: System.exit(0);
case JOptionPane.CANCEL_OPTION:
}
} else {
System.exit(0);
}
}
The
void open() {
JFileChooser jfc = new JFileChooser();
jfc.showOpenDialog(null);
File f = jfc.getSelectedFile();
try {
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String tmp;
StringBuffer all = new StringBuffer();
while(!(tmp = br.readLine()).trim().equals("")) {
all.append(tmp);
all.append("\n");
}
br.close();
fr.close();
this.jta.setText(all.toString());
}catch(IOException e) {
e.printStackTrace();
}
}
The
void save() {
try {
if(currentFileName==null) {
saveAsFile();
}
FileWriter fw = new FileWriter(this.currentFileName);
BufferedWriter bw = new BufferedWriter(fw);
String[] all = this.jta.getText().split("\n");
for(int i=0;i<all.length;i++) {
bw.write(all[i]);
bw.newLine();
}
bw.flush();
bw.close();
fw.close();
motified=false;
}catch(IOException e) {
e.printStackTrace();
}
}
The
void saveAsFile() {
JFileChooser chooser = new JFileChooser("c:/");
chooser.showSaveDialog(null);
this.currentFileName = chooser.getSelectedFile().getPath();
save();
}
The
void newFile() {
if(motified) {
int rst = JOptionPane.showConfirmDialog(null, "Do you want to save");
switch(rst){
case JOptionPane.YES_OPTION:
save();
break;
case JOptionPane.NO_OPTION:
case JOptionPane.CANCEL_OPTION:
}
}
jta.setText("");
motified = false;
}
}
Reply

Use magic Report

2

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-7-2 17:15:01
| Show all posts
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


class NoteMenuBar extends JMenuBar {
private JMenu menuFile;
private JMenuItem[] items;
private NotepadJFrame njf;
NoteMenuBar(NotepadJFrame njf) {
super();
this.njf = njf;
addMenuContent();
}
The
private void addMenuContent() {
menuFile = new JMenu("File");
this.add(menuFile);
items = new JMenuItem[5];
String[] strItem = {"New", "Open", "Save", "Save As", "Close"};
for (int i = 0; i <items.length; i ++) {
items[i] = new JMenuItem(strItem[i]);
if ("Close".equals(strItem[i])) {
menuFile.addSeparator();
}
items[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String strCmd = e.getActionCommand();
if("New".equals(strCmd)) {
njf.newFile();
} else if ("open".equals(strCmd)) {
njf.open();
} else if("Save".equals(strCmd)) {
njf.save();
} else if("Save as".equals(strCmd)) {
njf.saveAsFile();
} else if ("Close".equals(strCmd)) {
njf.close();
}
}
});
menuFile.add(items[i]);
}
}
}
Reply

Use magic Report

2

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-7-2 18:15:01
| Show all posts
A simple notepad, let's take a look and give some advice, see if it can be modified better
Reply

Use magic Report

0

Threads

21

Posts

19.00

Credits

Newbie

Rank: 1

Credits
19.00

 China

Post time: 2020-7-10 20:30:01
| Show all posts
Increase timing automatic save
Reply

Use magic Report

2

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-8-2 04:00:01
| Show all posts
I want to add a cut, copy, and paste menu item, and how to create a right-click menu. The expert will give you directions
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list