1

I am trying to take input from the below screen and open Birt Report directly into PDF. (I am using Vaadin 14)

Problem: My Anchor/Button is not opening any link on the first click every time this UI gets loaded. But After clicking once from the second click onward, it is working fine. Any Idea what can be the issue and what mistake I've made?

enter image description here

Below are my global Variables:

final Button                    printButton                     =
    new Button("Print Report", VaadinIcon.PRINT.create());
String                          url                             = null;
final Anchor                    anchorReport                    = new Anchor();

My Consutructor:

public AssignmentCompletedAllRPTView()
{
    super();
    this.initUI();
    this.radioButtonGroup.setItems("Yes", "No");
    this.printButton.addClickListener(new ComponentEventListener<ClickEvent<Button>>()
    {
        @Override
        public void onComponentEvent(final ClickEvent<Button> event)
        {
            AssignmentCompletedAllRPTView.this.printButton();
        }
    });
    this.ConfigureReportButton();
}

ConfigureReportButton():

private void ConfigureReportButton()
{
    this.anchorReport.setTarget("_blank");
    this.anchorReport.add(this.printButton);
    this.buttonHorizontalLayout.add(this.anchorReport);
}

printButton():

private void printButton()
{
        final String reportURL = //--URL
        this.anchorReport.setHref(reportURL);
}
Prashant Kumar
  • 145
  • 1
  • 11
  • Can you minimize your code sample to contain only the lines required to reproduce the issue? Now there's a large amount of code that I assume is unrelated to the issue at hand, like all of the logging. – ollitietavainen Nov 04 '20 at 14:07
  • I don't know actually where it is having an issue on the first click. I am so sorry but will short out the printButton() method. Removing all the logs. I did it. – Prashant Kumar Nov 04 '20 at 14:09
  • Are those Notifications required to reproduce the problem? – ollitietavainen Nov 05 '20 at 07:22
  • I removed all the unnecessary code. I hope someone will reply to me, what did I do wrong in the coding and not in posting :( . – Prashant Kumar Nov 05 '20 at 13:09
  • I have also noticed: It is always taking the previous Value. For example, In the initial click, it doesn't set anything and then changes the date it picks the previous values and not the current. – Prashant Kumar Nov 05 '20 at 16:10
  • Cleaning up the code example was helpful. It's somewhat confusing that you have both a component named `printButton` and a method `printButton`. – ollitietavainen Nov 06 '20 at 06:46

2 Answers2

1

You have two components here: a Button and an Anchor. Your Button is placed inside the Anchor in ConfigureReportButton. Your Button also gets a click listener in the constructor.

When you first click the button inside the anchor, the anchor has no href, so it does nothing. It also it executes the method printButton, which updates the href of the anchor. When you next click the button inside the anchor, the anchor has a href set, so it loads the file. It also executes the click listener of the button again, so the href gets updated.

To fix the issue, execute printButton() at the end of the constructor.

ollitietavainen
  • 3,900
  • 13
  • 30
0

Solved by not using Anchor but by using executeJavaScript (although it is depricated but working fine):

UI.getCurrent().getPage().executeJavaScript("window.open(\"" + reportURL + "\", \"_blank\");");
Prashant Kumar
  • 145
  • 1
  • 11