Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Next Button Event Listener - JFace Wizard
Next Button Event Listener - JFace Wizard [message #1815765] Thu, 10 October 2019 07:38 Go to next message
Mihnea-Costache Marin is currently offline Mihnea-Costache MarinFriend
Messages: 68
Registered: September 2019
Member
Hi,

Is there a way to create an event when the next button is pressed? I have a general class that creates two wizards(the two pages) and I want to parse some variable to the second page the moment the next button is pressed on the first page.

Thank you
Re: Next Button Event Listener - JFace Wizard [message #1815774 is a reply to message #1815765] Thu, 10 October 2019 11:00 Go to previous messageGo to next message
Emmanuel Chebbi is currently offline Emmanuel ChebbiFriend
Messages: 123
Registered: February 2018
Senior Member
Hi,

You can overridde the WizardDialog#nextPressed method to broadcast an event or parse your data. See also the thread add listener to next button wizard page.

That being said, here are some alternatives that may be easier than dealing with events:

  1. Give a reference of the first page to the second page (or alternatively use something like the WizardPage#getPreviousPage). Then use the reference in your second page's createControl method to retrieve the data to parse and initialize the fields properly (never tested).
    @Override
    public void createControl(Composite parent) {
        IWizardPage previous = getPreviousPage();
        MySecondPage secondPage = (MySecondPage) previous;
    
        initFields(secondPage.getData());
    }
    

  2. Override your wizard's getNextPage method, parse the data of your first page then initialize your second page properly (I used it):
    @Override
    public IWizardPage getNextPage(IWizardPage page) {
        IWizardPage nextPage = super.getNextPage(page);
        
        if (nextPage == secondPage) {
            ParsedData data = parse(page.getData());
            nextPage.initFields(data);
        }
        return nextPage;
    }
    

Re: Next Button Event Listener - JFace Wizard [message #1815777 is a reply to message #1815774] Thu, 10 October 2019 11:52 Go to previous messageGo to next message
Mihnea-Costache Marin is currently offline Mihnea-Costache MarinFriend
Messages: 68
Registered: September 2019
Member
The thing is, I have tried a lot of ways but it's not working. I will try to give some more details.

The principal class is extending from Wizard and implements IImportWizard, IWizard. There I create the two pages: page1 = new Page1();.

The pages are extending the WizardPage.

Is it ok?

Now, what I want to do is, after I click next, the data will be taken from the first page and parsed to the second page. The problem that I'm facing is the fact that the second page is created at the same time the first page is created thus affecting me. I'm not sure how to prevent that.
Re: Next Button Event Listener - JFace Wizard [message #1815779 is a reply to message #1815777] Thu, 10 October 2019 13:15 Go to previous messageGo to next message
Emmanuel Chebbi is currently offline Emmanuel ChebbiFriend
Messages: 123
Registered: February 2018
Senior Member
Mihnea-Costache Marin wrote on Thu, 10 October 2019 13:52
The principal class is extending from Wizard and implements IImportWizard, IWizard. There I create the two pages: page1 = new Page1();.

The pages are extending the WizardPage.

Is it ok?

Looks good to me.

Mihnea-Costache Marin wrote on Thu, 10 October 2019 13:52

The thing is, I have tried a lot of ways but it's not working

Please explain what you mean by "it's not working": what did you try exactly? Is an exception thrown? Did you browse the class' documentation but did not see any relevant method? Did you manage to execute some code when the user clicks Next but page2's fields are not updating?

Mihnea-Costache Marin wrote on Thu, 10 October 2019 13:52
The problem that I'm facing is the fact that the second page is created at the same time the first page is created thus affecting me.

Even if the two pages are created at the same time, that does not prevent you from updating them later on. Here, you have to:

  1. Determine when the user clicks on Next
  2. Retrieve the data from the first page
  3. Update the second page with these data

According to the documentation, the Wizard#getNextPage method is called by the wizard right before a new page is shown, so you can override it to add some preprocessing. That solves point 1).

In your wizard you should have a reference to Page1 and can use it to query your data. That solves point 2).

You should also have a reference to Page2 and can use it to update the fields. That solves point 3).

The corresponding code is the following:
class SecondPage extends WizardPage {

    private Text projectNameField;

    @Override
    public void createControl(Composite parent) {
         // Create empty fields
        String projectName = previous.getProjectName();
        projectNameField = new Text(parent, SWT.SINGLE);
        projectNameField.setText("");
    }

    public void updateProjectName(String newProjectName) {
        // Update the text field
        projectNameField.setText(newProjectName);
    }

class MyWizard extends Wizards {

    private Page1 page1;
    private Page2 page2;

    @Override
    public IWizardPage getNextPage(IWizardPage page) {
         // This method is called just before showing the second 

         IWizardPage nextPage = super.getNextPage(page);

         // If the wizard is about to show the second page
         if (nextPage == page2) {
             // We retrieve the data from page1 ...
             String projectName = ((Page1) page).getProjectName();

             // ... and use it to update page2
             page2.updateProjectName(projectName);
         }
        return nextPage;
    }

}

Note: according to this anwser on StackOverflow the getNextPage() method may be called multiple times, so take care not to put long-runtime process there.

This code may even be simplified. Indeed, the createControl() method may be called only when the page is shown. In other words, graphical components (lists, text fields, etc.) of the second page may not be created until the user clicks on Next. In fact I would expect this behavior but I didn't try so I am not sure that's actually the case. I invite you to test because if that's the case then the code can be simplify.

Indeed, you could thus both 1) retrieve the data from the first page and 2) initialize your fields with the expecting values within Page2:
class Page2 extends WizardPage {

    private Text projectNameField;

    @Override
    public void createControl(Composite parent) {
        IWizardPage previousPage = getPreviousPage();
        Page1 firstPage = (Page1) previous;

        String projectName = previous.getProjectName();
        projectNameField = new Text(parent, SWT.SINGLE);
        projectNameField.setText(projectName);
    }
}


Re: Next Button Event Listener - JFace Wizard [message #1815815 is a reply to message #1815779] Fri, 11 October 2019 09:26 Go to previous messageGo to next message
Mihnea-Costache Marin is currently offline Mihnea-Costache MarinFriend
Messages: 68
Registered: September 2019
Member
Worked like a charm! Thank you!
Re: Next Button Event Listener - JFace Wizard [message #1815823 is a reply to message #1815779] Fri, 11 October 2019 13:31 Go to previous messageGo to next message
Mihnea-Costache Marin is currently offline Mihnea-Costache MarinFriend
Messages: 68
Registered: September 2019
Member
Is there a way to check if the finish button was pressed?
Re: Next Button Event Listener - JFace Wizard [message #1815825 is a reply to message #1815823] Fri, 11 October 2019 14:20 Go to previous message
Emmanuel Chebbi is currently offline Emmanuel ChebbiFriend
Messages: 123
Registered: February 2018
Senior Member
Yes. You can check that both when the wizard is opened and when it has closed but without knowing your use case I can't tell you more details. Did you browse the JavaDoc, looked some tutorials up or tried to implement a solution?
Previous Topic:Is there a way to launch in full screen mode by default?
Next Topic:Unable To Connect The MS SQL Server
Goto Forum:
  


Current Time: Thu Apr 18 14:40:31 GMT 2024

Powered by FUDForum. Page generated in 0.02236 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top