[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Setting text into JTextField by typing

Posted on 2011-03-15
91
Medium Priority
?
726 Views
Last Modified: 2012-05-11
Please advice how to set text value into JTextField by typing on it?
0
Comment
Question by:gbcbr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 43
  • 25
  • 16
  • +1
91 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35136710
What's the problem? It should worked unless setEnabled(false) has been called on it
0
 
LVL 92

Expert Comment

by:objects
ID: 35136717
first you need to give your text field focus
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35136726
(if it didn't have focus you wouldn't be typing on it)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:gbcbr
ID: 35136727
Now I have:
support_EURUSD.setPreferredSize(new Dimension(80, 30));
        support_EURUSD.setBackground(new Color(255, 132, 132));
        support_EURUSD.setHorizontalAlignment(JTextField.CENTER);
        support_EURUSD.setText("1.3924"); // Support level
        support_EURUSD.setFont(new Font("Lucida Grande", 0, 12));

Open in new window

What I have to add to set text from GUI
0
 

Author Comment

by:gbcbr
ID: 35136739
I can type, but restart app return old value. I want to change value setText("1.3924") and keep it
0
 
LVL 92

Expert Comment

by:objects
ID: 35136741
>         support_EURUSD.setText("1.3924"); // Support level

that line will set the text of your text field

if you need to change it then just call setText() again to change it
0
 
LVL 92

Expert Comment

by:objects
ID: 35136751
> I can type, but restart app return old value. I want to change value setText("1.3924") and keep it

then you need to store that value somewhere, and then set it using that stored value

        support_EURUSD.setText(storedValue); // Support level

0
 

Author Comment

by:gbcbr
ID: 35136769
>>then you need to store that value somewhere, and then set it using that stored value
How to store it?
0
 
LVL 92

Expert Comment

by:objects
ID: 35136778
database, properties file, preferences, ... many options :)
0
 

Author Comment

by:gbcbr
ID: 35136786
So, it is no Action listener for typing?
0
 

Author Comment

by:gbcbr
ID: 35136852
Anyway I don't understand how I can type new value in GUI text field and this value will be saved somewhere, which method will manage reading from this field and saving in properties file?
0
 
LVL 7

Expert Comment

by:rodness
ID: 35137672
JTextField will not save this value between program runs.
You will need to do this yourself.

How you store this value is going to be specific to your application.
0
 

Author Comment

by:gbcbr
ID: 35137768
>>  is going to be specific to your application.
what you mean - specific?
It's the common, standard case, I just type value into text field and for me doesn't matter in which way to save it.
I don't understand who listen my typing. If I understand this I can, build some logic from this point.
For example:
When I press button I have:
chartButton_EURUSD.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    try {
                        jToggleButton3_mouseClicked(e);
                    } catch (Exception f) {
                        f.printStackTrace();
                    }
                }

Open in new window

and I can create procedure what to do, but for typing I have no idea how to manage it.
0
 
LVL 7

Expert Comment

by:rodness
ID: 35137867
The best way is to add a JButton next to the text field, the user enters text and clicks the button, and in the button ActionListener you do something with the text.

You can also add an ActionListener to the JTextField, but it will not get called UNLESS the user presses the Enter key.  This is probably not obvious enough, I would add the JButton instead.
0
 

Author Comment

by:gbcbr
ID: 35138378
I don't think that to add button for me is good solution.
I have JFrame with 10 JPanels in rows with 15 elements each, 5 of them have to be editable by typing, so I have to add 50 JButtons?
It will destroy my working panel, because now all elements are informative, and this 50 buttons will sit there just for pishing 3 times a day.
0
 

Author Comment

by:gbcbr
ID: 35138411
May be possible to create one button SAVE and connect all JTextFields to this button?
0
 
LVL 7

Expert Comment

by:rodness
ID: 35138492
You can certainly do that, yes.  Whatever works best for your application.
0
 

Author Comment

by:gbcbr
ID: 35141146
So, it's some other ideas about this question.
In general, I don't need to save values in properties file during application work, I need to save them when I exit.
But stil I have no idea how to get value from these textfields, some method has to read them and set them into properties file and after set them back.
0
 
LVL 7

Expert Comment

by:rodness
ID: 35141162
There is no automatic way to save the value in a textfield and restore it when you exit the program.  You will have to write this yourself.

To simply get the text value from a textfield, call textfield.getText().  This will return a String with the value of the text field.
0
 

Author Comment

by:gbcbr
ID: 35141337
>> call textfield.getText().
I'm already have these 50 return methods which return values of this 50 text fields.
How to write them in property file?
0
 
LVL 7

Expert Comment

by:rodness
ID: 35141384
0
 

Author Comment

by:gbcbr
ID: 35141400
I read it yesterday, it's just common information, I need advice for my specific case.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35141403
Would this be an auto-save or an explicit one?
0
 

Author Comment

by:gbcbr
ID: 35141411
I prefer auto-save on exit.
0
 

Author Comment

by:gbcbr
ID: 35141441
Somewhere here:
menuFileExit.setText("Exit");
        menuFileExit.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    fileExit_ActionPerformed();
                }
            });
.....................
void fileExit_ActionPerformed() {
        mFxcmGateway.logout();
        System.exit(0);
    }

Open in new window

0
 
LVL 7

Assisted Solution

by:rodness
rodness earned 200 total points
ID: 35141445
Use a WindowListener on your JFrame:

jFrame.addWindowListener( new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent e) {

// save properties information here.

      }
});
0
 
LVL 7

Expert Comment

by:rodness
ID: 35141461
ps-  The advantage of doing this on the WindowListener instead of in the menu command is that if they click the window close button (instead of using the menu exit) is that you will still save the data.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35141556
>>I prefer auto-save on exit.

Use the following with Rodness' WindowListener. Give all your text fields unique names
private void saveFields(Container contentPane){
	for (Component c : contentPane.getComponents()) {
	    if(c instanceof Container) {
		saveFields((Container)c);
	    }
	    if (c instanceof JTextField) {
		JTextField tf = (JTextField)c;
		System.out.println(tf.getText()); // Uncomment next line in real one
		//props.setProperty(tf.getName(), tf.getText());
	    }
	}
    }

Open in new window

0
 

Author Comment

by:gbcbr
ID: 35141595
So, as I understood it should be something like this:
 
Properties prop = new Properties();
OutputStream out = new FileOutputStream("my.properties");
.............

private void jbInit() throws Exception {

jFrame.addWindowListener( new WindowAdapter() {

      @Override
      public void windowClosing(WindowEvent e) {
try {
     prop.setProperty("resistGBPCHF", "??????????????");
     prop.store(out, null);
} catch (IOException ex) {
     ex.printStackTrace();
} finally {
     out.close();
}


      }
});

.................

    public String getTextresistGBPCHF() {
        String resist = resistance_GBPCHF.getText();
        //        System.out.println("    DCC  resist = " + resist);
        return resist;
    }

Open in new window

But how to set value "resist"?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 35141647
>>But how to set value "resist"?


>>Give all your text fields unique names

e.g.

resistance_GBPCHF.setName("resistance_GBPCHF");
0
 

Author Comment

by:gbcbr
ID: 35141674
OK, now little bit warmer:)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35142136
It might be actually simpler to serialize the whole panel containing the fields
0
 

Author Comment

by:gbcbr
ID: 35146402
@objects
I try to follow your sample, but I have error messages at addWindowListener - method addWindowListener not found and
 out.close(); - IOException not handled
import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.IOException;

.............

public class DCC extends JFrame {

..............

private void jbInit() throws Exception {

..............

        this.getContentPane().addWindowListener(new WindowAdapter() {

                Properties prop = new Properties();
                OutputStream out = new FileOutputStream("dcc.properties");

                @Override
                public void windowClosing(WindowEvent e) {
                    try {
                        prop.setProperty("resistance_EURUSD", "eurusd_resist");
                        prop.store(out, null);
                      } catch (IOException ex) {
                          ex.printStackTrace();
                        
                    } finally {
                        out.close();                        
                      }                       
                }
            });
..................

        resistance_EURUSD.setPreferredSize(new Dimension(80, 30));
        resistance_EURUSD.setBackground(new Color(181, 255, 181));
        resistance_EURUSD.setHorizontalAlignment(JTextField.CENTER);
 //       resistance_EURUSD.setText("1.4027"); // Resistanse level
        resistance_EURUSD.setFont(new Font("Lucida Grande", 0, 12));
        resistance_EURUSD.setName("resistance_EURUSD");

.................

    public String getTextresistEURUSD() {
        String eurusd_resist = resistance_EURUSD.getText();
        //        System.out.println("    DCC  resist = " + resist);
        return eurusd_resist;
    }

Open in new window

0
 

Author Comment

by:gbcbr
ID: 35146407
@CEHG
>>simpler to serialize the whole panel

I don't know about this way, can you advice please?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35146605
Essentially it'd be

contentPane = (JPanel)oin.readObject();
setContentPane(contentPane);

at startup and

oout.writeObject(contentPane);

at close
0
 

Author Comment

by:gbcbr
ID: 35146695
@CEHJ
but where it will save it at close, it has to be some file.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35146804
Yes, into a file
0
 

Author Comment

by:gbcbr
ID: 35146812
in dcc.properties?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35150337
No, it wouldn't be a properties file, but a serialization one, say 'gui.ser'

http://www.tutorialspoint.com/java/java_serialization.htm
0
 
LVL 92

Accepted Solution

by:
objects earned 1600 total points
ID: 35152313
>  out.close(); - IOException not handled

you just need to wrap it in a tr/catch block



                @Override
                public void windowClosing(WindowEvent e) {
                    try {
                        Properties prop = new Properties();
                        OutputStream out = new FileOutputStream("dcc.properties");
                        prop.setProperty("resistance_EURUSD", "eurusd_resist");
                        prop.store(out, null);
                      } catch (IOException ex) {
                          ex.printStackTrace();
                       
                    } finally {
                        try {out.close();} catch (Exception ex() {}                        
                      }                      
                }
0
 
LVL 92

Expert Comment

by:objects
ID: 35152326
you might also want to look at the Preferences API
http://www.vogella.de/articles/JavaPreferences/article.html
makes things a little simpler in some ways
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35152521
Here's a way with serialization. Run it, type into it, close it, reopen it:
import java.awt.*;
import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;


public class GuiSaver extends JFrame {
    final static String SERIALIZATION_FILE = "gui.ser";
    private JPanel gui;
    private JTextField tf;

    private JPanel makeGui() {
        gui = null;

        File f = new File(SERIALIZATION_FILE);

        if (f.exists()) {
            ObjectInputStream in = null;

            try {
                in = new ObjectInputStream(new FileInputStream(f));
                gui = (JPanel) in.readObject();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    in.close();
                } catch (Exception e) { /* ignore */
                }
            }
        } else {
            gui = new JPanel();
            tf = new JTextField(10);
            gui.add(tf);
        }

        return gui;
    }

    private void setGui() {
        try {
            setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	    addWindowListener(new Persister());
            setContentPane(makeGui());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class Persister extends WindowAdapter {
	public void windowClosing(WindowEvent ev) {
	    persistGui();
	    System.exit(0);
	}
    }


    private void persistGui() {
        File f = new File(SERIALIZATION_FILE);

        ObjectOutputStream out = null;

        try {
            out = new ObjectOutputStream(new FileOutputStream(f));
            out.writeObject(gui);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (Exception e) { /* ignore */
            }
        }
    }

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        GuiSaver f = new GuiSaver();
                        f.setGui();
                        f.setSize(200, 200);
                        f.setVisible(true);
                    }
                });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Open in new window

0
 

Author Comment

by:gbcbr
ID: 35175729
@CEHJ
Sorry for the delay, it's not me, it's Japanese tsunami destroy whole market. So, it was no time for non-critical questions. I needed to arrange much critical questions.
Anyway, please explain me about GuiSaver class, how I can use it and how to connect to it my data?
It has a main method, means it works alone, but in my case it has to be integrated into my app.
0
 

Author Comment

by:gbcbr
ID: 35175774
@objects
Please accept the same excuse for delay.
I have this errors:
addWindowListener - method addWindowListener not found; Presist1.tiff
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35176308
>>
Anyway, please explain me about GuiSaver class, how I can use it and how to connect to it my data?
It has a main method, means it works alone, but in my case it has to be integrated into my app.
>>

It's just a demo to show what you should do with your own code. You need to serialize the panel that holds all the fields you're interested in persisting
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35176309
Did you run the demo btw?
0
 
LVL 92

Expert Comment

by:objects
ID: 35176979
you need to add the window listener to the frame, not the content pane
just use:

addWindowListener(new WindowAdapter() ......
0
 

Author Comment

by:gbcbr
ID: 35178453
@CEHJ
I run the demo, looks nice, but it's need for me some time to understand how to serialize my panel.
0
 

Author Comment

by:gbcbr
ID: 35178464
@objects
I integrate, compile and run this code, as I see it's only OutputStream which record file, but how to read and restore data?
 
private void jbInit() throws Exception {

      this.addWindowListener(new WindowAdapter() {
      
              Properties prop = new Properties();
              OutputStream out = new FileOutputStream("dcc.properties");

       @Override
      
              public void windowClosing(WindowEvent e) {
                  try {
                      Properties prop = new Properties();
                      OutputStream out = new FileOutputStream("dcc.properties");
                      prop.setProperty("resistance_EURUSD", "eurusd_resist");
                      prop.store(out, null);
                    } catch (IOException ex) {
                        ex.printStackTrace();
      
                  } finally {
                      try {out.close();} catch (Exception ex) {}
                    }
              }
      });

        this.setJMenuBar(menuBar);
        this.getContentPane().setLayout(null);

Open in new window

It has to be InputStream also, please advice how to locate it also?
0
 
LVL 92

Expert Comment

by:objects
ID: 35178478
you just need to do the reverse ie. open the property file and read the values from it
http://download.oracle.com/javase/tutorial/essential/environment/properties.html
0
 

Author Comment

by:gbcbr
ID: 35178696
I check now dcc.properties, but it's empty.
Nothing written in. ?
private void jbInit() throws Exception {

        Properties defaultProps = new Properties();
        FileInputStream in = new FileInputStream("fxmsg.properties");
        defaultProps.load(in);
        in.close();

        Properties applicationProps = new Properties(defaultProps);

        in = new FileInputStream("dcc.properties");
        applicationProps.load(in);
        in.close();

      this.addWindowListener(new WindowAdapter() {
      
              Properties prop = new Properties();
              OutputStream out = new FileOutputStream("dcc.properties");

       @Override
      
              public void windowClosing(WindowEvent e) {
                  try {
                      Properties prop = new Properties();
                      OutputStream out = new FileOutputStream("dcc.properties");
                      prop.setProperty("resistance_EURUSD", "eurusd_resist");
                      prop.store(out, null);
                    } catch (IOException ex) {
                        ex.printStackTrace();
      
                  } finally {
                      try {out.close();} catch (Exception ex) {}
                    }
              }
      });

Open in new window

0
 
LVL 92

Assisted Solution

by:objects
objects earned 1600 total points
ID: 35178950
you're writing it to the wrong output stream


              public void windowClosing(WindowEvent e) {
                      OutputStream out = null;
                  try {
                      Properties prop = new Properties();
                      out = new FileOutputStream("dcc.properties");
                      prop.setProperty("resistance_EURUSD", "eurusd_resist");
                      prop.store(out, null);
                    } catch (IOException ex) {
                        ex.printStackTrace();
     
                  } finally {
                      try {if (out!=null) out.close();} catch (Exception ex) {}
                    }
              }
      });
0
 

Author Comment

by:gbcbr
ID: 35178987
Anyway it's still empty.
I don't understand why it doesn't write at least resistance_EURUSD=,  maybe I have to clarify better value of field, but field name it's ok.
Maybe it doesn't see this dcc.properties file?
How I can check if it's visible or not?
0
 
LVL 92

Expert Comment

by:objects
ID: 35179008
make sure you're checking the right directory
it would throw an exception if it wasn't working
0
 

Author Comment

by:gbcbr
ID: 35179035
I have only one file with this name here, this what I was asking before: is it a right place where is located? DCC-pr.tiff
0
 
LVL 92

Expert Comment

by:objects
ID: 35179058
you're code would be looking/creting the file in the same directory as you are running it from
0
 

Author Comment

by:gbcbr
ID: 35179061
>>it would throw an exception if it wasn't working
I change name:
out = new FileOutputStream("dcc1.properties");

Open in new window

but it's no any exception was thrown and I can't find anywhere new created file with this name
0
 

Author Comment

by:gbcbr
ID: 35179090
OK, I make  
Properties prop = new Properties();
              OutputStream out = new FileOutputStream("dcc1.properties");

       @Override
      
              public void windowClosing(WindowEvent e) {
                    OutputStream out = null;
           
                  try {
                      Properties prop = new Properties();
                      out = new FileOutputStream("dcc1.properties");

Open in new window

and it create new file at the same place but it's also empty
DCC-pr1.tiff
0
 
LVL 92

Expert Comment

by:objects
ID: 35179104
why do you create out and prop twice? thats just going to cause you problems.
0
 

Author Comment

by:gbcbr
ID: 35179168
I delete them but file still is empty
this.addWindowListener(new WindowAdapter() {

       @Override
      
              public void windowClosing(WindowEvent e) {
                    OutputStream out = null;
           
                  try {
                      Properties prop = new Properties();
                      out = new FileOutputStream("dcc.properties");
                      prop.setProperty("resistance_EURUSD", "eurusd_resist");
                      prop.store(out, null);
                    } catch (IOException ex) {
                        ex.printStackTrace();
      
                  } finally {
                        try {
                            if (out != null)
                                out.close();
                        } catch (Exception ex) {
                        }
                    }
              }
      });

Open in new window

Maybe something not correct here
prop.store(out, null);

Open in new window

It to be some reason why it doesn't write anything.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35179300
Please attach your gui class source
0
 

Author Comment

by:gbcbr
ID: 35182927
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35183881
You can start by working the following into the code, calling it in windowClosing. Verify that the file gui.ser has been created
final static String SERIALIZATION_FILE = "gui.ser";

    private void persistGui() {
        File f = new File(SERIALIZATION_FILE);

        ObjectOutputStream out = null;

        try {
            out = new ObjectOutputStream(new FileOutputStream(f));
            out.writeObject(jPanel_EURUSD);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (Exception e) { /* ignore */
            }
        }
    }

Open in new window

0
 

Author Comment

by:gbcbr
ID: 35190404
@CEHJ
Thank you for the code.
Just please clarify where exactly to place it.
I place it inside windowClosing method but it generate errors.
this.addWindowListener(new WindowAdapter() {

       @Override      
              public void windowClosing(WindowEvent e) {
                   
                private void persistGui() {
                    
                        File f = new File(SERIALIZATION_FILE);

                        ObjectOutputStream out = null;

                        try {
                            out = new ObjectOutputStream(new FileOutputStream(f));
                            out.writeObject(jPanel_EURUSD);
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                out.close();
                            } catch (Exception e) { /* ignore */
                            }
                        }
                    }

Open in new window

Method persistGUI not used;
Type file  not found;
Type ObjectOutputStream not found and other.
For sure I place it wrong.
Please advice.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35190440
You've nested methods. windowClosing and persistGui should be separate, the former calling the latter
0
 

Author Comment

by:gbcbr
ID: 35190570
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35190660
Also

import java.io.*;

in your code or just the io classes required
0
 

Author Comment

by:gbcbr
ID: 35190958
I start and close app but file gui.ser was not created
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35191035
I'm assuming no compilation errors, so please attach new DCC.java
0
 

Author Comment

by:gbcbr
ID: 35191060
0
 
LVL 92

Expert Comment

by:objects
ID: 35194828
changing how you save it isn't going to fixs your problem.

If the files not being created then that would suggest the code is not getting called. Add some debug to check it is called
0
 

Author Comment

by:gbcbr
ID: 35196364
@objects
You are right, it doesn't call anything in windowClosing method
this.addWindowListener(new WindowAdapter() {

//       @Override
      
              public void windowClosing(WindowEvent e) {

                    System.out.println(" windowClosing = " + e);
           
                         persistGui();
              }
           
//                    OutputStream out = null;
                    
                private void persistGui() {
                    
                        File f = new File(SERIALIZATION_FILE);

                    System.out.println(" File = " + f);

                        ObjectOutputStream out = null;

                        try {
                            out = new ObjectOutputStream(new FileOutputStream(f));

                        System.out.println(" ObjectOutputStream = " + out);
                            
                            out.writeObject(jPanel_EURUSD);
                        } catch (Exception e) {
                            e.printStackTrace();

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35196413
You're not calling System.exit(0); after persistGui(); so i'm surprised you managed to close the window at all. The file gui.ser will be in the same directory from which you run the app
0
 

Author Comment

by:gbcbr
ID: 35196530
I call  System.exit(0);
from
menuFileExit.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    fileExit_ActionPerformed();
                }
            }); 

void fileExit_ActionPerformed() {
        mFxcmGateway.logout();
        System.exit(0);
    }

Open in new window

why I have to call it second time?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35196791
If you call it there your WindowListener won't get called. You need to call dispose on the gui window (DCC.this.dispose();)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35196814
Or more easily (and this is why persistGui should be a method *of your gui* (not the WindowListener))
void fileExit_ActionPerformed() {
        mFxcmGateway.logout();
        persistGui();
        System.exit(0);
}

Open in new window

0
 

Author Comment

by:gbcbr
ID: 35196887
Now it create gui.ser
void fileExit_ActionPerformed() {
        mFxcmGateway.logout();
        persistGui();
        System.exit(0);
    }

    private void persistGui() {

        File f = new File(SERIALIZATION_FILE);

        ObjectOutputStream out = null;

        try {
            out = new ObjectOutputStream(new FileOutputStream(f));
            out.writeObject(jPanel_EURUSD);
            System.out.println(" File = " + f);
            System.out.println(" ObjectOutputStream = " + out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (Exception e) { /* ignore */
            }
        }
    }

Open in new window

File = gui.ser
ObjectOutputStream = java.io.ObjectOutputStream@5d53d05b

Open in new window

but how now to call this file on the start and get information from it?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35196985
That's a little more tricky. What you need to do is study my demo carefully. You basically read jPanel_EURUSD from gui.ser if that file is present. If it's not, you construct it in the normal way
0
 
LVL 92

Expert Comment

by:objects
ID: 35197078
> You are right, it doesn't call anything in windowClosing method

then just fix that problem and you're done

> but how now to call this file on the start and get information from it?

you can't.
you should use a Properties file for that
0
 

Author Comment

by:gbcbr
ID: 35197119
@objects
>>you should use a Properties file for that
But I can't write into this file anything. It's empty and I don't understand a reason, because at my view everything was done logically.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 1600 total points
ID: 35197145
you found the reason :)  the code just wasn't getting called.
The code itself was fine

0
 

Author Comment

by:gbcbr
ID: 35197299
@objects
As always you are right, it was just invisible. I place it:
void fileExit_ActionPerformed() {
        mFxcmGateway.logout();
      windowClosing();
//        persistGui();
        System.exit(0);
    }

    public void windowClosing() {
        OutputStream out = null;

        try {
            Properties prop = new Properties();
            out = new FileOutputStream("dcc.properties");

            System.out.println(" FileOutputStream = " + out);

            prop.setProperty("resistance_EURUSD", "eurusd_resist");
            prop.store(out, null);
        } catch (IOException ex) {
            ex.printStackTrace();

        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (Exception ex) {
            }
        }
    }

Open in new window

and I have the result
#Wed Mar 23 12:28:26 EET 2011
resistance_EURUSD=eurusd_resist

Open in new window

but why I have name of value instead of value?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 1600 total points
ID: 35197346
>             prop.setProperty("resistance_EURUSD", "eurusd_resist");

should be:

            prop.setProperty("resistance_EURUSD", eurusd_resist);

(assuming thats the name of the variable containing the value you want to store
0
 

Author Comment

by:gbcbr
ID: 35197663
Now it's fine, how to get it back now?
resistance_EURUSD.setPreferredSize(new Dimension(80, 30));
        resistance_EURUSD.setBackground(new Color(181, 255, 181));
        resistance_EURUSD.setHorizontalAlignment(JTextField.CENTER);

        resistance_EURUSD.setText(??????getProperty(eurusd_resist));

        resistance_EURUSD.setFont(new Font("Lucida Grande", 0, 12));
        resistance_EURUSD.requestFocusInWindow();
        resistance_EURUSD.setName("resistance_EURUSD");

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35198185
>> prop.setProperty("resistance_EURUSD", eurusd_resist);

You only want to store *one* property?
0
 

Author Comment

by:gbcbr
ID: 35198281
@objects
I found how to get it back:
resistance_EURUSD.setText(applicationProps.getProperty("resistance_EURUSD"));

Open in new window


@CEHJ
>>You only want to store *one* property?
of course not, and your way much easy because I have only 10 JPanels, but I have to save 80 textfields, so I have big donkey job ahead. But with properties it starts to work apart of serialization. I read your sample, but I don't see how I can apply all these methods in my DCC class without destroying whole system.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35198337
>>so I have big donkey job ahead

Yes you do if you use Properties.

>> but I don't see how I can apply all these methods in my DCC class without destroying whole system.

1. write one method that returns just the base panel. Let's call it 'makeBasePanel'.
2. put all the code that currently populates jPanel_EURUSD into it
3. in your code, call it as below.

This btw is a good thing to do in any case, as it modularises your code
jPanel_EURUSD = makeBasePanel();

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35198356
Step 2. should largely  be a cut and paste job (back up first)

Let me know when you've got that working and we'll put the final piece in place
0
 

Author Comment

by:gbcbr
ID: 35198363
OK, I'll try
0
 

Author Comment

by:gbcbr
ID: 35199104
@CEHJ
I don't want to mix these two ways in my working class.
I've reduce number of changeable fields to 20 and already complete this job.
I'll open new question about serialization job, duplicate DCC to DCC1 and we'll play with it.
Anyway, thank you for your efforts.

@objects
Thank you for your assistance, now I know how to manage Properties files.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35199609
OK :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question