0

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();
  • What do you mean by 'unable to find the label'? – greg-449 Mar 02 '15 at 20:34
  • @greg-449 For example, setting things such as the window status you would type showError.setStatus("Hello World!"); how can I call the label instead? I want to call the label and set the text from the main application window. –  Mar 02 '15 at 20:38

1 Answers1

0

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.

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • Thank you for your answer. I haven't yet had time to try it out. Thank you also for pointing out my variable names. I will change these as I quite often forget what's what. I **should** try and stick to the documentation more often. It can sometimes be really boring and long-winded though... –  Mar 09 '15 at 11:47
  • I would also like to point out that this application will not see the light of day. It is just an experimental application. I know that any other layout is better than absolute... I just can't be bothered messing about with something that will only be added to the archives. –  Mar 09 '15 at 12:00