-2

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);
                }


            }
        }
    }
}
  • 2
    Well thanks for that 436 lies of code dump Don't you think it might be more productive to try and sort these types of things in a short program *first?* – Andrew Thompson Jan 01 '14 at 12:34
  • BTW - please stop stuffing up my edits to the question! They are intended to help people be able to read it. Use the `edit` link immediately below the post, in future. – Andrew Thompson Jan 01 '14 at 12:36
  • its a sample only dear , if i made it short then its hard to understand – user3151146 Jan 01 '14 at 12:37
  • @Andrew Thompson sorry i was editing spelling of word i wrote it mistake ... – user3151146 Jan 01 '14 at 12:38
  • *"if i made it short then its hard to understand"* Do you really think people are qoing to go trawling through over 400 LOC just to help with a general question?!? – Andrew Thompson Jan 01 '14 at 12:38
  • As an aside, if you want to show someone an example, it does not take over 400 LOC. E.G. See [this answer](http://stackoverflow.com/a/7778332/418556) in which I successfully serialize and restore the settings of a user app. in just ***81 lines of self contained code.*** – Andrew Thompson Jan 01 '14 at 12:42
  • ..This is so begging for more down-votes. – Andrew Thompson Jan 01 '14 at 12:44
  • @Andrew Thompson your advice is much appreciated , I've edited the code just now for more simple ! – user3151146 Jan 01 '14 at 12:44
  • And in doing so, have largely wiped out the edits I made. You are either not listening carefully, misunderstanding what I am saying, or deliberately trying to irritate me. I no longer care which it is.. – Andrew Thompson Jan 01 '14 at 12:47
  • @Andrew Thompson but sir , you said to me use the edit button to edit ! don't you ? I'm sorry but I'm really confused now ! dont know which edit button you meant ! – user3151146 Jan 01 '14 at 12:50
  • MS Access? Who would use that when you could be using SQLite or Hypersonic or Derby? Silly. – duffymo Jan 01 '14 at 13:14
  • because i tried to use mysql over mac os x, its complicated for now ! so i want to test it on microsoft access at the beginning . – user3151146 Jan 01 '14 at 13:16
  • *"..you said to me use the edit button to edit ! don't you ?"* My bad. The loss of one minor edit made me think you were still using the old way of editing. – Andrew Thompson Jan 01 '14 at 13:28

2 Answers2

1

Store the preferences in some serializable object (say a POJO). Serialize the object (anyway you like) and store it in a single DB field (which is e.g. of type LONGBINARY). On the reverse way, load the DB field, deserialize your objecy, and then use your object again.

While the app is running you can keep track if the user changed at least one of their preferences. If so - save the object to the DB e.g. when the user quits the app (when the app is shutting down).

The good thing is that you can relate that DB field to a user account. This way any user can have its own set of preferences, which are nicely persisted in the DB, and which can be loaded from there any time (e.g. when the app is starting up or when the user logs in).

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
1

I think the database should use a class that extends the class java.util.prefs.Preferences. This is easier to store in a file. Use listeners to be notified when settings change.

Vinz243
  • 9,654
  • 10
  • 42
  • 86