I'm working on a java application for text editing , what i am facing problem at is :
I have JTextArea
and the user change some setting like changing the font or the color for the text in the text area . So I want to save these settings into the database and retrieve it when the file is opened again.
Is another way! How to know that the user changed the settings and how to save it into the database as string or what exactly! is it related to properties library in Java?
I'm using Microsoft access and database and here is sample of my application.
package texteditor;
public class TextEditor extends JFrame implements ActionListener, ItemListener {
String s1;
File ff;
final static String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
static Statement stmt = null;
static Connection con = null;
static ResultSet rs = null;
static String filename = "C:\\Users\Seth\\Desktop\\Settings.accdb";
static String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + filename;
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == Open) {
JFileChooser f = new JFileChooser();
f.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int x = f.showOpenDialog(null);
if (x != JFileChooser.CANCEL_OPTION) {
try {
ff = f.getSelectedFile();
Scanner s = new Scanner(ff);
while (s.hasNext()) {
String name = s.nextLine();
Text1.append(name + "\n");
}
String y = ff.getName();
setTitle(y);
s.close();
} catch (FileNotFoundException eee) {
JOptionPane.showMessageDialog(rootPane, "File not found ! \n" + eee.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
try {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException ex) {
Logger.getLogger(TextEditor.class.getName()).log(Level.SEVERE, null, ex);
}
con = DriverManager.getConnection(url, "", "");
stmt = con.createStatement();
rs = stmt.executeQuery("select * from Pref");
while (rs.next()) {
s1 = rs.getString("FileName");
if (s1.contentEquals(ff.getName())) {
System.out.println("it is exists");
} else {
}
}
} catch (SQLException ex) {
Logger.getLogger(TextEditor.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (e.getSource() == Save) {
JFileChooser g = new JFileChooser();
g.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int y = g.showSaveDialog(null);
if (y != JFileChooser.CANCEL_OPTION) {
try {
File t = g.getSelectedFile();
String name = t.getCanonicalPath();
if (!name.endsWith(".txt")) {
t = new File(name + ".txt");
}
PrintWriter w = new PrintWriter(new FileOutputStream(t, true));
StringTokenizer st = new StringTokenizer(Text1.getText(), "\n");
while (st.hasMoreTokens()) {
w.println(st.nextToken());
}
String r = t.getName();
setTitle(r);
w.close();
} catch (FileNotFoundException ee) {
JOptionPane.showMessageDialog(rootPane, "File not found ! \n" + ee.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
} catch (IOException ex) {
JOptionPane.showMessageDialog(rootPane, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
}
public static void main(String[] args) {
TextEditor t1 = new TextEditor();
t1.show();
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException ex) {
Logger.getLogger(TextEditor.class.getName()).log(Level.SEVERE, null, ex);
}
try {
con = DriverManager.getConnection(url, " ", " ");
System.out.println("Connection Established Succesfully");
} catch (SQLException ex) {
Logger.getLogger(TextEditor.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (!(stmt == null)) {
try {
con.close();
stmt.close();
} catch (SQLException ex) {
Logger.getLogger(TextEditor.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}