Results 1 to 8 of 8
  1. #1
    daxserver is offline Member
    Join Date
    Apr 2012
    Location
    Vijayawada, India
    Posts
    22
    Rep Power
    0

    Default JTextFields not updating

    I have two JTextFields ipfield and portfield. If I put the JOptionPane at the fourth line the ipfield and portfield did not get update.

    Java Code:
    JTextField ipfield=new JTextField();
    JTextField portfield=new JTextField();
    
    JOptionPane.showMessageDialog(null,"You have five minutes for your Opponent to connect"+newline+"Program Locks until Opponent connects", "Connection", 1);
    			
    localport=serversocket.getLocalPort();
    ipfield.setText(localip);
    portfield.setText(Integer.toString(localport));
    			
    socket=serversocket.accept();
    If I put it at line 8, the fields are updated with the IP and Port successfully.
    Java Code:
    JTextField ipfield=new JTextField();
    JTextField portfield=new JTextField();
    
    localport=serversocket.getLocalPort();
    ipfield.setText(localip);
    portfield.setText(Integer.toString(localport));
    
    JOptionPane.showMessageDialog(null,"You have five minutes for your Opponent to connect"+newline+"Program Locks until Opponent connects", "Connection", 1);
    
    socket=serversocket.accept();
    But I need the first sample code only, because the JOptionPane must come first before the fields get updated. How can I do that?

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

    Default Re: JTextFields not updating

    Are you saying that your program fails even if the user presses the JOptionPane "OK" button making the JOptionPane go away?

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

    Default Re: JTextFields not updating

    Also, your code shows little information on how you're handling your communication with regard to the Swing event thread.

  4. #4
    daxserver is offline Member
    Join Date
    Apr 2012
    Location
    Vijayawada, India
    Posts
    22
    Rep Power
    0

    Default Re: JTextFields not updating

    Here is the compilable code in attachments.

    Observe the JOptionPane in the server method in two files.

    First is b.java -> Here the textfields are updated.

    Second is c.java -> Here the textfields are updated when SocketTimeoutException occurs. If there is no timeout defined, then they never get updated. If there is JOptionPane, then it is the same.

    I got trouble in attaching files here, so I have put them on my Google Drive https://docs.google.com/folder/d/0Bz...40a0xrRnM/edit

    How can I update those fields without the JOptionPane?

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

    Default Re: JTextFields not updating

    I'd rather you post the salient code here. I'm of the philosophy that if you're asking for free advice, the onus is on you to try to make it as easy as possible for us to be able to help you. I prefer an SSCCE, but will review a reasonable amount of non-SSCCE code if it is written so that it is easy to understand. Also, please go into greater detail on your code's expected and observed behavior. Again, what happens if the user handles the dialogs and they go away?

  6. #6
    daxserver is offline Member
    Join Date
    Apr 2012
    Location
    Vijayawada, India
    Posts
    22
    Rep Power
    0

    Default Re: JTextFields not updating

    Sorry for posting code like that. It is scrambled as many parts all over my program. So it made me hard to post as short and simple as possible as I can. And I compiled on JDK 7

    In b.java(second code in the first post of thread) -> the JOptionPane is written after the ipfield and portfield are updated with setText(). When I run this code these fields are actually updated with the desired data (my external ip and opened port for socket [getLocalPort()] ) and then the message dialog appears. //But as long as the user doesn't click the ok button, the socket.accept() is not called.

    In c.java(first code)-> the JOPane is written before the ipfield and portfield are updated. Running this, first the dialog comes and user clicks ok. The next line in the code is to update the ipfield and portfield before the socket.accept() is called but it is not happening - they are not updated. But when the timeout occurs and exception is thrown then these two fields are updated. (If there is no timeout then those two fields are never updated)

    I didn't understand why they were not updating.

    //I am unable to attach screenshot. When I click on "Manage Attachments" a new empty window opens up.
    Last edited by daxserver; 05-28-2012 at 10:37 PM.

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

    Default Re: JTextFields not updating

    It sounds like you've likely got a threading issue. Are you using a SwingWorker for your background thread work? If not, look into doing this.

  8. #8
    daxserver is offline Member
    Join Date
    Apr 2012
    Location
    Vijayawada, India
    Posts
    22
    Rep Power
    0

    Default Re: JTextFields not updating

    Quote Originally Posted by Fubarable View Post
    It sounds like you've likely got a threading issue. Are you using a SwingWorker for your background thread work? If not, look into doing this.
    No, I am not using it.I will try it.
    Thanks.

Similar Threads

  1. JTextFields
    By wired-in=p in forum New To Java
    Replies: 1
    Last Post: 01-30-2012, 12:14 AM
  2. Problems Using JTextFields
    By jrJava in forum New To Java
    Replies: 2
    Last Post: 02-03-2011, 01:55 AM
  3. get values from JTextFields input
    By cselic in forum Java 2D
    Replies: 3
    Last Post: 07-27-2010, 09:35 PM
  4. Problem with JTextFields not null
    By romina in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 05:17 AM
  5. JTextFields with username & password.
    By Eric in forum AWT / Swing
    Replies: 2
    Last Post: 07-01-2007, 11: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
  •