I create a window by scene builder and I want to override some controls gettext() and paste() method. But I don't know how to do it because I didn't create these controls by coding but by scene builder.
How can I override these controls methods?
You can't do that, you have to extend the custom control class (If it's not final class) to override any of it's methods.
Related
I've written a simple custom control which extends HBox and contains a TextField and Button. As for many controls, it makes sense to have a label alongside describing its purpose, and I want users on mnemonic-capable platforms to access the control easily, more specifically giving focus to the button instance.
My first thought was to assign a focus listener to the custom control, on an assumption that the target node would be focused on label activation, but this doesn't work. On delving into JavaFX source, I find the Label.labelForProperty() uses a NodeHelper$NodeAccessor.setLabeledBy(Node, Node) method, but can't get beyond that as I can't find a solid implementation of the NodeHelper$NodeAccessor interface. My guess is that this utility class only maps associations with targets it deems suitable, and for some reason my custom control isn't.
My second thought is to open up access to the button, which can then be directly assigned as the target, but for obvious reasons this is a poor choice, and I'd far prefer to leave the button inaccessible.
Has anyone encountered this scenario and found a viable solution/workaround without compromising the visibility of internal implementation details of a custom control?
This is a simple workaround that doesn't need your Button to have public visibility:
public class YourCustomClass {
private TextField tf;
private Button btn;
public void registerLabel(Label label) {
label.setLabelFor(btn);
}
}
I have an FXML file that I'm using to allow user input when requested. Right now I just put it in a new stage and do Stage.show(). I would like to not have it appear in a new window and behave more like a ContextMenu.
Looking at ContextMenu class it doesn't appear that I can set the content based off an FXML file. Is there a way to do this either with ContextMenu or Popup or some other class I am unaware of?
Although that library is quite nice, I wanted something simple that didn't require 3rd party downloads. I came up with this:
Popup popup = new Popup();
CustomController controller = new CustomController();
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlfile));
loader.setController(controller);
popup.getContent().add((Parent)loader.load());
The problem was I didn't realize that a Parent could be considered a Node for the method Popup#getContent#add
ControlsFX has a PopOver control you might like. That PopOver can use any Node for its content, so you can simply create a popover, load a node from FXML and set the content of the popover to that node.
I am looking for a solution to define editable properties of custom controls. Jens Deters built already superb custom controls with fontawesomefx for JavaFX.
After the import of the jar-files into Scene Builder you are able to use custom controls (drag & drop). The best thing is that you are able to style the symbols directly in the properties menu. You are able to style special node properties on the fly.
My question is, how it is possible to create editable custom (node) properties directly in the properties menu of Scene Builder? Is there a workflow?
Once you have added your custom component, if you click on it, the right panel containing the section Properties, Layout and Code are updated.
In the Properties section, there is on the top a Custom subsection which contains your custom properties, in the following example I added Error Text, Field Type and Title text:
To add these custom properties, you have to add them as attributes in the custom component controller:
private ObjectProperty<FieldType> fieldType = new SimpleObjectProperty<>(FieldType.NONE);
private StringProperty titleText = new SimpleStringProperty();
private StringProperty errorText = new SimpleStringProperty();
These attributes shall be properties, no #FXML is needed, if you enter a value, for example NONE for the enum, then it will be set as default value in the scene builder custom property
It is possible to add a context menu to a scroll pane, but not to other types of panes. Why?
How FXML Works
FXML works by introspecting on the Java API using reflection (or by using specialized builder classes). More information on FXML works can be found in the Introduction to FXML documentation.
Why ContextMenus can't be defined on Panes in JavaFX using FXML Markup
Control has a contextMenu property. A ScrollPane is a Control. Other pane types such as StackPane are not controls. As there is no corresponding property in these other pane types which could be set to contain a reference to a contextMenu, you can't define a contextMenu on these pane types using FXML.
For similar reasons, you can't define a Tooltip on a Pane either.
How to define a ContextMenu for a Panes in an FXML Controller
You can still set a context menu on panes (and any other arbitrary nodes which are not controls) via code, using the contextMenu show API, for example by placing the following code in your FXML controller.
#FXML StackPane stack;
// . . .
public void initialize() {
final ContextMenu contextMenu = new ContextMenu(new MenuItem("xyzzy"));
stack.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
contextMenu.show(
stack,
mouseEvent.getScreenX(),
mouseEvent.getScreenY()
);
}
});
}
Why not add a ContextMenu property
Node could have a contextMenu property, which would allow ContextMenus to be defined on Panes via FXML Markup.
The reason why Node does not have a contextMenu property but Control does is because ContextMenu is itself a Control. Adding a ContextMenu property to node would mean that the core scene graph model code for the JavaFX implementation would have a dependency on the controls module (which would have a dependency on the scene graph module), hence a circular dependency. This would prevent the shipping of a very light Java runtime system which included the core JavaFX scene graph and rendering engine but did not include controls (not that anybody ships such a system today).
How to File Feature Requests
If you think the system should be changed to allow definition of context menus on arbitrary panes using SceneBuilder, then you can file a feature request against the JavaFX issue tracker (if you do so, include a link back to this question in the feature request).
Described method to open popup leads to multiple popups open if every node in the scene graph want to open context menu. Consuming of event is definitely needed.
See also discussion at Using FXML to Create ContextMenu within a Pane It provides working answer to this problem.
BTW, Node.onContextMenuRequested(...) should be used instead, yes?
I'm new to Qt programming and I am developing a drawing application. I have a class MyWidget which has a member QGraphicsView. MyWidget is a member of another class MainWidget (which has other widgets as well and all of them are in a layout).MainWidget is the central widget of my subclass of QMainWindow.
I have created functions to scale the view with the mouse wheel event and a function to drag the scene around.
The problem is - I want to set the Scene's size to be fixed, and to be 3 times the size of the view, but since the view is managed by a layout in order to take as much space as possible I can't get the view's size?
Any help appreciated.
the size property will give you the current size of your widget:
http://qt-project.org/doc/qt-4.8/qwidget.html#size-prop
Alternatively, you could subclass QGraphicsView and re-implement the resizeEvent:
http://qt-project.org/doc/qt-4.8/qwidget.html#resizeEvent
For a full example, have a look into:
http://qt-project.org/doc/qt-4.8/widgets-scribble.html
You can definitely get the view's size. There are two ways to go about it:
Attach an event filter object to your view: myView->installEventFilter(filterObject). The filterObject's eventFilter method will be invoked for all events reaching your view, including resize events.
Use a custom view class and reimplement resizeEvent. This method gets called each time the widget is resized. Do note that designer allows you to add custom classes without having to write plugins.