Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7171911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:30:29+00:00 2026-05-28T15:30:29+00:00

I am reading A Definite Guide to SWT and JFace and I am trying

  • 0

I am reading “A Definite Guide to SWT and JFace” and I am trying to understand the following code:

public class MultipleListenersExample implements HelpListener, VerifyListener, 
  ModifyListener{

    // Constants used for conversions
    private static final double FIVE_NINTHS = 5.0 / 9.0;
    private static final double NINE_FIFTHS = 9.0 / 5.0;

    // Widgets used in the window
    private Text fahrenheit;
    private Text celsius;
    private Label help;

    /**
     * Runs the application
     */
    public void run() {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Temperatures");
        createContents(shell);
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    /**
     * Create the main window's contents
     * @param shell the main window
     */
    private void createContents(Shell shell) {
        shell.setLayout(new GridLayout(3, true));

        // Create the label and input box for Fahrenheit
        new Label(shell, SWT.LEFT).setText("Fahrenheit:");
        fahrenheit = new Text(shell, SWT.BORDER);
        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        data.horizontalSpan = 2;
        fahrenheit.setLayoutData(data);

        // Set the context-sensitive help
        fahrenheit.setData("Type a temperature in Fahrenheit");

        // Add the listeners
        fahrenheit.addHelpListener(this);
        fahrenheit.addVerifyListener(this);
        fahrenheit.addModifyListener(this);

        // Create the label and input box for Celsius
        new Label(shell, SWT.LEFT).setText("Celsius:");
        celsius = new Text(shell, SWT.BORDER);
        data = new GridData(GridData.FILL_HORIZONTAL);
        data.horizontalSpan = 2;
        celsius.setLayoutData(data);

        // Set the context-sensitive help
        celsius.setData("Type a temperature in Celsius");

        // Add the listeners
        celsius.addHelpListener(this);
        celsius.addVerifyListener(this);
        celsius.addModifyListener(this);

        // Create the area for help
        help = new Label(shell, SWT.LEFT | SWT.BORDER);
        data = new GridData(GridData.FILL_HORIZONTAL);
        data.horizontalSpan = 3;
        help.setLayoutData(data);
    }

    /**
     * Called when user requests help
     */
    public void helpRequested(HelpEvent event) {
        // Get the help text from the widget and set it into the help label
        help.setText((String) event.widget.getData());
    }

    /**
     * Called when the user types into a text box, but before the text box gets
     * what the user typed
     */
    public void verifyText(VerifyEvent event) {     
        // Assume you don't allow it
        event.doit = false;

        // Get the character typed
        char myChar = event.character;
        String text = ((Text) event.widget).getText();
        System.out.println(text);

        // Allow '-' if first character
        if (myChar == '-' && text.length() == 0) event.doit = true;

        // Allow zero to nine
        if (Character.isDigit(myChar)) event.doit = true;

        // Allow backspace
        if (myChar == '\b') event.doit = true;
    }

    /**
     * Called when the user modifies the text in a text box
     */
    public void modifyText(ModifyEvent event) {     
        // Remove all the listeners, so you don't enter any infinite loops
        celsius.removeVerifyListener(this);
        celsius.removeModifyListener(this);
        fahrenheit.removeVerifyListener(this);
        fahrenheit.removeModifyListener(this);

        // Get the widget whose text was modified
        Text text = (Text) event.widget;

        try {
            // Get the modified text
            int temp = Integer.parseInt(text.getText());

            // If they modified Fahrenheit, convert to Celsius
            if (text == fahrenheit) {
                celsius.setText(String.valueOf((int) (FIVE_NINTHS * (temp - 32))));
            } else {
            // Convert to Fahrenheit
                fahrenheit.setText(String.valueOf((int) (NINE_FIFTHS * temp + 32)));
            }
        } catch (NumberFormatException e) { /* Ignore */ }

        // Add the listeners back
        celsius.addVerifyListener(this);
        celsius.addModifyListener(this);
        fahrenheit.addVerifyListener(this);
        fahrenheit.addModifyListener(this);
    }

    /**
     * The application entry point
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new MultipleListenersExample().run();
    }

}

In “modifyText” method it removes all the listeners (VerifyListener and ModifyListener) and I don’t understand why? Why an infinite loop appears?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:30:30+00:00Added an answer on May 28, 2026 at 3:30 pm

    Because you’re actually modifying text string (value) in modifyText() method of Text GUI element, this change of text will trigger ModifyEvent which calls modifyText() method again. So you have to remove those listeners, change text to appropriate value and add those listeners back, otherwise infinite loop occurs.

    EDIT (based on comments)

    You’re right. First you type a key in Text field and verifyText() is called. When this method finish and typed text is approved, modifyText() method is called. And as I said previous, this method changes text string of Text field by setText() method. So before the text string could be actually changed verifyText() is called and approves the new text string, then goes to modifyText() method which should actually change the text inside Text GUI element, but it tries to call setText() of the element and starts the circle again.

    So, you type some number into Text element (like '5'), verifyText() is called and modifyText() follows, it calls setText() which calls verifyText() and modifyText() again, it calls setText(), and so on.. Oh yeah, infinite loop is here..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading source code of my current project, I see: [self retain] in one class,
Consider the following code: #include <iostream> struct test { void public_test() { [this]() {
Reading code from other posts, I'm seeing something like this. struct Foo { Foo()
Reading over some example Objective C code just now. @property (nonatomic, strong) IBOutlet UILabel
Reading the documentation on the changes to UIViewControllers in iOS, I trying to figure
Reading for hours, I am pretty sure I understand how blocks in Jade work.
Reading the code of many javascript libraries, I see that many developers use to
Reading the design guidelines. I came across a little issue while trying to practice
Reading this question I found this as (note the quotation marks) code to solve
Reading the Doctrine2 documentation for the Query Builder I found the following expressions: //

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.