Search code examples
javajtextfield

Can't change the JTextField text


I usually write the code in C#, and I even have the application finished in C#, but I need to port it to Java.

Just started out today, never really used Java before.

The problem is quite strange because I do not get any error it just does not update, here's the code:

public void setInfoStrip(String infoStrip) {
    InfoStrip.setText(infoStrip);
}

The above is a Setter that should update the JTextField (and it does if I update it using the ActionListener for a button), but it does not update the text when I call it in a class or even in the main(String[] args) entry point of application using this code:

mainGUI GUI = new mainGUI();
GUI.setInfoStrip("test");

or this code:

new mainGUI().setInfoStrip("test");

My guess is that it does nothing becuase I call it from a static class

public static void main(String[] args)

But even if I create a new class that's not static and reference it from the public staitc void main(String[] args) then put either

mainGUI GUI = new mainGUI();
GUI.setInfoStrip("test");

or

new mainGUI().setInfoStrip("test");

It the newly created class, which I call by

new ImGoingToCry().Alot();

It still does nothing.

I'm confused as hell, I even read some problems connected with this on google but they were all solved by this:

mainGUI GUI = new mainGUI();
GUI.setInfoStrip("test");

Here's the MVCE that some of you requested:

public class mainGUI {
// GUI Elements
private JPanel WorkSpace;
private JTabbedPane tabbedPane1;
private JList DetectedProfiles;
private JButton StartGame;
private JTextField CurProf;
private JButton BackupProfiles;
private JButton SearchSaves;
private JButton RetrieveProfiles;
private JTextField InfoStrip;
private JLabel ProfileSize;

/**
 * Getter and Setter functions
 */
public void setInfoStrip(String infoStrip) {
    InfoStrip.setText(infoStrip);
}


// Initialize the main application GUI and set it's properties
public static void main(String[] args) {
    JFrame mainGUIFrame = new JFrame("The Witcher 3 Save Manager | " + " ver. "  + GlobalVariables.appversion);
    mainGUIFrame.setContentPane(new mainGUI().WorkSpace);
    mainGUIFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainGUIFrame.setLocationRelativeTo(null);
    mainGUIFrame.setPreferredSize(new Dimension(420, 370));
    mainGUIFrame.setResizable(false);
    mainGUIFrame.pack();
    mainGUIFrame.setVisible(true);
    new mainGUI().run();
}


public void run() {
    /**
     * Initialize the core application functions
     */
    // Load the application settings
    GlobalVariables.Settings();
    // Initialize the app components
    GlobalVariables.Initialize();

    // Pass the value to setter
    new mainGUI().setInfoStrip("test"); // This should change the text but it does nothing there's not even an error
}
}

Solution

  • May be this will be of help. From javadocs for JTextComponent, from which JTextField inherits setText() method:

    setText

    Sets the text of this TextComponent to the specified text. If the text is null or empty, has the effect of simply deleting the old text. When text has been inserted, the resulting caret location is determined by the implementation of the caret class.

    Note that text is not a bound property, so no PropertyChangeEvent is fired when it changes. To listen for changes to the text, use DocumentListener.

    In case you wonder what a BoundProperty is:

    A bound property notifies listeners when its value changes. This has two implications:

    1. The bean class includes addPropertyChangeListener() and removePropertyChangeListener() methods for managing the bean's listeners.
    2. When a bound property is changed, the bean sends a PropertyChangeEvent to its registered listeners. PropertyChangeEvent and PropertyChangeListener live in the java.beans package.

    The java.beans package also includes a class, PropertyChangeSupport, that takes care of most of the work of bound properties. This handy class keeps track of property listeners and includes a convenience method that fires property change events to all registered listeners.

    P.S.: as the OP has not given any mcve, this sounds relevant. But, I strongly feel the case is going to be otherwise once we see the OP's code.