0

In my GUI method I have two textFields using the Java JFrame classes. One textField sends text to the server (this function work as needed) and one textField that receives data from the server and prints it to the GUI.

I am using the textField.setText(ServerString) method to set my text to the textField, but the .setText method only will get the text from the string once when the GUI is started up by a command on the server and then never set the text on the textField again.

I have verified that the variable ServerString is being live updated by the server every time something is typed but it still will not set the text to the textField. Once when it is launched, I have verified the method is being called and that .setText() is not working after first launch. I have tried everything from try-catch statements to setting the text to blank (which didn't work).

Is there something I am missing or some function that I am not calling or a different one I should be using?

Just an example of code so you know what libraries and stuff I am using

Here is my GUI Code that can be used to re-create the problem

package Encrypter.src;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import net.dv8tion.jda.events.message.MessageReceivedEvent;

import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.swing.JTextPane;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;

public class TalkGUI extends JFrame {
    JTextPane textPane = new JTextPane();
    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField2;
    //JTextPane textPane = new JTextPane();
    /**
     * Launch the application.
     */
    public static void GUI() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TalkGUI frame = new TalkGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TalkGUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 647, 448);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        textField = new JTextField();
        textField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int key = e.getKeyCode();
                 if (key == KeyEvent.VK_ENTER) {
                     System.out.println("ENTER pressed");


                     if(textField.getText().length() > 0)
                     {
                     }

                     //textField.setText("test");
                     //System.out.println(textField.getText());


                 }
            }
        });

        textField.setBounds(10, 77, 292, 321);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel lblHd = new JLabel("Input to Server");
        lblHd.setBounds(86, 34, 108, 32);
        contentPane.add(lblHd);

        JLabel lblOutputFromServer = new JLabel("Output From Server");
        lblOutputFromServer.setBounds(388, 43, 174, 14);
        contentPane.add(lblOutputFromServer);

        textField2 = new JTextField();
        textField2.setBounds(328, 77, 293, 321);
        contentPane.add(textField2);
        textField2.setColumns(10);

    /*
        textPane.setBounds(312, 77, 309, 321);
        textPane.setEditable(false);
        textPane.setText("you cant edit this paneen");
        contentPane.add(textPane);
        */
    }

    public void set(String string)
    {
        System.out.println(string + " ok");
        textField2.setText(string);
    }


}
Shiloh
  • 21
  • 5
  • It is most likely a problem in your code. Can you provide the detailed code that your have. – randominstanceOfLivingThing Aug 12 '16 at 05:08
  • Are you reloading the page? – Paul Hicks Aug 12 '16 at 05:08
  • 1
    Please do provide a [mcve] of the problem – OneCricketeer Aug 12 '16 at 05:09
  • Thank you for telling me...I forgot. I just provided my GUI code excluding some of the server side stuff. – Shiloh Aug 12 '16 at 05:14
  • 1
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) **Don't block the EDT** (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. – Andrew Thompson Aug 12 '16 at 05:58
  • 3) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) **all lower case** for package names, and use it consistently. – Andrew Thompson Aug 12 '16 at 05:59
  • More comments about the (lack of) layout. The look of the current GUI can be easily reproduced using layouts, but the two text fields should apparently be text areas (`JTextArea`). Ask on a separate question & likely someone will show you how to lay it out properly. BTW - when the GUI is expanded, I would guess that extra height should be assigned to the text input/output areas, while the extra width should be divided evenly between them? Another thing you might consider is to replace the labels (`Input to Server` & `Output From Server`) with a `TitledBorder` for each text control. – Andrew Thompson Aug 12 '16 at 06:15
  • Try `JComponent.validate();` <-- Updates the Component. – DarkV1 Aug 12 '16 at 15:21

2 Answers2

0

You can set the text as many times as you want. you just put a logic in your code that whenever your server string is getting modified then you just call setTest() method, i.e: textField2.setText(ServerString);

Lori
  • 1,392
  • 1
  • 21
  • 29
  • that Is what I have been doing though and it doesnt work...I have provided my code for my GUI excluding some of the server side stuff – Shiloh Aug 12 '16 at 05:13
0

what was wrong with my code was constant label assignment. when i pressed a button, i was running this code:

notInStr = new JLabel ("Not in string : " + currentWord.getNotInWord());

but when i only use setText(), my problem is solved. like this:

        notInStr.setText("Not in string : " + currentWord.getNotInWord());

you should assign once JLabel (and textfield). I hope can help you.