Javafx : Activate a tooltip with a button - button

I'm using JavaFx for a little app and a want to display a tooltip on a textArea when the user is clicking on a "help" button.
No problem for linking a tootltip to my textArea, but no way to activate it when the user click on the button. Is there a way to do that?

This is what you are looking for:
final Button helpButton = new Button("Help");
helpButton.setOnAction(new EventHandler()
{
public void handle(Event arg0)
{
showTooltip(stage, helpButton, "test tool tip", null);
}
});
public static void showTooltip(Stage owner, Control control, String tooltipText,
ImageView tooltipGraphic)
{
Point2D p = control.localToScene(0.0, 0.0);
final Tooltip customTooltip = new Tooltip();
customTooltip.setText(tooltipText);
control.setTooltip(customTooltip);
customTooltip.setAutoHide(true);
customTooltip.show(owner, p.getX()
+ control.getScene().getX() + control.getScene().getWindow().getX(), p.getY()
+ control.getScene().getY() + control.getScene().getWindow().getY());
}
Just pass the button as an input instead of control.

The ability to display a Tooltip on demand requires a resolution of RT-19538 Customizable visibility timing for Tooltip, which is not implemented in JavaFX 2.2.
As a workaround, you could try any of the possible strategies below:
Displaying your Tooltip data in a ContextMenu instead. With a ContextMenu, you have complete control over when it is shown.
You could create a custom PopupControl for your required functionality.
You could replace the default TooltipSkin with a custom implemented skin which allows you to control when the Tooltip is displayed.
You could implement RT-19538 and provide a patch to the Tooltip and TooltipSkin to the openjfx project.
3rd party libraries such as Jide's JavaFX Beta Release provide special classes like Decorator utilities, IntelliHints and ShapedPopups which might be useful in your case.

The following show a tooltip over control.
If controlhas a Tooltip assigned to, this tooltip is not chnaged.
public static void showOneTimeTooltip(Control control, String tooltipText) {
Point2D p = control.localToScreen(5 , 5);
final Tooltip customTooltip = new Tooltip(tooltipText);
customTooltip.setAutoHide(false);
customTooltip.show(control,p.getX(),p.getY());
PauseTransition pt = new PauseTransition(Duration.millis(2000));
pt.setOnFinished(e->{
customTooltip.hide();
});
pt.play();
}

Related

Proper way to add nodes using loop

I'm trying to make a small application that displays the contents of an arrayList but I have not been succesful. Currently I have a loop concatenating each object in the list with their toString method. This is not the solution I want for displaying the arrayList however. I want to be able to add a separate label and button for each object in the list so that I can press the button and open a window to change the item's data. Is there a way to add multiple javafx nodes using a loop AND add a parameter/id to a button? I know you can set an id to a button using fxml but I have to make this application without using fxml and have not been able to figure out how to do it otherwise.
Here is a code example of how I add the text right now
String list;
for (Registration registration : registrationList) {
list += registration.toString();
}
label.setText(list);
and what I've tried so far
for (Registration registration : registrationList) {
Label dynamicLabel = new Label(registration.toString());
Button dynamicButton = new Button("" + registration.getId());
layout1.getChildren().addAll(dynamicLabel, dynamicButton);
}
please let me know if something is unclear in my question, thanks in advance.
The problem is that you're thinking about the controls like data, and you need to treat them like regular objects. You're also thinking about the entire thing monolithically, which makes it difficult to see a solution.
Think about a Label + a Button as a distinct unit that works together. Define them together and you can freely have them talk to each other without figuring out ways to identify them and link them together later on.
Something like this will do what you're looking for:
public class ButtonActionSample extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
List<String> strings = List.of("String 1", "String 2", "String 3");
VBox vBox = new VBox(5);
vBox.getChildren().addAll(strings.stream().map(string -> createHBox(string)).collect(Collectors.toList()));
primaryStage.setScene(new Scene(vBox));
primaryStage.show();
}
private Node createHBox(String string) {
Label label = new Label(string);
Button button = new Button("Change Text");
button.setOnAction(evt -> new TextInputDialog(label.getText()).showAndWait().ifPresent(label::setText));
return new HBox(5, label, button);
}
}
When you do it like this, you don't need to hang on to references to any of the screen elements at all; just create them, set the action on the Button and stick them on the screen. They just work after that.

Interaction with parent control triggers RippleDrawable in Xamarin.Forns custom renderer

I have implemented a custom clickable label class in Xamarin.Forms along with a custom renderer, that adds a RippleDrawable as the controls Foreground. I am creating the RippleDrawable with the following code:
public static Drawable CreateRippleDrawable(Context context)
{
var typedValue = new TypedValue();
context.Theme.ResolveAttribute(Resource.Attribute.SelectableItemBackground, typedValue, true);
var rippleDrawable = context.Resources.GetDrawable(typedValue.ResourceId, context.Theme);
return rippleDrawable;
}
In my custom renderer I assign the drawable
this.Control.Foreground = DrawableHelper.CreateRippleDrawable(this.Context);
and update the ripple when the user touches the control
private void LinkLabelRenderer_Touch(object sender, TouchEventArgs e)
{
if (e.Event.Action == MotionEventActions.Down)
{
this.Pressed = true;
}
if (e.Event.Action == MotionEventActions.Cancel)
{
this.Pressed = false;
}
if (e.Event.Action == MotionEventActions.Up)
{
this.Ripple.SetHotspot(e.Event.GetX(), e.Event.GetY());
this.Pressed = false;
// raise the event of the Xamarin.Forms control
}
}
Now, whenever I click the control, the ripple will be shown, which is the expected behavior, but if I touch (tap or long-press) the parents of the control (e.g. the StackLayout, Grid or whatever layout contains the label, including their parent Layout, Page or View) the ripple animation will be triggered. Anyway, the event handler LinkLabelRenderer_Touch in not called in this case, only when the actual control is touched.
I can work around this behavior by adding an empty GestureRecognizer to the respective parent(s), but I really dislike this solution, because this is but a hack. And to make things worse it is a hack I'll always have to remember whenever I use the control.
How can I prevent the RippleDrawable being shown when the parent is touched?
Turned out I got things fundamentally wrong. Subscribing the Touch event is not the way to go. I had to make the control clickable and subscribe the Click event
this.Control.Clickable = true;
this.Click += LinkLabelRenderer_OnClick;
There is no need to handle all that RippleTouch stuff the way I did (via the Touch event) but could let android handle things for me.

Adding a button to a dialog

Are AX dialog buttons limited to OK and Cancel?
Is it possible to add a custom button to the dialog?
I have the following code for my dialog:
static void mitTabPage(Args _args)
{
Dialog dialog;
DialogGroup dialoggroup, dialoggroup2;
DialogField dialogfield, dialogfield2;
;
dialog = new Dialog ("A new Dialog");
dialog.addTabPage("Brand Id's");
dialoggroup = dialog.addGroup("Brand Id's");
dialogfield = dialog.addField(extendedTypeStr(SYCCarBrandId));
dialog.addTabPage("Owners");
dialoggroup2 = dialog.addGroup("Owners");
dialogfield2 = dialog.addField(extendedTypeStr(SYCOwner));
dialog.run();
}
I'd like to add another button to the dialog. How can I do that?
The Dialog framework is a simple framework for prompting users to obtain some data/settings then performing some action or canceling.
For what you're trying to do, it most likely doesn't make sense to use the dialog framework and instead you could/should create another form if you need additional functionality.
However, if you do insist on using the Dialog framework for this, you would add a runtime button and use registerOverrideMethod.
See following links:
https://msdn.microsoft.com/en-us/library/dialogfield.registeroverridemethod.aspx
https://blogs.msdn.microsoft.com/axsupport/2015/06/07/using-x-to-add-a-control-at-runtime/

How do I create a JavaFX Alert with a check box for "Do not ask again"?

I would like to use the standard JavaFX Alert class for a confirmation dialog that includes a check box for "Do not ask again". Is this possible, or do I have to create a custom Dialog from scratch?
I tried using the DialogPane.setExpandableContent() method, but that's not really what I want - this adds a Hide/Show button in the button bar, and the check box appears in the main body of the dialog, whereas I want the check box to appear in the button bar.
Yes, it is possible, with a little bit of work. You can override DialogPane.createDetailsButton() to return any node you want in place of the Hide/Show button. The trick is that you need to reconstruct the Alert after that, because you will have got rid of the standard contents created by the Alert. You also need to fool the DialogPane into thinking there is expanded content so that it shows your checkbox. Here's an example of a factory method to create an Alert with an opt-out check box. The text and action of the check box are customizable.
public static Alert createAlertWithOptOut(AlertType type, String title, String headerText,
String message, String optOutMessage, Consumer<Boolean> optOutAction,
ButtonType... buttonTypes) {
Alert alert = new Alert(type);
// Need to force the alert to layout in order to grab the graphic,
// as we are replacing the dialog pane with a custom pane
alert.getDialogPane().applyCss();
Node graphic = alert.getDialogPane().getGraphic();
// Create a new dialog pane that has a checkbox instead of the hide/show details button
// Use the supplied callback for the action of the checkbox
alert.setDialogPane(new DialogPane() {
#Override
protected Node createDetailsButton() {
CheckBox optOut = new CheckBox();
optOut.setText(optOutMessage);
optOut.setOnAction(e -> optOutAction.accept(optOut.isSelected()));
return optOut;
}
});
alert.getDialogPane().getButtonTypes().addAll(buttonTypes);
alert.getDialogPane().setContentText(message);
// Fool the dialog into thinking there is some expandable content
// a Group won't take up any space if it has no children
alert.getDialogPane().setExpandableContent(new Group());
alert.getDialogPane().setExpanded(true);
// Reset the dialog graphic using the default style
alert.getDialogPane().setGraphic(graphic);
alert.setTitle(title);
alert.setHeaderText(headerText);
return alert;
}
And here is an example of the factory method being used, where prefs is some preference store that saves the user's choice
Alert alert = createAlertWithOptOut(AlertType.CONFIRMATION, "Exit", null,
"Are you sure you wish to exit?", "Do not ask again",
param -> prefs.put(KEY_AUTO_EXIT, param ? "Always" : "Never"), ButtonType.YES, ButtonType.NO);
if (alert.showAndWait().filter(t -> t == ButtonType.YES).isPresent()) {
System.exit();
}
And here's what the dialog looks like:

JavaFX TreeView: remove expand / collapse button (disclosure node) & functionality

I want to have a TreeView that has all of its children permanently expanded, and I don't want the user to be able to expand or collapse any of the children.
To do this I've found that I need to do the following:
Remove icon with CSS (Done)
Change expand and collapse image TreeView JavaFX 2.2
[edit] Above link should be used to change image; to remove completely, use this solution: https://stackoverflow.com/a/27831191/4430591
Remove double click functionality (Done)
Disable TreeItem's default expand/collapse on double click JavaFX 2.2
[edit] Remove ability to collapse / expand using keyboard arrrow keys (Done)
Given in José Pereda's solution below ( https://stackoverflow.com/a/27831085/4430591 )
[edit] Remove ability to right click for a ContextMenu (Done)
Given in José Pereda's solution below ( https://stackoverflow.com/a/27831085/4430591 )
Remove icon's clickablity (How do I do this?)
[edit] solution: https://stackoverflow.com/a/27831191/4430591
Even though the icon is no longer visible, it's still clickable. I don't see any way of filtering this; I only see ways to be able to respond to it after the fact.
Also, if I'm missing anything else that I need to do to ensure this functionality, please let me know.
I feel quite silly. I think this was mostly just a matter of not knowing what that darn arrow was called. Apparently it's a disclosureNode? Maybe that's common knowledge.
In the custom defined TreeCell, all I did was add this line in the updateItem method:
setDisclosureNode(null);
The solution to avoid modifying the skin or the default behavior is more simple if we trap the clicks before they are dispatched, and consume the right ones.
For that we can use an EventDispatcher, to filter both the mouse pressed and the right click over the arrows, which are StackPane nodes:
class CellEventDispatcher implements EventDispatcher {
private final EventDispatcher original;
public CellEventDispatcher(EventDispatcher original) {
this.original = original;
}
#Override
public Event dispatchEvent(Event event, EventDispatchChain tail) {
if (event.getEventType().equals(MouseEvent.MOUSE_PRESSED) ||
event.getEventType().equals(ContextMenuEvent.ANY)){
event.consume();
}
if(event instanceof KeyEvent && event.getEventType().equals(KeyEvent.KEY_PRESSED)){
if((((KeyEvent)event).getCode().equals(KeyCode.LEFT) ||
((KeyEvent)event).getCode().equals(KeyCode.RIGHT))){
event.consume();
}
}
return original.dispatchEvent(event, tail);
}
}
Now we apply our custom dispatcher to the tree view:
#Override
public void start(Stage primaryStage) {
TreeView<String> tree = new TreeView<>();
...
EventDispatcher treeOriginal = tree.getEventDispatcher();
tree.setEventDispatcher(new CellEventDispatcher(treeOriginal));
Scene scene = new Scene(tree);
primaryStage.setScene(scene);
primaryStage.show();
}
This will consume any click (left or right) over the arrows on the tree.
EDIT
Added to the event dispatcher class the case where the user uses the keyboard to traverse the tree view, consuming the collapse/expand events with arrow LEFT or RIGHT.

Resources