AndreiL
Welcome to AndreiL.
Enjoy your time.
Please log in.

Join the forum, it's quick and easy

AndreiL
Welcome to AndreiL.
Enjoy your time.
Please log in.
AndreiL
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Text editor: simple form

Go down

Text editor: simple form Empty Text editor: simple form

Post by Lucaci Andrei Sat Jan 26, 2013 12:26 am

Here's a simple-based text editor, which allows you to create/open/save new files.


  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class TE extends JFrame{
private static final long serialVersionUID = 1L;
private JTextArea tx = new JTextArea();
final JFileChooser fc = new JFileChooser();
private JButton open = new JButton("Open");
private JButton save = new JButton("Save");
private JScrollPane jsp;
public TE(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
tx.setTabSize(2);
jsp = new JScrollPane(tx);
JPanel mn = new JPanel();
jsp.setPreferredSize(new Dimension(800, 550));
JPanel ms = new JPanel(new GridBagLayout());
ms.add(open);
ms.add(save);
fc.setCurrentDirectory(new java.io.File("."));
mn.add(new JSplitPane(JSplitPane.VERTICAL_SPLIT, jsp, ms));
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == open) {
int returnVal = fc.showOpenDialog(TE.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
if (file.canRead()){
rds(file);
}else{
JOptionPane.showMessageDialog(null, "Can't open file.");
}
}
}
}
public void rds(File SourceFile){
tx.setText("");
Scanner inputs;
try {
inputs = new Scanner(SourceFile);
while(inputs.hasNext()){
tx.append(inputs.nextLine()+"\n");
}
inputs.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
});
save.addActionListener(new ActionListener() {
private JDialog diag = new JDialog(TE.this);
private JLabel name_lbl = new JLabel("Filename: ");
private JTextField name_ln = new JTextField();
private JButton sav = new JButton("Save");

public void actionPerformed(ActionEvent arg0) {
sav.addActionListener(new sv());
name_ln.addActionListener(new sv());
JPanel mn = new JPanel(new GridBagLayout());
name_ln.setPreferredSize(new Dimension(200, 20));
mn.add(name_lbl);
mn.add(name_ln);
mn.add(sav);
diag.add(mn);
diag.pack();
diag.setVisible(true);
}

class sv implements ActionListener{
public void actionPerformed(ActionEvent e) {
if (name_ln.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Field empty");
return;
}
BufferedWriter bw;
try {
String choosertitle = "";
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle(choosertitle);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(TE.this) == JFileChooser.APPROVE_OPTION) {
JOptionPane.showMessageDialog(null, chooser.getCurrentDirectory());
bw = new BufferedWriter(new FileWriter(chooser.getCurrentDirectory()+"\\"+name_ln.getText()));
bw.write(tx.getText());
bw.close();
JOptionPane.showMessageDialog(null, "Saved at\n"+chooser.getCurrentDirectory()+"\\"+name_ln.getText());
}
else if (chooser.showOpenDialog(TE.this)==JFileChooser.CANCEL_OPTION){return;}
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, e1.getMessage());
}
}
}
});
mn.setPreferredSize(new Dimension(800, 600));
add(mn);
}

public static void main(String arg[]){
TE t= new TE();
t.pack();
t.setVisible(true);
}
}

Lucaci Andrei
Lucaci Andrei
"Tata Lor"
Text editor: simple form 138

Number of messages : 222
Points : 2266743
Reputation : 1007
Registration date : 2008-08-15
Age : 31
Location : Cluj-Napoca

http://www.andreil.wgz.ro

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum