html body gwt click event - html

html file has two textbox and one button.
but i need to generate click event when i only click outside of the two textboxes and button
element.how can i do that.
RootPanel.get().addEventListener or something like that?? help.

Typing anywhere in the browser window will trigger alert pop-up:
Event.addNativePreviewHandler(new Event.NativePreviewHandler() {
#Override
public void onPreviewNativeEvent(NativePreviewEvent event) {
NativeEvent ne = event.getNativeEvent();
if (KeyDownEvent.getType().getName().equals(ne.getType())) {
Window.alert("who fired me last?"
+ event.getNativeEvent().getCurrentEventTarget()
+ "\nevent target:" + event.getNativeEvent().getEventTarget());
}
}
});

I don't know, if RootPanel.get().addEventListener works, but you can add another panel, which contains the three elements. To the new panel you can add an listener.

Related

Make JavaFX Button visually appear to be clicked on right click

I converted some old Java Swing code to JavaFX. The JavaFX code had explicit doClick() for the right mouse button calls:
myButton.addMouseListener(new MouseAdapter() {
#Override
public final void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
// ...
}
else if (SwingUtilities.isRightMouseButton(e)) {
// ...
myButton.doClick();
}
}
});
Left clicks make the button visually appear to be clicked in Java Swing. However right clicks do not visually do this without adding myButton.doClick()
I am seeing the same visual behavior in JavaFX and I want right clicks to visually make the button look clicked. Below is my JavaFX code:
myButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(final MouseEvent event) {
if (event.getButton() == MouseButton.PRIMARY) {
// ...
}
else if (event.getButton() == MouseButton.SECONDARY) {
// ...
}
}
});
What do I have to add to make right clicks visually click myButton?
That visual appearance of button click is usually referred as "armed" pseudo state. So you can turn on/off the armed pseudo state of the button when doing right button pressed&released.
Something like..
myButton.setOnMousePressed(e->{
if(e.getButton()== MouseButton.SECONDARY){
myButton.pseudoClassStateChanged(PseudoClass.getPseudoClass("armed"), true);
}
});
myButton.setOnMouseReleased(e->{
if(e.getButton()== MouseButton.SECONDARY){
myButton.pseudoClassStateChanged(PseudoClass.getPseudoClass("armed"), false);
}
});
Updating the :armed pseudo class is not enough if you want the Button's action to be fired by a right click. You actually need to arm the Button. In other words, you need to make the armed property change to true. Also, updating the armed property will update the pseudo class for you.
As the armed property is read-only, you can't set it directly; you need to call arm() and disarm(). You may also need to manually call fire() (expanded on below). Here's an example:
Button button = new Button("Click Me!");
button.setOnAction(event -> System.out.println("Button clicked!"));
button.setOnMousePressed(event -> {
if (event.getButton() == MouseButton.SECONDARY) {
button.arm();
}
});
button.setOnMouseReleased(event -> {
if (button.isArmed()) {
button.disarm();
button.fire();
}
});
However, you don't appear to need the onMouseReleased handler at all—at least in JavaFX 11 and 12, using the default Button's skin/behavior. The Button's behavior class will fire the action if said Button is armed at the time the mouse is released (and no keys are down). Note that the default behavior class does a more complex check regarding which MouseButton was used (i.e. it does more then just check event.getButton() == MouseButton.PRIMARY). You can see the implementation for yourself here.
All that said, if you only want the visuals to change then you should use the approach shown in Sai's answer.

Extjs4: how do I add a listener when I change my panel

How do I add a listener when I change my panel to another panel in same tab?
I know there is a function is tabchange, when my tab is changed and listener is working.
Like this:
tabchange:function(tabPanel, tab){
if(tab.id == "selecttraffic"){
setclickmap = 'line';
map.addControl(clickmap);
clickmap.activate();
} else if(tab.id == "selectrange"){
setclickmap = 'range';
map.addControl(clickmap);
clickmap.activate();
}
}
But I can't find a similar function work to my panel change!!!
Thanks!
You need to describe it more specifically. Event tabchange fires when a new tab has been activated/selected. What do you mean here by change my panel to another panel in same tab?

How do I remove focus from a button, so that pushing the spacebar or Enter doesn't trigger the button?

When you click or tap a button, it gives it focus, so that when you push the spacebar or Enter, it triggers the button again. I don't want that to happen.
I tried this: button1.Focus(Windows.UI.Xaml.FocusState.Unfocused);
But the unwanted behavior still happens (pushing the spacebar or Enter triggers the button again). I also tried setting focus to another button using the Programmatic and Keyboard FocusStates but that doesn't fix it either.
Any help, especially an explanation of why this is happening, would be greatly appreciated.
You can set the TabStop property to False, but be aware that doing this disallows user from using the button by keyboard.
<Button Content="Click Me By Pointer!" IsTabStop="False"/>
Update
As msdn states:
You can't remove focus from a control by calling this method with FocusState.Unfocused as the parameter. This value is not allowed and causes an exception. To remove focus from a control, set focus to a different control.
So another way to achieve this, is setting focus to previously focused element when the button is about to get focus by pointer. Unfortunatly the GettingFocus event is not working for the Button class (maybe because final version is not released yet?) so we should use GotFocus event, it's ugly it but works.
bool moveFocus = false;
public MainPage() {
this.InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e) {
if (moveFocus) {
UIElement focusedElement = FocusManager.FindNextFocusableElement(FocusNavigationDirection.Right);
if (focusedElement is Control) {
((Control)focusedElement).Focus(FocusState.Programmatic);
}
}
}
private void Button_GotFocus(object sender, RoutedEventArgs e) {
Button button = (Button)sender;
moveFocus = (button.FocusState == FocusState.Pointer);
}
Note that I passed FocusNavigationDirection.Right to FocusManager.FindNextFocusableElement to get an element in right, I should have used FocusNavigationDirection.Previous instead to get previous element but it returns null for unknown reason.

Handle text selection wp8 Webbrowser

I am using WP8 webbrowser control to show an html page and xaml is like this
<Grid Grid.Row="1" >
<phone:WebBrowser IsScriptEnabled="True" x:Name="mainBrowserControl">
<tools:GestureService.GestureListener>
<tools:GestureListener DragCompleted="GestureListener_DragCompleted"/>
</tools:GestureService.GestureListener>
</phone:WebBrowser>
</Grid>
What i want to do is to show an application bar when user selects some text in browser.And for doing that i listen to DragCompleted event and show the Applicationbar when some selection of text is there.The code used is
private void GestureListener_DragCompleted(object sender, DragCompletedGestureEventArgs e)
{
string selected_text = "";
try
{
selected_text= //get selected text from browser
}
catch { }
if (!string.IsNullOrEmpty(selected_text.Trim()))
{
Show Applicationbar menus
}
}
But the problem with this approach is if user simply selects a text in browser , the default copy icon is visible but how can i show the applicationbar menu as well in the bottom(Since i am not dragging the selection , just make a selection only - more like double click) See the image attached
First question. you can get selected content in WebBrowser via this
function GetSelectedText() {
window.external.Notify(document.selection.createRange().htmlText);
}
in your GestureListener_DragCompleted you call browser.InvokeScript("GetSelectedText");
you can get the text in browser.ScriptNotify event
Second question, I have no idea too. If you get a solution, Please let me know. Thank you!

Detect tap in page?

I have a PhoneApplicationPage with some grids, images and a few buttons. If I tap outside my buttons, anywhere in the page (on an image, grid or whatever), I want to open a new page.
How can I detect a tap/click anywhere on the page?
I see at least two ways to do that:
Listen to the MouseLeftButtonUp event on your PhoneApplicationPage. It should be triggered by images and labels but not by buttons
Listen to the Tap event on your PhoneApplicationPage. However, this event will be triggered even when the user tap on a button. To prevent this, you can retrieve the list of controls at the tap coordinates in the event handler, and open the new page only if there's no button:
private void PhoneApplicationPage_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
var element = (UIElement)sender;
var controls = VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(element), element);
if(controls.OfType<Button>().Any())
{
return;
}
// Open the new page
}