I would like to have a focus listener but the way I did it shows error in IDE. My code follows
txtField.addFocusListener(event ->{
#Override
public void focus() {
System.out.println("Focus");
}
});
Can you tell me what I am making wrong?
You are mixing up Java 6 and Java 8 syntaxes. You should just write
txtField.addFocusListener(event -> {
System.out.println("Focus");
});
Related
I created a thread (via a lambda expression) to fetch some data based on user input fields but when I try to click on dropdown menus while it is retrieving data I get the mini progress bar indicator. So is a new thread even being created? What am I doing wrong here?
Button doComputation = new Button("Get Results);
doComputation.addClickListener(event -> {
UI ui = UI.getCurrent();
new Thread(() -> {
// Do some work
ui.access(() -> layout.add(results);
}).start();
});
UPDATE: RESOLVED! Unneccesary ui.access calls were made, which locked up resources. Thank you to all that commented and helped.
The code looks correct to me (apart from the missing end parenthesis after ui.access). Is that the only ui.access call, and is that all you do inside it?
I made this example for reference, and the combo box stays responsive while the background task is running.
#Route
public class ThreadView extends VerticalLayout {
public ThreadView() {
Button runThreadButton = new Button("Start thread", e -> {
UI ui = UI.getCurrent();
new Thread(() -> {
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
ui.access(() -> Notification.show("Completed"));
}).start();
});
ComboBox<String> comboBox = new ComboBox<>("Items", "One", "Two", "Three");
add(runThreadButton, comboBox);
}
}
So is a new thread even being created?
You can verify that by adding a System.out.println("Thread started") and checking whether that message is printed, or by running your application in debug mode and setting a breakpoint on the part that you're interested in.
What am I doing wrong here?
The code that you showed looks fine. I would guess there's a problem outside the shown code, namely that you have forgotten that you also need to add the #Push annotation. Without that annotation, the server will have no way of directly sending messages to the client.
How can I acheive this in Vaadin.
// inside myButton click event
myButton.setEnabled(false);
doMyActionThatTakeSomeTime();
myButton.setEnabled(true);
Inside the event, the button is never disabled because the UI is not refresh.
What would be the best practice to do this in Vaadin 11 (or 10) ?
force view to refresh ? (how?)
put my action inside a Thread ? (how ?)
Edit SOLUTION - How to make it work with Thread
So far, example with Thread (working):
#Push
#Route(value = "secured")
public class MainView extends VerticalLayout
[ ... ]
// inside click event
UI ui = UI.getCurrent();
new Thread(() -> {
ui.access(() -> {
goButton.setEnabled(false);
ui.push();
});
doMyActionThatTakeSomeTime();
ui.access(() -> {
goButton.setEnabled(true);
ui.push();
});
}).start();
Sounds like the "Asynchronous updates" chapter in the docs explains what you want: https://vaadin.com/docs/v11/flow/advanced/tutorial-push-access.html . Basically: run doMyActionThatTakeSomeTime() in a separate background thread, then re-enable the button once the thread completes and Server Push will make sure the UI state is correctly updated.
This is frequently asked topic, there is also another answer here: How to dismiss Vaadin 8 confirmation dialog while performing lengthy operation Doing asynchronous updates work in Vaadin 8 and Vaadin 10+ in similar manner.
In Vaadin 8, there’s a Button::setDisableOnClick() method for this exact purpose.
That should probably be reintroduced in the newer versions as well.
For me the simplest way is this:
Button btnAction = new Button("Action");
btnAction.setDisableOnClick(true);
btnAction.addClickListener(e -> {
try {
for (int i = 0; i < 900000; i++) {
System.out.println(i);
}
} finally {
btnAction.setEnabled(true);
}
});
A better way of doing it:
UI ui = UI.getCurrent();
ui.access( () -> {
// disable button
goButton.setEnabled(false);
ui.push();
doMyActionThatTakeSomeTime();
// enable
goButton.setEnabled(true);
ui.push();
});
Is it possible to use default dart library html with angular dart?
ie:
class Test1Component implements OnInit{
#override
void ngOnInit() {
ButtonElement button = querySelector('button');
//Broken code, avoid button to be null.
button.onClick.listen(onClick);
}
void onClick(Event e){
print('Button clicked');
}
}
How can I avoid to get a 'null' button without the using any timers?
Basically I'm using only angular just for the Routes and but I'd like to stick with dart:html to control the DOM and events.
Yes, you can do that, but it's usually not a good idea.
Use instead #ViewChild(...) or similar Angular methods to get references to elements in a components view.
<button #myButton>click me</button>
#ViewChildren('myButton')
set myButton(List<Element> value) {
if(value.isNotEmpty) {
print(value.first);
}
}
If you want to just add a click handler using
<button (click)="onClick">click me</button>
would be the better way but it sounds you are somehow adding the button dynamically and adding a click handler declaratively might not work in this case (would need more info)
EDIT:
If someone like me want to use dart:html instead angular ng code, it's possible to use it
import 'package:angular/angular.dart';
import 'dart:html';
// AngularDart info: https://webdev.dartlang.org/angular
// Components info: https://webdev.dartlang.org/components
#Component(
selector: 'container',
template: '<h1>Test 1</h1><button #test1>Bottone test 1</button>',
)
class Test1Component implements OnInit{
#ViewChild('test1')
ButtonElement button;
#override
void ngOnInit() {
//Verified that button is initialized
print(button);
//Initialize click
button.onClick.listen((e) => print("Clicked"));
}
}
I am trying to add a very simple listener to my table header as specified in the book of Vaadin. I am using vaadin 6.4.5 in a liferay portlet. But the listener is never called...
table.addListener(new Table.HeaderClickListener() {
public void headerClick(HeaderClickEvent event) {
System.out.println("Column header clicked");
}
// Disable the default sorting behavior
});
table.setSortDisabled(true);
I am also unable to add footer to my table like this..
table.setFooterVisible(true);
table.setColumnFooter("Name", "Average");
table.setColumnFooter("Died At Age", String.valueOf(avgAge));
Both these code snippets were taken from the book of vaadin but they just don't work in my portlet application. please help
I have working Listener Yeah !
table.addHeaderClickListener(new HeaderClickListener() {
#Override
public void headerClick(HeaderClickEvent event) {
System.out.println("click Header");
Object object= event.getPropertyId();
}
});
I'm looking for a way to block/disable right click in javafx.scene.web.WebView. To be more specific I don't want the context menu to show up on right click. I'm new to the technology and can't find the way to do it.
Just for the record, it is implemented in JavaFX 2.2. See documentation for setContextMenuEnabled (JavaFX 2) and setContextMenuEnabled (JavaFX 8)
I've came up with working, but ugly, inelegant and, I'd say, partisan solution, which I don't really like, but actually I have no (or just can't find) other way out.
It includes modifying EventDispatcher of WebView.
So my implementation of EventDispatcher needs a reference to original WebView EventDispatcher and looks like that:
public class MyEventDispatcher implements EventDispatcher {
private EventDispatcher originalDispatcher;
public MyEventDispatcher(EventDispatcher originalDispatcher) {
this.originalDispatcher = originalDispatcher;
}
#Override
public Event dispatchEvent(Event event, EventDispatchChain tail) {
if (event instanceof MouseEvent) {
MouseEvent mouseEvent = (MouseEvent) event;
if (MouseButton.SECONDARY == mouseEvent.getButton()) {
mouseEvent.consume();
}
}
return originalDispatcher.dispatchEvent(event, tail);
}
}
Everytime event is dispatched it goes through our dispatcher and I check if it's right click. If it is I just consume it and proceed further.
To make it work you have to use WebView like that:
WebView webView = new WebView();
EventDispatcher originalDispatcher = webView.getEventDispatcher();
webView.setEventDispatcher(new MyEventDispatcher(originalDispatcher));
Every comment, clue and so on are appreciated.
With JavaFX 2.2+ it's now possible to set WebView's ContextMenuEnabled to false:
webView.setContextMenuEnabled(false);
WebView API Doc.
You can style context menus away using the following css.
.context-menu { -fx-background-color: transparent; }
.menu-item { -fx-background-color: transparent; }
.menu-item .label { -fx-text-fill: transparent; }
.menu-item:show-mnemonics .mnemonic-underline { -fx-stroke: -transparent; }
This will make all context menus and menu items transparent. I'm not sure of the css selector you could use to make this only apply to WebView context menus. If you don't have other menus in your application, that may not be a big deal.
You can do it with js (jquery used)
$(document).ready( function() { document.oncontextmenu = function() { return false } } );
Unfortunately it's not yet possible. There is a feature request for that, which you may want to track: http://javafx-jira.kenai.com/browse/RT-15684
This will will remove the context menu for the entire stage:
primaryStage.getScene().addEventFilter(MouseEvent.MOUSE_RELEASED,
new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
if (event.getButton() == MouseButton.SECONDARY) {
event.consume();
}
}
});