1

I'm new to Vaadin and I want to implement this: when a button is clicked a window containing a calendar is opened, and the user should choose a date in the calendar. The user should be blocked until he chooses the date, so I wrote this:

public class KopivaadincomponentsUI extends UI {

@Override
protected void init(VaadinRequest request) {
    final VerticalLayout layout = new VerticalLayout();
    layout.setMargin(true);
    setContent(layout);


    Button button = new Button("Click Me");
    button.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {

          DateChooser dateChooser = new DateChooser(new com.kopiright.xkopi.lib.type.Date(2013, 12, 9));
          UI.getCurrent().addWindow(dateChooser);

            synchronized (Lock.getInstance()) {
              while (dateChooser.isVisible()) {
            try {
              Lock.getInstance().wait();
            } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
              }
         }

        }
      });
    layout.addComponent(button);
}

Here's the DateChooser—the window containing the calendar:

public class DateChooser extends Window implements ValueChangeListener{


public DateChooser(Date date){
  this.setModal(true);
  final FormLayout content = new FormLayout();
  calPane = new CalendarPane();
  content.addComponent(calPane);
  this.setContent(content);
  lock =new Lock();

}


/*package*/ class CalendarPane extends InlineDateField {

public CalendarPane() {
  setImmediate(true);
  setResolution(RESOLUTION_DAY);
  setShowISOWeekNumbers(true);
  }

  private static final long serialVersionUID = -3958329773743250969L;
}

private CalendarPane        calPane;
private static final long  serialVersionUID = 1L;

@Override
public void valueChange(ValueChangeEvent event) {
  synchronized(Lock.getInstance()) {
    String date = String.valueOf(event.getProperty().getValue());
    Calendar    cal = Calendar.getInstance(Locale.FRANCE);
    cal.setTime((java.util.Date) event.getProperty().getValue());

     setSelectedDate(new NotNullDate(cal));
     this.close();
    Lock.getInstance().notify();

   }

  }

 }

The problem is that when I click the button it waits infinitely. Any ideas will be appreciated.

matts
  • 6,738
  • 1
  • 33
  • 50
Amira
  • 3,184
  • 13
  • 60
  • 95

2 Answers2

1

Don't reinvent a wheel. What you want is a modal dialog. There's a question about them here which has links to all the information you need.

Community
  • 1
  • 1
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • 1
    Modal in vaadin prevents interaction from other forms, it does not stop the thread from executing further instructions. – SureshS Feb 03 '16 at 11:00
  • @SureshS That's true. The OP didn't ask how to stop a thread from executing - they asked how to block the user from doing things other than choosing the date. – Dawood ibn Kareem Feb 04 '16 at 02:29
  • You're right. The body of the question asks something different from the title. Who's to say which of the two I should have answered? – Dawood ibn Kareem Feb 04 '16 at 06:51
0

In fact Swing has blocking dialogs. The show() method in fact blocks until the user clicks a button and the dialog obtains a result. This is done by show() running its own nested event dispatcher loop as described here: How can Swing dialogs even work?

Unfortunately this is not possible with Vaadin. However, there is a clever technique called Coroutines which allows you to use Vaadin dialogs as if they were blocking: http://mavi.logdown.com/posts/3488105-vaadin-kotlin-coroutines

Martin Vysny
  • 3,088
  • 28
  • 39