Updating a jLabel

Haanthid :

I have a simple GUI that has a jTextField that waits for the user to put in something. After a button is clicked, the program:

  1. reads the input, saves it in a String variable;
  2. opens a new GUI (that is in a separate class file), which contains an empty jLabel, and passes the String variable to it, changing the jLabel text to it.

The problem is that no matter how hard I try to reconfigure the code, adding things like repaint(), revalidate(), etc., the jLabel in the second GUI stays empty. Using a System.out.println(jLabel.getText()) reveals that the text value is indeed changed, but not displayed. How do I "refresh" this jLabel, so it'd show what I want it to? I'm aware I could add an event, though I don't want the user to click anything to refresh the GUI, the values should be there as it's initiated. I've read trough several similar posts, but found that the solutions don't work for me.

The code of first GUI's button click event:

private void sbuttonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    errortext.setText("");
    Search = sfield.getText();
    Transl = hashes.find(Search);
    if (Transl.equals("0")) errortext.setText("Word not found in database.");
    else {
        ws.run(Search, Transl); // <- this opens the second GUI, with two String parameters I want to display in the second GUI;
    }
}

The code of the second GUI (activeword and translation are the jLabels that are giving me trouble.):

public void run(String Search, String Transl) {
    WordScreen init = new WordScreen(); //initialise the second GUI;
    init.setVisible(true);
    activeword.setText(Search); 
    translation.setText(Transl);
}

Any reply is very welcome! Please ask me for more information about the code if necessary, I will make sure to reply as soon as possible!

Hovercraft Full Of Eels :

Best solution: change WordScreen's constructor to accept the two Strings of interest:

From this:

public void run(String Search, String Transl) {
    WordScreen init = new WordScreen(); //initialise the second GUI;
    init.setVisible(true);
    activeword.setText(Search); 
    translation.setText(Transl);
}

to this:

public void run(String search, String transl) {
    WordScreen init = new WordScreen(search, transl); 
    init.setVisible(true);
}

Then in the WordScreen constructor use those Strings where needed:

public WordScreen(String search, String transl) {
    JLabel someLabel = new JLabel(search);
    JLabel otherLabel = new JLabel(transl);

    // put them where needed
}

Note that I cannot create a comprehensive answer without your posting a decent MRE


As an aside, you will want to learn and use Java naming conventions. Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=284777&siteId=1