I have a horizontal split panel in which I want to show the image selected in combobox but unable to set the datasource for image file.
FilesystemContainer container = new FilesystemContainer(new File("C:/myData/wallpaper"));
ComboBox box = new ComboBox("Documents", container);
#Override
protected void init(VaadinRequest request) {
setContent(box);
com.vaadin.ui.HorizontalSplitPanel horizontalSplitPanel = new com.vaadin.ui.HorizontalSplitPanel();
setContent(horizontalSplitPanel);
horizontalSplitPanel.addComponent(box);
//horizontalSplitPanel.addComponent(label);
final Image image = new Image();
horizontalSplitPanel.addComponent(image);
box.addValueChangeListener(new ValueChangeListener() {
#Override
public void valueChange(ValueChangeEvent event) {
image.setData(event.getProperty().getValue());
///label.set//setPropertyDataSource( (Property) ImageIO.read((ImageInputStream) new TextFileProperty((File) event.getProperty().getValue())));
}
});
box.setImmediate(true);
How can I set datasource for images.I'm very new in Vaadin.
I suggest this way:
#Override
public void valueChange(ValueChangeEvent event) {
image.setSource(new FileResource((File)box.getValue()));
}
Related
I defined a custom FieldEditorPreferencePage. In createFieldEditors method I construct a simple interface with a custom FieldEditor that holds button that loads a file. When a user loads the file the interface region (where the button stands) need to change and show other specific gui. How to invalidate the FieldEditorPreferencePage? I used the layout() method of the parent composite but nothing happens. What am I doing wrong here?
public class MyPage extends FieldEditorPreferencePage {
...
private Composite fieldEditorParent;
private boolean fileLoaded = false;
#Override
protected void createFieldEditors() {
fieldEditorParent = this.getFieldEditorParent();
MyFieldEditor compositeFieldEditor = new MyFieldEditor(fieldEditorParent) {
if(fileLoaded) {
//draw file content
} else {
Button chooseButton = new Button(buttonsComposite, SWT.NONE);
chooseButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
//choose file
fileLoaded = true;
//invalidate page <-- THIS DOES NOT WORK!
fieldEditorParent.layout(true, true);
}
}
}
}
}
i cannot get a navigator to run within my project.
i always get the Error
"Component must be attached to a session when getConnectorId() is called for the first time"
i followed several examples where it ran out of the box with nealy the exact same setup but i cannot get it to run within my environment and i don't know why.
This is my MainUI
#SpringUI
public class MainUI extends UI implements View{
HorizontalLayout root;
#Override
protected void init(VaadinRequest request) {
root = new HorizontalLayout();
VerticalLayout menu = this.createMenuBar();
menu.setWidth("20%");
root.addComponents(menu);
Panel viewPanel = new Panel();
root.addComponentsAndExpand(viewPanel);
setContent(root);
final Navigator navigator = new Navigator(this, viewPanel);
navigator.addView("Food", FoodView.class);
navigator.addView("Meal", MealView.class);
navigator.addView("", MainView.class);
// even with
//navigator.addView("Main", MainView.class);
//navigator.navigateTo("Main");
// i get the same error
}
#Override
public void enter(ViewChangeEvent event) {
// TODO
}
private VerticalLayout createMenuBar() {
VerticalLayout menuBar = new VerticalLayout();
for(String s: new String[] {"Food", "Meal"}) {
menuBar.addComponents(this.createNavigationButton(s, this.getNavigator()));
}
return menuBar;
}
private Button createNavigationButton(final String state, final Navigator navigator) {
return new Button("Go go" + state, new Button.ClickListener() {
#Override
public void buttonClick(ClickEvent event) {
navigator.navigateTo(state);
}
});
}
}
This is my Main/Food/Meal View, they are only stumps with only a label because i just wanted to test a Multipage example
#SpringView(name = MainView.VIEW_NAME)
public class MainView extends UI implements View {
public static final String VIEW_NAME = "Main";
#Override
protected void init(VaadinRequest request) {
VerticalLayout root = new VerticalLayout();
setContent(root);
Label lbl = new Label("MainView");
root.addComponent(lbl);
}
#Override
public void enter(ViewChangeEvent event) {
}
}
I am new to SpreadSheet functionality of ControlsFx Api. I would like to open Dialog on right click of Spreadsheetcell of SpreadsheetView in Javafx. Any help is greatly appreciated.
this is code where you can off the standard ContextMenu and implements own handler with Dialog, in this example TextInputDialog:
SpreadsheetView spreadsheetView = new SpreadsheetView();
//off the standard ContextMenu
spreadsheetView.setContextMenu(null);
//set own handler for right click with Dialog
spreadsheetView.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
#Override public void handle(ContextMenuEvent event) {
CellView cellView = (CellView) event.getTarget();
TextInputDialog dialog = new TextInputDialog(cellView.getText());
Optional<String> result = dialog.showAndWait();
if (result.isPresent()){
System.out.println(cellView.getText());
}
}
});
I don't know very good this library, but it works good.
Example how it works:
My program:
public class MainController extends Application {
public static void main(String[] args) {
launch(args);
}
#Override public void start(Stage primaryStage) throws Exception {
SpreadsheetView spreadsheetView = new SpreadsheetView();
//off the standard ContextMenu
spreadsheetView.setContextMenu(null);
//set own handler for right click with Dialog
spreadsheetView.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
#Override public void handle(ContextMenuEvent event) {
CellView cellView = (CellView) event.getTarget();
TextInputDialog dialog = new TextInputDialog(cellView.getText());
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
System.out.println(cellView.getText());
}
}
});
HBox hBox = new HBox();
hBox.getChildren().add(spreadsheetView);
Scene scene = new Scene(hBox);
primaryStage.setScene(scene);
primaryStage.show();
}
}
It is using mouse handler on the table view which checks when mouse is clicked and on clicking it fires a new dialogue in fx and then accepts the input and updates the fx table view.
table.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (event.getClickCount() == 1) {
Call dialogue method of java fx
}
}
});
Or if you want right click you can create cell
Eg
FirstNameCol.setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
#Override
public TableCell<Person, String> call(TableColumn<Person, String> col) {
final TableCell<Person, String> cell = new TableCell<>();
cell.textProperty().bind(cell.itemProperty()); // in general might need to subclass TableCell and override updateItem(...) here
cell.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (event.getButton == MouseButton.SECONDARY) {
// handle right click on cell...
// access cell data with cell.getItem();
// access row data with (Person)cell.getTableRow().getItem();
}
}
});
return cell ;
}
});
I have a menubar in my vaadin application and want to add an item to open a pdf-file is a new tab of the browser. I found some solutions to open files with a button, but I have to use an MenuItem...
MenuBar.Command commandHandler = new MenuBar.Command() {
#Override
public void menuSelected(MenuItem selectedItem) {
if (selectedItem.equals(menu_help)) {
openHelp();
}
}
};
...
menu_help = menuBar
.addItem("", WebImageList.getImage(ImageList.gc_helpIcon),
commandHandler);
...
private void openHelp() {
// open pdf-file in new window
}
Thanks for help!
SOLUTION:
private void openHelp() {
final String basepath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();
Resource pdf = new FileResource(new File(basepath + "/WEB-INF/datafiles/help.pdf"));
setResource("help", pdf);
ResourceReference rr = ResourceReference.create(pdf, this, "help");
Page.getCurrent().open(rr.getURL(), "blank_");
}
Attention: This code works, but the the structure of code is not perfect ;-) Better is to store "basepath" and "pdf" as attribute...
There is a similar problem described here: How to specify a button to open an URL?
One possible solution:
public class MyMenuBar extends MenuBar {
ResourceReference rr;
public MyMenuBar() {
Resource pdf = new FileResource(new File("C:/temp/temp.pdf"));
setResource("help", pdf);
rr = ResourceReference.create(pdf, this, "help");
}
private void openHelp() {
Page.getCurrent().open(rr.getURL(), "blank_");
}
...
}
The setResource method of AbstractClientConnector is protected, so this is you need to extend some Vaadin component to make it work. This is why Im creating the class MyMenuBar here. If you are using an external resource you don't need to attach it to any component with setResource and then this is not nessecary.
I used the following code to do something similar:
private Component buildUserMenu() {
final MenuBar settings = new MenuBar();
settings.addStyleName("user-menu");
final User user = getCurrentUser();
settingsItem = settings.addItem("", new ThemeResource(
"img/logo.png"), null);
updateUserName(null);
settingsItem.addItem(Lang.getMessage("menu.edit"), new Command() {
#Override
public void menuSelected(final MenuItem selectedItem) {
ProfilePreferencesWindow.open(user, false);
}
});
settingsItem.addSeparator();
settingsItem.addItem(Lang.getMessage("menu.help"), new Command() {
#Override
public void menuSelected(final MenuItem selectedItem) {
Window help = new Window();
help.setWidth("90%");
help.setHeight("90%");
BrowserFrame e = new BrowserFrame("PDF File", new ThemeResource("pdf/ayuda.pdf"));
e.setWidth("100%");
e.setHeight("100%");
help.setContent(e);
help.center();
help.setModal(true);
UI.getCurrent().addWindow(help);
}
});
settingsItem.addSeparator();
settingsItem.addItem(Lang.getMessage("menu.logout"), new Command() {
#Override
public void menuSelected(final MenuItem selectedItem) {
BarsEventBus.post(new UserLoggedOutEvent());
}
});
return settings;
}
I have problem with return value from my method. I created an Image and I want scale it and return.
final Image img = new Image(src);
img.addLoadHandler(new LoadHandler() {
#Override
public void onLoad(LoadEvent arg0) {
resize img...
}
}
return img;
How do I return it after I have changed its size?
Need not return the image just to re-size.
The Image should be added to the DOM first.Then you can do operations on that .
You can do something like this :
Image image = new Image();
image.addLoadHandler(new LoadHandler() {
#Override
public void onLoad(LoadEvent event) {
// resize image
image.getElement().getStyle().setVisibility(Style.Visibility.Visible);
}
});
image.getElement().getStyle().setVisibility(Style.Visibility.HIDDEN);
RootPanel.get().add(image);
image.setUrl(url);