gwt suggestbox doesn't hide on page scroll - java

I've created a suggest box and generated HTML page with huge text, so I can scroll.
1. Show suggest list
2. Scroll page
The popup box with suggestion list moves with scrolled page, but I want that it will neither hide when page scrolls nor move with page.
As I understand that suggest popup has absolute position. But is there some non css solution.
Answer to:
I've tried add scroll handler to Window, but I've found out that handle only event when I have and right of them moves, only in this case. If I have one scroll to scroll page like in case with large text - nothing invokes

When constructing a SuggestBox you can provide your own SuggestOracle, TextBox and SuggestionDisplay. DefaultSuggestionDisplay can be used to hide suggestion list. You can do it in Window.scrollHandler.
Here is the code:
MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
oracle.add("one");
oracle.add("two");
oracle.add("three");
TextBox box = new TextBox();
final DefaultSuggestionDisplay display = new DefaultSuggestionDisplay();
SuggestBox suggestBox = new SuggestBox(oracle, box, display);
Window.addWindowScrollHandler(new ScrollHandler() {
#Override
public void onWindowScroll(ScrollEvent event) {
display.hideSuggestions();
}
});
Note, that you need to use DefaultSuggestionDisplay - see documentation on deprecated hideSuggestionList method.
I hope that the example explains it all.
I've also checked that if you don't use own SuggestionDisplay it uses DefaultSuggestionDisplay anyway. So you can do it even simpler.
((DefaultSuggestionDisplay) suggestBox.getSuggestionDisplay()).hideSuggestions();
EDIT:
If not the whole window is scrolled but only content of some panel, you can add a ScrollHandler to the panel:
panel.addDomHandler(new ScrollHandler() {
#Override
public void onScroll(ScrollEvent event) {
((DefaultSuggestionDisplay) suggestBox.getSuggestionDisplay()).hideSuggestions();
}
}, ScrollEvent.getType());

Related

How to Print in Vaadin

I have a VerticalLayout filled with components in Vaadin. PLease i will to print this layout exactly as it is to an A4 paper. Any Idea how to do this, Code Sample will be great.
Straight out of the Vaadin doc…
Printing the Browser Window
Vaadin does not have special support for launching the printing in browser, but you can easily use the JavaScript print() method that opens the print window of the browser.
Button print = new Button("Print This Page");
print.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
// Print the current page
JavaScript.getCurrent().execute("print();");
}
});
The button in the above example would print the current page, including the button itself. You can hide such elements in CSS, as well as otherwise style the page for printing. Style definitions for printing are defined inside a #media print {} block in CSS.
There's no "built-in" API for printing in Vaadin.
You have to use the browser's native printing API.

Where to set the focus in wizard pages?

I have a Wizard containing two wizard pages (org.eclipse.jface.wizard.WizardPage) and would like to set the focus for each page separately so that always the top input field of each page is focused.
Setting the focus in WizardPage.createControl(Composite), the first page focus is set correctly. The second page does not have a focus.
This is due to Wizard.createPageControls(Composite) which creates all the pages at the beginning.
Where would be the place to handle the focus after switched to the next wizard page?
Override the WizardPage setVisible method and set focus when the page becomes visible:
#Override
public void setVisible(boolean visible) {
super.setVisible(visible);
if (visible) {
// TODO set focus
}
}
JFace wizards don't offer a designated hook to set the focus. However, as Greg already mentioned, the setVisible() method can be used to set the initial focus of a wizard page.
Usually, the focus of wizard pages should only be set when showing the page for the first time. If a user returns back to a page, the focus should remain where it was when the page was left.
Therefore, I usually guard the focus code so that it is only executed when the page is shown for the first time:
private boolean firstTimeShown = true;
#Override
public void setVisible( boolean visible ) {
super.setVisible( visible );
if( visible && firstTimeShown ) {
firstTimeShown = False;
control.setFocus();
}
}

Vaadin TabSheet. Additional functionality in Tab captions

I m a newbie in Vaadin and I am using a TabSheet where I attach some tabs. I was wondering if I could get some more functionality from their captions.
For instance, when a tab is selected and the user clicks its caption then a dropdown menu can appear.
I have already experimented with the existing listeners of TabSheet and I concluded that I have to make a custom listener. The thing is that I cannot find a way to fire the event when I click on the selected tab caption...
You could use SelectedTabChangeListener on a TabSheet. It would fire the event when the caption is clicked, and you could reselect your current tab, etc.. It would be cumbersome.
Why not use the MenuBar? Isn't it exactly what you want? https://demo.vaadin.com/valo-theme/#!menubars
I had the same problem (Vaadin 7.4), what you can do is you can surround the tab caption with a <div onclick="...">captionText</div> and add a JavaScript callback function:
final TabSheet sheet = new TabSheet();
sheet.setTabCaptionsAsHtml(true); // don't forget this!
// This is the callback
JavaScript.getCurrent().addFunction("clickedTab", new JavaScriptFunction() {
#Override
public void call(JsonArray arguments) {
LOGGER.info(arguments.getString(0)); // this is 'some identifier'
}
});
final TabSheet.Tab tab = sheet.addTab(component, "<div onclick=\"clickedTab('some identifier');\">captionText/div>");

GWT How to Layout Widgets within a Panel using CSS

I have two labels, two datepickers, and a submit button within a DockLayoutPanel. I'm trying to just get the button to show up in the center of the panel.
Here is the code I am trying to get just to get the button centered:
Button b = new Button("Submit", new ClickListener()
{
public void onClick(Widget sender)
{
getAwards(text.getText(),text2.getText());
}
});
b.setWidth("80px");
b.addStyleName("gwt-Button");
and within my css:
.gwt-Button{
margin-left:auto;
margin-right:auto;
}
What edits or method do I have to take to actually get this to work?
Edit: For extra information, this button is being added to a DockLayoutPanel
Try:
.gwt-Button{
position:absolute;
left:50%;
margin-left: -40px; /* Width Button /2 */
}
Try disabling the loading of whatever default GWT theme you are using (such as clean) in your gwt.xml file, it may be messing with things, since you're using the same class name.
Alternatively, just use a CSS class name other than "gwt-Button" here.

Using the FileDownloader with Combobox

I've tried using the new FileDownloader in Vaadin7. Unfortunately, it needs an AbstractComponent for the "extend" component (where it listens for the clicks)
Is there a way to use it with combobox items? As they are not AbstractComponents and thus do not fit with the "extend" method.
The Vaadin forums have discussed this a lot, and there is no scheme now using FileDownloader or the similarly functioning BrowserWindowOpener. They all only work on AbstractComponents, and thus don't work on Action handlers for Table and Tree, or row click handlers on Table, or MenuItem in Menu, etc. The same applies to selected elements in their various select boxes.
You have to revert to the popup window style (so browsers will need to allow popups for it to work) using a regular click/valuechange listener, creating a Resource and passing it to the deprecated, but still working, Page.getCurrent().open(Resource...) method.
Here is my work-around. It works like a charm for me. Hope it will help you.
This example is for the MenuItem, but you can modify for ComboBox.
Create a button and hide it by Css (NOT by code: button.setInvisible(false))
final Button downloadInvisibleButton = new Button();
downloadInvisibleButton.setId("DownloadButtonId");
downloadInvisibleButton.addStyleName("InvisibleButton");
In your theme, add this rule to hide the downloadInvisibleButton:
.InvisibleButton {
display: none;
}
When the user clicks on menuItem: extend the fileDownloader to the downloadInvisibleButton, then simulate the click on the downloadInvisibleButton by JavaScript.
menuBar.addItem("Download", new MenuBar.Command() {
#Override
public void menuSelected(MenuBar.MenuItem selectedItem) {
FileDownloader fileDownloader = new FileDownloader(...);
fileDownloader.extend(downloadInvisibleButton);
//Simulate the click on downloadInvisibleButton by JavaScript
Page.getCurrent().getJavaScript()
.execute("document.getElementById('DownloadButtonId').click();");
}
});

Categories

Resources