I've tried to add a method inside MyUi.java class in vaadin, but all I get are errors. So, is it actually possible to do so? The reason why I ask is this: basically MyUi.java class has a button which when clicked opens up another window (the code for this other window sits in a different class). This button is removed (button.setVisible(false);) when clicked and I added a addCloseListener to the new window so that when that window is closed it fires an event and calls a function which will allow me to re-display the button. This function needs to sit inside the MyUi class as I can't figure out how to access the button which currently sits inside MyUi class from a different class.
Some code to make things a little clearer: MyUi.java contains the button
public class MyUI extends UI {
#Override
protected void init(VaadinRequest vaadinRequest) {
final VerticalLayout layout = new VerticalLayout();
final NewWindow newWindow = new NewWindow();
final UploaderComponent uploaderComponent = new UploaderComponent();
//final TextField name = new TextField();
// name.setCaption("Type your name here:");
final Button button = new Button("Click Me");
button.addClickListener(new Button.ClickListener()
{
#Override
public void buttonClick(ClickEvent event)
{
//button.setVisible(false);
// TODO Auto-generated method stub
getUI().addWindow(newWindow.getWindow());
newWindow.getWindow().setContent(uploaderComponent.formLayout);
}
});
layout.addComponents(button );
layout.setMargin(true);
layout.setSpacing(true);
setContent(layout);
public void testMethod(){//produces errors
}
}
}
And the window class:
public class NewWindow
{
//public static final Sizeable.Unit PIXELS;
Window window = new Window();
public NewWindow(){
window.setStyleName("wrappingWindow");
window.setWidth("620px");
window.center();
window.addCloseListener(new CloseListener()
{
#Override
public void windowClose(CloseEvent e)
{
System.out.println("window closed");
}
});
}
public Window getWindow(){
return window;
}
}
Related
I've been trying to build a simple GUI with JavaFX (I'm completely new to JavaFX) and I've found myself stuck. In every tutorial I've found event handling is done on the level of the UI object, mostly with annonymous inner classes - what I want to accomplish is to move the event handlers to controller class, and inject references to them trough methods called on controller's (and view's) instantiation.
My small GUI is properly build and displayed, the reference is indeed passed, but for a reason the handle() method is not invoked, and I can't find the reason why.
The View:
//imports here
public class View extends Application implements ViewInterface, Runnable {
private Menu fileMenu;
private Menu storheouseMenu;
private MenuBar menuBar;
private Scene scene;
private MenuItem exitItem;
public View() {
initialize();
}
public void initialize() {
fileMenu = new Menu("Plik");
storheouseMenu = new Menu("Magazyn");
MenuItem exitItem = new MenuItem("Exit");
MenuItem displayStorehouse = new MenuItem("Display");
fileMenu.getItems().addAll(exitItem);
storheouseMenu.getItems().add(0, displayStorehouse);
}
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = new VBox();
scene = new Scene(root, 400, 200);
primaryStage.setScene(scene);
primaryStage.setTitle("Szefowa test");
menuBar = new MenuBar();
menuBar.getMenus().addAll(fileMenu, storheouseMenu);
((VBox) scene.getRoot()).getChildren().addAll(menuBar);
primaryStage.setResizable(false);
primaryStage.show();
}
public void addFileMenuListeners(EventHandler<ActionEvent> eventHandler) {
exitItem = fileMenu.getItems().get(0);
exitItem.setOnAction(eventHandler);
}
public void addStorehouseMenuListeners(EventHandler<ActionEvent> eventHandler) {
MenuItem displayStorehouse = fileMenu.getItems().get(0);
displayStorehouse.setOnAction(eventHandler);
}
public void displayMessage(String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Ping");
alert.setContentText(message);
}
//other methods here
}
The Controller:
package kitke.szefowa.controller;
//imports here
public class Controller implements ControllerInterface {
private Model model;
private View view;
public Controller(Model model, View view) {
this.model = model;
this.view = view;
this.view.addFileMenuListeners(new FileMenuListener());
this.view.addStorehouseMenuListeners(new StorehouseMenuListener());
}
public class FileMenuListener implements EventHandler<ActionEvent> {
#Override
public void handle(ActionEvent event) {
//do some stuff
}
}
public class StorehouseMenuListener implements EventHandler<ActionEvent> {
#Override
public void handle(ActionEvent event) {
//do some stuff
}
}
}
}
PS I've no such problem while build the GUI with Swing so the issue is connected with JavaFX.
I have tested your code by manual instantiation as:
Controller controller = new Controller( this );
in View.start() method. The event handlers are working as expected with only small problem. Both in addFileMenuListeners() and addStorehouseMenuListeners() methods you are setting the event handler to the same menuitem fileMenu.getItems().get(0). So calling of these method one after another, second invocation is overriding the setOnAction of the first one.
So change the addStorehouseMenuListeners() to:
public void addStorehouseMenuListeners( EventHandler<ActionEvent> eventHandler )
{
MenuItem displayStorehouse = storheouseMenu.getItems().get(0);
displayStorehouse.setOnAction( eventHandler );
}
Hello I've just started programming and now i'm making a university project where I must get a gwtcontainer from another class and let it see in a popup window, don't know exactly how I can call it
This is a part of my code:
import com.is.lap.client.gui.Login;
public class StartRU extends TabPanel {
private static class MyPopup extends PopupPanel {
public MyPopup() {
super(true);
// Is here where i have the trouble don't know exactly how to do it
setWidget( (Widget) new Login().getLayoutData());
center();
}
}
public StartRU() {
Button LoginButton =new Button("Login");
LoginButton.setWidth("100px");
LoginButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// Instantiate the popup and show it.
new MyPopup().show();
}
});
If Login class extends Widget, you can simply
setWidget(new Login());
I've got window class implementation with annotation #Component. Inside this class I declare object with annotation #Autowired.
On my window form I've got a button Create which should read data from TextFields, create new object and store it in the database.
#Component("newProjectWindow")
public class NewProjectWindow {
private Window createProjectWindow;
#Autowired
private ProjectService service;
public Window createWindow() {
createProjectWindow = new Window("New project");
initWindow();
fillWindow();
return createProjectWindow;
}
private void initWindow() {
createProjectWindow.setSizeUndefined();
createProjectWindow.setResizable(false);
createProjectWindow.setModal(true);
createProjectWindow.addCloseListener(new CloseListener(){
#Override
public void windowClose(CloseEvent e) {
Notification.show("Closed");
}
});
}
private void fillWindow() {
final TextField projectName = new TextField("Project name");
final TextField projectOwner = new TextField("Project owner");
Button create = new Button("Create");
create.addClickListener(new Button.ClickListener() {
#Override
public void buttonClick(ClickEvent event) {
Project newProject = new Project();
newProject.setProjectName(projectName.getValue());
newProject.setProjectOwner(projectOwner.getValue());
//save it somehow
}
});
Button close = new Button("Cancel");
close.addClickListener(new Button.ClickListener() {
#Override
public void buttonClick(ClickEvent event) {
createProjectWindow.close();
}
});
HorizontalLayout layout = new HorizontalLayout(create, close);
FormLayout formLayout = new FormLayout(projectName, projectOwner, layout);
formLayout.setMargin(true);
createProjectWindow.setContent(formLayout);
}
}
However the problem is how to store object in the database. I've got no access to instantiated ProjectService(which uses ProjectRepisitory which uses SqlSessionTemplate and etc.) because it is under control of Spring - and anonymous ClickListener is not.
But how to store object?
I tend not to use anonymous inner methods for click listeners, but instead get my own classes to implement the ClickListner. So in your example I would change the class like this:
#Component("newProjectWindow")
public class NewProjectWindow {
private Window createProjectWindow implements Button.ClickListener;
#Autowired
private ProjectService service;
private Button create = new Button("Create", this);
private Button cancel new Button("Cancel", this);;
public Window createWindow() {
createProjectWindow = new Window("New project");
initWindow();
fillWindow();
return createProjectWindow;
}
private void initWindow() {
createProjectWindow.setSizeUndefined();
createProjectWindow.setResizable(false);
createProjectWindow.setModal(true);
createProjectWindow.addCloseListener(new CloseListener(){
#Override
public void windowClose(CloseEvent e) {
Notification.show("Closed");
}
});
}
private void fillWindow() {
final TextField projectName = new TextField("Project name");
final TextField projectOwner = new TextField("Project owner");
HorizontalLayout layout = new HorizontalLayout(create, close);
FormLayout formLayout = new FormLayout(projectName, projectOwner, layout);
formLayout.setMargin(true);
createProjectWindow.setContent(formLayout);
}
#Override
public void buttonClick(ClickEvent event) {
if (event.getButton() == cancel)
{
createProjectWindow.close();
}
else
{
Project newProject = new Project();
newProject.setProjectName(projectName.getValue());
newProject.setProjectOwner(projectOwner.getValue());
//save it somehow
}
}
}
To access service from listener in your example, consider following solutions:
Anonymous inner classes can reference outer class (using OuterClassName.this syntax - in your case NewProjectWindow.this.service).
You can declare (inner) class and pass appropriate references to it.
You can use Chris M suggestion of parent class implementing listener interface itself.
I have a MenuItem that has a ScheduledCommand attached. When the user clicks on the menu, a new PopupPanel appears that has autoHide enabled. Now when the user clicks on the MenuItem while the popup is open, the panel gets closed, but immediately opens again as the PopupPanel's close event fires as a click event on the menu item. Can somebody tell me how can I prevent the PopupPanel from opening in this case?
My code is something like this:
#UiField
protected MenuItem menuItem;
....
menuItem.setScheduledCommand(new ScheduledCommand() {
#Override
public void execute() {
PopupPanel window = new PopupPanel();
window.init();
window.addCloseHandler(new CloseHandler<PopupPanel>() {
#Override
public void onClose(final CloseEvent<PopupPanel> event) {
// TODO Maybe something here?
}
});
window.show();
}
});
Create a single instance for the popup and use PopupPanel#isShowing method to hide or show the popup.
public class MyMenuClass{
private PopupPanel window;
....
....
menuItem.setScheduledCommand(new ScheduledCommand() {
#Override
public void execute() {
if(window==null){
window = new PopupPanel(true);
window.add(new Label("Hello close me!!!"));
}
if(window.isShowing()){
window.hide();
}else{
window.show();
}
}
}
OK, I managed to do this by checking whether the last hovered element on the Menubar was the menuItem that opens the window. To do this, I had to subclass the default MenuBar class and exposing the getSelectedItem() method (it was protected by default, why?)
#UiField
MyMenuBar myMenuBar;
....
menuItem.setScheduledCommand(new ScheduledCommand() {
#Override
public void execute() {
if (!wasHoveredWhenClosed) {
window.init();
window.addCloseHandler(new CloseHandler<PopupPanel>() {
#Override
public void onClose(final CloseEvent<PopupPanel> event) {
wasHoveredWhenClosed = myMenuBar.getSelectedItem() != menuItem;
}
});
window.show();
} else {
wasHoveredWhenClosed = false;
}
}
});
I'm very new in Vaadin and JavaEE at all and I have I think basic question, but will be glad for help.
I have 3 classes in my Vaadin project, here they are:
Main class, responding only for starting and creating navigator:
public class MyprojectUI extends UI {
public Navigator navigator;
public static final String SECOND_VIEW = "SecondView";
#WebServlet(value = "/*", asyncSupported = true)
#VaadinServletConfiguration(productionMode = false, ui = MyprojectUI.class)
public static class Servlet extends VaadinServlet {
}
#Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
ComponentContainerViewDisplay viewDisplay = new ComponentContainerViewDisplay(layout);
navigator = new Navigator(UI.getCurrent(), viewDisplay);
navigator.addView("", new FirstView());
navigator.addView(SECOND_VIEW, new SecondView());
}
}
And two views class:
public class FirstView extends HorizontalLayout implements View {
TextArea text = new TextArea();
Button button = new Button("go");
#Override
public void enter(ViewChangeEvent event) {
this.addComponent(text);
this.addComponent(button);
button.addClickListener(new ClickListener() {
#Override
public void buttonClick(ClickEvent event) {
getUI().getNavigator().navigateTo(MyprojectUI.SECOND_VIEW);
}
});
}
}
-
public class SecondView extends HorizontalLayout implements View {
Label label = new Label("Passed text here");
#Override
public void enter(ViewChangeEvent event) {
this.addComponent(label);
}
}
How can I pass a data from my TextArea to second view? I want set label text to text which was in TextArea when "go" button is clicked.
Thanks for help.
I recommend you apply Model-View-Presenter pattern. View is already made. Model is not needed here. Just write a Presenter.
In MVP, Presenter is triggered by view, then presenter collects required data from view, does some calculation and updates view. In current case it could look like:
class Presenter {
FirstView firstView;
SecondView secondView;
public Presenter (FirstView firstView, SecondView secondView) {
this.firstView = firstView;
this.secondView = secondView;
}
public void goClicked(UI ui) {
secondView.label.setValue(firstView.text.getValue());
ui.getNavigator().navigateTo(MyprojectUI.SECOND_VIEW);
}
}
Code is simplified, recommended practice is define an interface for view instead direct fields access, but main idea is the same.
SecondView secondView = new SecondView();
navigator.addView(SECOND_VIEW, secondView );
Presenter presenter = new Presenter(firstView, secondView);