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.)
Related
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");
});
I want to create a "focus" effect once the user clicks an item in the recycler view. Look at the image below, the second one is the effect I wish to have.
I've tried to get the RecyclerView's clicked item and bring it to the front and show a dark/transparent overlay covering all the others.
This is my view hierarchy:
RelativeLayout
--- Recycler View
--- View (dark overlay)
I've tried with view.bringToFront();, view.setZ();, view.setElevation(); but none of them works.
Now I guess this is about the layout hierarchy. How to solve this problem?
you can just change the background color of touch item view and text color. when user click on it .
I achieved this effect by sending the item view to the Window.DecorView.
You can just make a copy of the desired view you want to put in the front (in this case you want the layout used by the ViewHolder) and then
((ViewGroup)activity.getWindow().getDecorView()).addView(yourView);
after that you can set Translations according to the original view. The new view will be in front of the dark overlay in the same place the previous view is
EDIT: I have a sample of what you can do:
The following code is C# (using Xamarin.Android), it should be easy to turn to Java.
var productView = productsLayoutManager.FindViewByPosition(position);
var cardView = productView.FindViewById<CardView>(Resource.Id.product_layout);
int[] location = new int[2];
productView.GetLocationOnScreen(location);
int left = location[0];
int top = location[1];
selectedView = LayoutInflater.Inflate(Resource.Layout.ProductRow2, null);
productsAdapter.CloneItemViewHolder(productView, ref selectedView);
selectedView.LayoutParameters = new FrameLayout.LayoutParams(cardView.LayoutParameters);
((ViewGroup)activity.Window.DecorView).AddView(selectedView, cardView.Width, cardView.Height);
selectedView.SetX(left);
selectedView.SetY(top);
selectedView.RequestLayout();
activity.DimBackground();
The cardView is the main layout of the RecyclerView item Viewholder.
This is actual code in use, producing this:
I have webpage but I don't want navigation menu and search box. Can we float the webview container?
How to hide through android-webview?
mWebView.setVisibility(View.GONE); & mWebView.setVisibility(View.VISIBLE);
Do not use INVISIBLE setting.
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.
I have created some tabs in a TabPane. Each time I make a tab it has got a close(x) button on its right side. I don't want the tabs to be removed from the TtabPane so I have used:
TabPane tabPane = new TabPane();
Tab tab = new Tab("new tab");
tab.setContents(new Label("Please help"));
tabPane.getTabs().add(tab);
tab.setOnCloseRequest(e -> e.consume());
so that it won't be removed. Is there some way not to display this close button on tab.
Any help is appreciated.
You can set the TabClosingPolicy on a TabPane
myTabPane.setTabClosingPolicy(TabClosingPolicy.UNAVAILABLE);
There are the following possibilities:
TabClosingPolicy.ALL_TABS // all tabs can be closed
TabClosingPolicy.SELECTED_TAB // only the selected tab can be closed
TabClosingPolicy.UNAVAILABLE // you cant close
If you are adding classes to myTabPane.getTabs() there is also the possibility to set the class to not be closeable (because it needs to extend from Tab):
setClosable(false);
If you define it in the class which extends from Tab I guess the policy you set will be useless and is overridden.
Link to the oracle doc:
JavaFX 8 TabPane.TabClosingPolicy
You can also define this using FXML by this code:
<TabPane tabClosingPolicy="UNAVAILABLE">