Results 1 to 19 of 19
  1. #1
    Ramona is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default update textField in a window extending Dialog class

    Hy,

    I have a big problem. I have an application with many buttons and windows. On one of this windows (which extends Dialog class) a have a textField. I want to set it's value from another class B. If in class B I write tf.setText("test") and on the next line I write System.out.println(tf.getText) "test" is displayed, but in the textField the value doesn't change.


    The idee of my program is to read some values from a file and display them in textFields in other window (extends dialog).

    Thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Without source code, all I can venture is a guess, but I'll bet that you have two of your Dialog class objects, one displayed which you aren't changing, and the other which is having its tf TextField class's text changed, but this second Dialog is not being displayed.

    The solution is to somehow get a reference to the first Dialog object and make the changes on this reference.

  3. #3
    Ramona is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default

    Here is the code:
    public class DataWindow extends Dialog
    { public TextField latdegree;

    public DataWindow(GPSDesk gp){
    super(gp,"Data", false);
    this.gp=gp;
    setLayout(null);
    setResizable(false);
    setUndecorated(true);
    setBackground(new Color(238,238,238));
    nmea=new NMEA();

    .......


    latdegree=new TextField();
    latdegree.setForeground(Color.red);
    latdegree.setBackground(Color.blue);
    latdegree.setFont(f);
    latdegree.requestFocus();
    add(latdegree);
    latdegree.setBounds(75,135,26,20);

    repaint();
    resize(300,347);
    move(673,95);
    setVisible(false);

    }

    public void setLatdegreeText(String text){
    this.latdegree.setText(text);
    System.out.println(text);
    repaint();
    }
    }


    and here is the code for setting the value from other class:


    public class ReadLog extends Thread{

    public String filepath;
    private BufferedReader f;
    public NMEA nmea;
    GPSDesk gp;
    DataWindow dw;
    Graphics g;
    String filename;

    public ReadLog(GPSDesk gp){
    this.gp=gp;
    nmea=new NMEA();
    dw=gp.dw;
    }

    public void parseLog(String filename){
    this.filename=filename;
    start();
    }

    public void run(){
    File file=new File(filename);
    URL url=null;
    filepath=filename;
    try{
    url=file.toURL();
    InputStream is=url.openStream();
    f=new BufferedReader(new InputStreamReader(is));
    }
    catch(IOException io){
    System.err.println(io);
    return;
    }
    try{
    StreamTokenizer str=new StreamTokenizer(new FileReader(filepath));
    while(str.nextToken()!=str.TT_EOF){
    String linie;
    while((linie=f.readLine())!=null){
    StringTokenizer st=new StringTokenizer(linie,",");
    String sentence_type=st.nextToken();
    if (sentence_type.equals("$GPRMC")){
    nmea.parseRMC(linie);
    //dw.latdegree.repaint();
    dw.setLatdegreeText("text");
    //System.out.println(dw.latdegree.getText());
    }
    }
    }
    f.close();
    }
    catch(IOException io){}
    }

    }

    Thanks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If I am right, the error will not likely be in the DataWindow/Dialog code but will be in how you use this class. Again, I'll bet that you create a DataWindow object and display it, and then somewhere you create a second DataWindow object that's not displayed, but that has it's tf changed.

    Also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. Another way is to place the tag [code] at the top of your block of code and the tag [/code] at the bottom, like so:

    Java Code:
    [code]
      // your code block goes here.
      // note the differences between the tag at the top vs the bottom.
    [/code]

  5. #5
    Ramona is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default

    Ok, sorry.

    I have a main class (GPSDesk) in which I have new DataWindow(this) and then I use this instance of it. If you have a YM id I will send all code (it is to large to post it here).

    More, if I type a text in textField and then I use System.out.println(latdegree.getText()) from other class doesn't print the new text.

    What you say sounds to be the problem, but I don't know were I do what you say.

    Thanks
    Last edited by Ramona; 11-22-2008 at 04:18 PM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Nah, I'm an old fuddy-duddy who doesn't use YM. Besides, my personal belief is that it goes against a major philosophy of this forum: to share problems and solutions for all.

    You may want to create new compilable classes that simplify your problem down to the essentials, so that we can run this code, but so that it is still of reasonable length to post.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Also on a side question, why are you coding with AWT and not the more robust and flexible Swing library?

  8. #8
    Ramona is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default

    Ok. We will try here to find a solution.
    I can simplify the code to much.

    So, I have a main class (GPSDesk) which call the DataWindow constructor. Then I have other class in which I try to set the DataWindow TextField value using the instance of this window created in main class.

    I must coding with AWT. Is not my choice.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I see that you've posted more code in post number 3. Sorry to say but I don't see the source of the error in your posted code. I don't see you creating any shadow instances of the DataWindow class, so this doesn't explain the bug.

    I must coding with AWT. Is not my choice.
    that is certainly a valid reason to use AWT. :)

    Pete

  10. #10
    Ramona is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default

    Ok. Thanks for the help.

    Ramona

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    A small nitpick though, I think you may want to encapsulate things a little bit better. Myself, I like to use private widgets such as TextFields, etc. Then if I want an outside agent to modify my widget, I'll provide a public method to do this. This will help prevent the outside agents from being able to do too much to my widget and also allows me to check for errors or inappropriate input. Something like so:

    Java Code:
    public class DataWindow
    {
      private TextField latdegree;
      
      // ..........
      
      public void setLatDegree(String text)
      {
        latdegree.setText(text);
      }
    }

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    One last thing, you could try to post your code to a public file sharing site, and then write the link here. Since as a new poster you can't post real links yet, you could always use a pseudo link:

    www<dot>mylink<dot>com or something similar.

    There's no guarantee that anyone here will want to download and sift through a large file, but you may get lucky.

  13. #13
    Ramona is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default

    I solved the problem. It was like you side. Another instance of the DataWindow - when I push the button which opens that window.
    You are great. Thank you a lot.

    I will use your advice and I make private the components.

    Thanks,
    Ramona

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Great, glad you solved it, and pretty much on your own too. Good show. Hope to see you here again.

  15. #15
    Ramona is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default

    I hope to be here soon, too, but helping others and not posting my problems (I don't want to have other problems).

    Your ideea helped me.Thanks

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Please don't cross-post without at least noting that you're doing this.

  17. #17
    Ramona is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default

    Sorry, I don't undestand.

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You posted the same question in JavaRanch and perhaps elsewhere. While it's ok to do this, please realize that anyone who helps you here or on JavaRanch (or the Sun forums or similar places) are all volunteers. Just like you, our time is valuable to us, and we don't like performing futile actions such as making suggestions that have been made elsewhere.

    Your best course of action when cross-posting the same question on multiple sites is to explicitly state that you are doing this and if possible (not possible here for you just yet) providing links in each thread to the other threads. This will help prevent duplication of effort among the volunteers helping you.

  19. #19
    Ramona is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default

    I posted just in JavaRanch and here. I'm new on forums and I needed an answer. I didn't know that this is a bad thing. I'll not do this anymore. Sorry.

Similar Threads

  1. [SOLVED] Problem with extending classes...
    By Bizmark in forum New To Java
    Replies: 4
    Last Post: 04-07-2008, 11:21 PM
  2. main window & print dialog
    By roo7 in forum Advanced Java
    Replies: 3
    Last Post: 01-02-2008, 06:50 AM
  3. Implementing and Extending together
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-24-2007, 09:49 AM
  4. EXTENDING the string class
    By ferno in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 09:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •