1 Replies - 1069 Views - Last Post: 08 November 2011 - 09:38 AM Rate Topic: ***-- 2 Votes

#1 rosebrit3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 02-November 11

Properties File in Java

Posted 08 November 2011 - 07:31 AM

Hai all,

I am doing a Project where I have to create an Application that contains a GUI. The user has to enter the details for the textfields. These has to be stored in a Properties File and then the value that the user has entered the previous time have to be loaded again the next time the user opens the GUI. Since, its a Project, I have modularised the code into classes. I am writing the code below.

It contains a Frame, contianing a menubar, on clicking a menuitem a dialog window is opened, which consists of a TabbedPane. In the First Tab, called DatabaseOptions, the user enters the name and value. These two are the textfields which I want to store in the Properties file and retrieve them again on loading the GUI.

MAIN.JAVA
    public class Main {
    public static void main (String args[])
    {
        MainMenu v=new MainMenu();
        v.setVisible(true);
    }
}



MAINMENU.JAVA
    public class MainMenu extends javax.swing.JFrame {

    Properties props=new Properties();
    
    /** Creates new form MainMenu */
    public MainMenu() {
        
        initComponents();
    }
                          
    private void initComponents() {

        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
         private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt)                                                
     {  
            ReportOptions mf=new ReportOptions(this, true);
            mf.setVisible(true);
            UserInformation ui=new UserInformation();
            ui.getValues();     
    }        



REPORTOPTIONS.JAVA

public class ReportOptions extends javax.swing.JDialog {

    /** Creates new form ReportOptions */
    public ReportOptions(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
        addOptionPage(new DatabaseOptions(), "DatabaseOptions");
        addOptionPage(new TableOptions(), "ReportOptions");

   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       UserInformation ui=new UserInformation();
        try {
            ui.storeValues();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ReportOptions.class.getName()).log(Level.SEVERE, null, ex);
    
   public void addOptionPage(JPanel p, String title ) {
    
        this.jTabbedPane1.addTab(title, p );        
    }




DATABASEOPTIONS.JAVA

public class DatabaseOptions extends javax.swing.JPanel {

    /** Creates new form DatabaseOptions */
   
    public DatabaseOptions() {
       
        initComponents();
    }

 private void initComponents() {

        NameLabel = new javax.swing.JLabel();
        ValueLabel = new javax.swing.JLabel();
        NameText = new javax.swing.JTextField();
        ValueText = new javax.swing.JTextField();
}



Now, I have created anew class called FileInformation, contianing two methods, getInformation() and setInformation(). I want these methods to contain the code for getting and setting the key and value pairs in the Properties file. Can anyone help me in guiding me how to do this, please?

Thank You all.

Is This A Good Question/Topic? 0
  • +

Replies To: Properties File in Java

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10664
  • View blog
  • Posts: 39,608
  • Joined: 27-December 08

Re: Properties File in Java

Posted 08 November 2011 - 09:38 AM

Check out this File I/O Tutorial. Also, please avoid the NetBeans GUI Builder. It produces horrid, unmaintainable code that humans can neither read nor maintain. Write your code by hand instead.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1