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();
// ...
}
Related
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.
I apologize as I understand that the question is a bit broad in nature. What I want to achieve is having the ability to load different FXML files (located in different packages) when specific conditions are met, for example when a button is pressed or when a certain condition is satisfied. So far I've managed to load a file when a button is pressed.
#FXML
private AnchorPane rootPane;
#FXML
private Button btn;
#FXML
private void loadLoginWindow(javafx.event.ActionEvent event) throws IOException {
AnchorPane pane = FXMLLoader.load(getClass().getResource("login/MainWindow.fxml"));
rootPane.getChildren().setAll(pane);
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
And it works fine but for one button and one handler only, as soon as another is added it stops working, it seems that only one can work at the time. So clearly I'm missing a bunch of important info but every tutorial I've had a look at doesn't address this point because they don't even get there (They all revolve around a single file, or they rely on hiding and showing panes from the same file)
Does anyone have any good tutorial or comprehensive guide to do this kind of things?
Thanks in advance.
What can happen is the link to the controller within the FXML file can be incorrect. Even though the file location may not exist, the application will still build.
<AnchorPane id="AnchorPane" fx:controller="main.MainController">
The pathway in the fx:controller need to be correct.
Cheers!
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();
}
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.
I would like to use Window Builder and use the MVC paradigm simultaneously. It is very messy with a complex window when Window Builder adds all the code to just one file.
I would like the default file created to be the 'view'.
I would like to keep my control actions (event listeners) in a 'controller' class. Is there a way to have Window builder automatically put the event listeners in a class of your choice rather than adding to one monolithic file?
I don't know how Eclipse's Window Builder works, but I do know that NetBean's creates anonymous inner classes that call a custom method for each button and then allows the programmer to alter the body of the custom method. If Eclipse is similar, then you can simply have this custom method call a method of your Control object. Sure it adds a layer of indirection, but it's a small price to pay to give you complete control over your control.
For instance, if I create a JButton called "myButton" and then have the code generator create an action for my button, it will create this code:
myButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
myButtonActionPerformed(evt);
}
});
and will allow me to access and write code in the generated method, myButtonActionPerformed:
private void myButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
And inside of this method I would call my Control's method:
private void myButtonActionPerformed(java.awt.event.ActionEvent evt) {
if (myControl != null) {
myControl.myButtonAction();
}
}
The control class could look something like
class MyControl {
void myButtonAction() {
//TODO: implement control code
}
}
The GUI would need a setControl(MyControl myControl) method in order to "inject" the control into the GUI.
Another way of splitting up your code is to use the wizard to create JPanels as your own controls, then add those in a layout in the main window using the "Choose component" tool (looks like three beans) from the palette. Easy and much cleaner. Although I understand it still merges the "V" and "C" in MVC...