How to add context menu to a canvas element? - java

Problem : It is not possible to simply 'add' ContextMenu to a Canvas or Pane element via addContextMenu(menu), which works only with javafx.scene.control elements (and neither Canvas or Panel extends this class).
Question : Is there any 'clean' way to 'register' ContextMenu item to a Canvas element? I expect standard behavior of this menu (to show after RMB clicked on Canvas element, autohide when clicked with LMB etc.).

Canvas canvas = ... ;
ContextMenu menu = ... ;
canvas.setOnContextMenuRequested(e -> menu.show(canvas, e.getScreenX(), e.getScreenY()));

James_D's solution may not dismiss the menu when clicking the canvas while the context menu is visible. See this bug :
https://bugs.openjdk.java.net/browse/JDK-8095591
So instead, I suggest using :
Canvas canvas = ... ;
ContextMenu menu = ... ;
canvas.setOnContextMenuRequested(e -> menu.show(canvas.getScene().getWindow(), e.getScreenX(), e.getScreenY()));

Maybe you can use Chrome extension's contextMenu. here is the official document.
The only issue so far I find is that the Context type for a canvas is not clear. ["all"] works but is not a good way.

Related

Is there a way to put a anchor component in an context menu in vaadin 14?

Im trying to get a vaadin anchor component inside of an context menu, but I cant get it to work.
I tried it like following:
this.contextMenu.add(anchorFile);
But the menu item inside of the context menu wont appear.
I hope someone can help me.
Thanks in advance
Have you set the text content for your Anchor component?
The proper way to add the Anchor as menu item is this.
ContextMenu menu = new ContextMenu(targetComponent);
Anchor vaadin = new Anchor("https://vaadin.com/","Vaadin");
menu.addItem(vaadin);
add methods also adds the component to the menu, but wont't wrap as menu item. It is more purposed for adding decorative components like dividers etc.
ContextMenu menu = new ContextMenu(targetComponent);
Anchor vaadin = new Anchor("https://vaadin.com/","Vaadin");
menu.add(vaadin);
Moreover with addIten method you can set the event listener for menu item being selected:
ContextMenu menu = new ContextMenu(targetComponent);
Anchor vaadin = new Anchor("https://vaadin.com/","Vaadin");
menu.addItem(vaadin, event -> {
System.out.println("Selected");
});

JavaFX How to get a children (WebView) from a Tab?

I have just started to do things with JavaFX and I'm trying to 'build' an browser. Right now I have a TabPane with two tabs. One tab has a WebView and the other tab is able to add new tabs. Over the TabPane is a Textfield. When I enter an internet adress I want to load a website in the selected Tab.
My Problem is, that I dont know how to get the Webview inside the selected Tab. I was able to get the selected Tab, but I have no Idea how to get the WebView inside it.
int index = TabPane.getSelectionModel().getSelectedIndex();
Tab selectedTab = TabPane.getTabs().get(index);
You could always call selectedTab.getContent(), and then navigate down through the scene graph hierarchy until you get to the right element. E.g. if your tab content is a BorderPane and the WebView is in the center you could do
BorderPane selectedBorderPane = (BorderPane) selectedTab.getContent();
WebView selectedWebView = (WebView) selectedBorderPane.getCenter();
This is pretty ugly code, though, and you'd have to rewrite it any time you changed the layout, which means your application becomes hard to maintain.
A (much) better way would be to create a variable at the appropriate scope (controller for the view that displays the tab pane, or the class that contains it if you're doing the layout in Java) for the current web view:
private WebView currentWebView ;
Then whenever you create a new tab containing a web view, add a listener to the tab's selectedProperty:
Tab tab = new Tab(...);
// ...
WebView webView = new WebView(...);
// ... layout, etc tab content, etc...
tab.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
if (isNowSelected) {
currentWebView = webView ;
}
});
Now currentWebView always references the currently displayed web view. (You will also need to initialize it to the first web view displayed at startup.)

JavaFX8 How to pass the "press" status to child?

I have an ImageView in a Pane, and the pane has been set a MouseEventHandler to do some actions.
The ImageView's Image is defined in a CSS file as below:
.imageView{
-fx-image: url("image/normal.png");
}
.imageView:pressed{
-fx-image: url("/image/press.png");
}
Under normal status, the ImageView's image is 'normal.png'. When the user presses the ImageView, its image will be changed to 'press.png'. Then, when released, its image will be changed back to 'normal.png'.
However, if the user does not press on the ImageView's image exactly, even though Pane's MouseEvent is trigged, the image will not be changed.
How can I do to change ImageView's image when its parent Pane is pressed. For instance, on Android, the image will be switched automatically. It is very convenient.
Add a style class to the pane ("image-container", for example), then in the css do
.imageView{
-fx-image: url("image/normal.png");
}
.image-container:pressed .imageView {
-fx-image: url("/image/press.png");
}

jPopupMenu at the position of Hyperlink in Java Swing

I am implementing an editor in Swing. It shows plain text with hyperlinks. The problem statement is that when a user clicks on a hyperlink, a popup menu must appear at the mouse position or just below the hyperlink text. User can select the option from the menu and the text of hyperlink will be updated to the text in the option user selected. Moreover, after selecting the option, the menu disappears.
I am trying it on both jTextPane and jEditor pane. I used hyperlinkUpdate listener to display popup menu, but it always appears at the top left corner of the GUI.
Moreover, I could not find how to update the text of the hyperlink.
Here is the code of hyperlinkUpdate event:
private void jEditorPane1HyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
if(evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
System.out.println(evt.getDescription());
jPopupMenu1.setAlignmentX(jEditorPane1.getMousePosition().x);
jPopupMenu1.setAlignmentY(jEditorPane1.getMousePosition().y);
jPopupMenu1.setVisible(true);
}
}
Someone kindly guide me in this matter.
Thanks in advance.
The call should be like this
JPopupMenu popup = new JPopupMenu();
popup.add("Item 1");
popup.add("Item 2");
popup.show(mouseEvent.getComponent(),mouseEvent.getX(),mouseEvent.getY());
See an example here
http://www.experts-exchange.com/Programming/Languages/Java/Q_20143329.html

opption menu Jelly bean

How will it be possible to have a menu button which call option-menus with the following condistions ( must be ):
App hides the titlebar :
requestWindowFeature(Window.FEATURE_NO_TITLE);
the targetversion in Manifest is set to 16 :
android:targetSdkVersion="16"
Some Infos and researches from my side:
Setting targetSdkVersion="10" shows the menu still in the bottom, which I would like to achieve.
Showing the titlebar shows the menubutton in top ( 3 points icon ) and the menu is also callable. But I need to hide the titlebar.
any hints , suggestions ?
thanks & regards
Andreas
Another option is to use slider menu and have it on left or right. You shouldn't include button on action bar (like facebook), you can open menu after item click or onFling event.
There is nice and easy tutorial that I tried:
http://oodlestechnologies.com/blogs/Facebook-Style-Slide-Menu-In-Android

Categories