简体   繁体   中英

Java (JFace Application Window) Setting external label text

I am looking to figure out how to set the text of a label on an external Application Window.

What I have:

I have two windows so far. The first one is the main application window that will appear when the user starts the program. The second window is another separate window that I have created specifically to display a custom error window.

The problem: I seem to be unable to call the label that I have created on the error window and set the text to something custom. Why? I want to be able to reuse this window many times! This window is aimed for things like error handling when there is invalid input or if the application cannot read/save to a file.

I was going to post screen shots but you need 10 rep for that. It would have explained everything better.

Here is the code for the label on the Error_dialog window:

Label Error_label = new Label(container, SWT.NONE);
Error_label.setBounds(10, 10, 348, 13);
Error_label.setText("Label I actively want to change!");

Here is the condition I would like to fire off when it is met:

if(AvailableSpaces == 10){
//Set the label text HERE and then open the window!
    showError.open();
}

I have included this at the top of the class as well:

Error_dialog showError = new Error_dialog();

Just save the label as a field in your dialog class and add a 'setter' method. Something like:

public class ErrorDialog extends Dialog
{
  private Label errorLabel;

  ... other code

  public void setText(String text)
  {
    if (errorLabel != null && !errorLabel.isDisposed()) {
      errorLabel.setText(text);
    }
  }

You will need to use your dialog like this:

 ErrorDialog dialog = new ErrorDialog(shell);

 dialog.create();  // Creates the controls

 dialog.setText("Error message");

 dialog.open();

Note: you should stick to the rules for Java variable names - they always start with lower case.

Further learn to use Layouts . Using setBounds will cause problems if the user is using different fonts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM