Javafx : Disabling Menu (MenuButton) auto close - java

I have a MenuButton that contains only CheckMenuItems. My user will usually check several items and if he has to re-open the menu for each one, he will soon throw his mouse through the screen.
I choose to use a menubutton rather than a combobox because it seems that it's not possible to put checkboxes into a combobox (https://community.oracle.com/thread/2598157).
Anyone has an idea ?
Thank you very much,
Léo

Consider in addition to the menu items to provide a toolbar with toggle buttons.
Note: Drombler FX provides an action framework to keep the state and logic between menu items and toolbar buttons in sync. It supports CheckMenuItems and toggle buttons as well.
Disclaimer: I'm the author of Drombler FX.
Getting Started: http://wiki.drombler.org/GettingStarted
Blog: http://puces-blog.blogspot.ch/search/label/Drombler

This worked for me:
#FXML
public void autoShow() {
checkmenuitem.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
e.consume();
}
});
}
checkmenuitem is the id. When it is clicked, the handle method is executed. For a detailed explanation on the event.consume() method, see this:
What is the meaning of Event consumes in JavaFX
Place the above method in your controller class and then call it from the initialize method in your controller class:
#Override
public void initialize(URL location, ResourceBundle resources) {
autoShow();
}

Related

ZK 8.5.0 how to override button widget setLabel function

The ZK setLabel() function of Button widget does not work; when the code runs to the line like foobutton.setLabel(mystring), the button disappears from the browser.
In the eclipse IDE, if I hover on the setLabel() function, the IDE shows this message:
If label is changed, the whole component is invalidate.Thus, you want to smart-update, you have to override this method.
Using ZK 8.5.0
Inside the controller class, I declare:
#Wire
Button delSelectedMonitor;
Inside the controller, I implement a class which implements EventListener:
public class onClickHolderEditMode implements EventListener{
public void onEvent(Event event) throws Exception {
clickedDivEditMode = (Div) event.getTarget();
clickedDivIdEditMode = clickedDivEditMode.getId().split(myUtil.monitorholderString)[1];
String curName = getCamNameById(clickedDivIdEditMode);
delSelectedMonitor.setLabel("DELETE:"+clickedDivIdEditMode+","+curName);
}
}
event binding:
tmpdiv.addEventListener("onClick", new onClickHolderEditMode());
My expectation is that when someone clicks the tmpdiv, the button delSelectedMonitor will change its label according to the property of tmpdiv. However as I say previously, the button is just disappearing.
https://www.zkoss.org/wiki/ZK_Client-side_Reference/General_Control/Widget_Customization
I have tried the section "Specify Your Own Widget Class" at the above website link, but the browser will be pending.
Please help, thank you.
I would prefer a different approach.
Why not use a
<button label="#load(vm.xyz)" ... />
(I wrote using MVVM pattern) and modify variable xyz in clicking action?
Check out http://books.zkoss.org/zk-mvvm-book/8.0/syntax/load.html for implementing guide.

How to handle several events for the same Node in JavaFX using only one Event Handling class?

everyone. I've been searching for this question but I haven't found it here, so I'll guess it's really simple.
I'm creating a very simple application in JavaFX with a single button. Now I want to handle its events (like when it's pressed or when it's released), but when I see examples over the Internet, they all use anonymous classes (and a different class for each event), which makes the code dirty in my opinion. That's why I want to put the event handlers in a separate class and add them to the button.
The problem is that I don't know if I have to create a different class for every event, which I think isn't cool. So I came up with an idea. In the handle() method of the class I check which type of event is going on and process it.
This is the code
Main class
public class Main extends Application{
Button button;
PruebaEventHandler evhandler;
public Main() {
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("h0i");
button = new Button("Púlsame!");
evhandler = new PruebaEventHandler();
button.addEventHandler(MouseEvent.ANY, evhandler);
StackPane layout = new StackPane();
layout.getChildren().add(button);
Scene scene = new Scene(layout, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
}
EventHandler class
public class PruebaEventHandler implements EventHandler<MouseEvent>{
#Override
public void handle(MouseEvent event){
if(event.getEventType().equals(MouseEvent.MOUSE_PRESSED)){
System.out.printf("Te cogí\n");
}
if(event.getEventType().equals(MouseEvent.MOUSE_RELEASED)){
System.out.printf("\nMe ha soltado!!!");
}
}
}
I don't know if this is very inefficient or bad programming style, but is the only solution I've come up with so far. So I want to ask you if this is a good solution or, if there's a better one, shed your light on me! Thanks beforehand.
There already exists a solution for this, which is creating a JavaFX-project with a FXML-file, Controller and also a Main class. IDEs like IntelliJ and NetBeans have support for letting you create a JavaFX-project, which automatically creates those files for you, but I'm not sure if you have to add a plugin to make it work, that I don't remember.
The FXML-file takes care of the GUI, for example placing a button in a scene, and the easiest way to use it is with a SceneBuilder, which Oracle has, and can also be integrated in your IDE.
If you use FXML you can direct buttons to methods inside your FXML-document, so you don't need to have anonymous classes for event handlers. Instead you make the button call a spesific method in your Controller-class.
Here are a couple of youtube-tutorials that showcase the basics of using JavaFX with FXML:
https://www.youtube.com/watch?v=K7BOH-Ll8_g
https://www.youtube.com/watch?v=LMdjhuYSrqg

Using one JavaFX method for multiple Buttons

Right now I'm using Eclipse Luna, JavaFX and SceneBuilder. I have ~40 buttons, and I'd like to use a generic "buttonPressed" action method that every button can use. Something like this:
public void buttonPressed(ActionEvent event, Button b) {
b.setText("Pressed");
}
When I change the On Action panel in SceneBuilder however, I get the following Exception when I try to run my program:
javafx.fxml.LoadException: Error resolving onAction='#buttonPressed', either the event handler is not in the Namespace or there is an error in the script.
Is there a step I missed? Or does anyone know of an alternate way to use one method to control the on-click behavior of multiple buttons?
Any help appreciated!
As in your comment, the only signatures allowed for an onAction attribute are either zero arguments, or a single argument which is an ActionEvent.
You can get the source of the event as follows:
#FXML
public void buttonPressed(ActionEvent event) {
Object source = event.getSource();
// ...
}
and of course if you know you only registered the handler on buttons, you can do
#FXML
public void buttonPressed(ActionEvent event) {
Button button = (Button) event.getSource();
// ...
}

JavaFX getting scene from a controller

I recently started playing around with Java FX, FXML, and scene builder, and I've been trying to add key listeners to one of the controllers for a scene. When I do this though, the key listeners don't work as they should, and I figure it's because they're not focused onto that particular scene. I tried to get access to the scene the controller was part of in order to set it directly, but it comes up that it's part of a null scene.
Is there a way to gain access to the scene that this controller is used in in order to try and assign key event and listeners to that particular scene? Should I go through the rootController which is static throughout the whole application? Or, better yet, is there a simpler way of going about this?
Most examples I see assume that everything is mostly together in a main class or separated amongst a couple of other classes without FXML being brought in, and I'm not sure how to apply their fixes when I have the java controllers, FXML pages, and the main application all separated.
Thanks for any help!
Use any of the controls that is bound in the Controller and use getScene() on it.
Remember not to use it in initialize() as the root element(though completely processed) is still not placed on the scene when initialize() is called for the controller
public class WindowMainController implements Initializable {
#FXML
private Button button;
#FXML
private void handleButtonAction(ActionEvent event) {
System.out.println(button.getScene()); // Gives you the Scene
}
#Override
public void initialize(URL url, ResourceBundle rb) {
System.out.println(button.getScene()); // Prints null
}
}

How to get the TrimBar of a TrimWindow in Eclipse RCP 4?

I put a toolbar on the top of my TrimmedWindow in my application. I have a handler which has to check whether a check button is pressed on this menu bar or not.
I tried putting EMenuService in my execute() method of the handler but it has no useful methods. If I debug into my application I can see my menu in the EMenuService object however.
How can I get my menu from the Eclipse context?
Without code it's hard to help you.
But the basic idea for your handler is the following :
public class BrokerHandler {
#Inject
// the services you need
#Execute
public void execute(IEclipseContext context, #Named(IServiceConstants.ACTIVE_SHELL) Shell shell)
throws InvocationTargetException, InterruptedException {
// do some stuff
}
}
Then, in your application.e4xmi you need to create a Window>Trimmed Window>Trim Bars>Window Trim>Toolbar>Handled Tool Item wich points to your Commands>Command that is binded to your Handlers>Handler pointing to your java class with a method annotated #Execute as described above.
Then each execution of the #Execute method means the user has pressed the toolbar button.
You can pass messages to other parts of your app with the event broker service, or store some of your own stuff in the IEclipseContext.
You can have a look here: http://xseignard.github.com/demoCamp2012/prez/#1
Hope this helps, but your question is too blurry.

Categories