Not to sure if my title question is correct.
What I'm trying to achieve is to have one button create a Vehicle object. Then have a different button call the method embark (which will just update some fields).
So in general:
One button to create an instance of the object Vehicle.
A second button to call a method on this instance.
btnCar.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent event){
Vehicle C = new Car(amountPass, "hej", "hej");
}
});
btnEmbark.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent event){
ferry.embark(C);
}
});
Thanks!
There are several ways to that, the first that comes to my mind is with JavaFX properties:
ObjectProperty<Object> object = new SimpleObjectProperty<>();
Button button1 = new Button("create");
button1.setOnAction(ev -> object.set(new Object()));
Button button2 = new Button("magic");
button2.setOnAction(ev -> object.get().hashCode());
button2.disableProperty().bind(Bindings.isNull(object));
With Bindings we ensure that the second button can only be fired when the custom object has already been created and stored.
Another way would be to write a subclass of Button doing the communication with another Button. That is a matter of personal taste.
Related
Im having some issues trying to wait for an input after clicking a button.
With my team, we are making a card game, in which cards attack one another, the problem is that i don't know how to, after a button is clicked, make the event handler wait for the user to click another button.
The code looks like this:
private Button attackingButton(){
Button b1 = new Button();
b1.setOnAction(new EventHandler<ActionEvent>()
{
public void handle(ActionEvent event){
//Here i want the user to press another button and, depending which one he
//pressed, asing a variable
Card aCard = //The card that the button pressed has inside
}
}
That's just it, you don't make the handler wait. Instead you change the behavior of the handler depending on the state of the object. If the object is in the "user has not pressed the first button yet" state, the handler does one thing. If the object is in the "user has previously pressed the first button", then the handler does something else. Your handler should query the state of the object's instance fields to determine this state.
e.g.,
b1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event){
// may need to use boolean fields or .equals(...) method......
if (someStateField == someValue) {
doBehavior1();
} else {
doBehavior2();
}
}
}
So i have a button in my program (made with java.awt.Button) and I want to execute some code when i press the button. But i don't want to do this with an ActionListener. Is there a way to avoid the ActionListener?
You could override the processActionEvent(ActionEvent e) method, to execute the code.
Button btn = new Button("No Listener Button") {
void processActionEvent(ActionEvent e) {
// "some code" here
}
};
Note that doing this will create a new anonymous class for every button you create this way, so it is not really a Good Idea™. But it will do what you are asking; buyer beware.
I'm currently made an Form with JavaFX.
Always i press a Button, i call the "addAnswer()"-Method.
In that I create a RadioButton, a Label and a delete-Button, which i bundle in a HBox. All that HBoxes i pack in a vBox.
The Problem now is the delete-Button. I want to delte just THAT HBox in which the clicked Button is.
Here is my code:
public void addAnswer() {
this.rB = new RadioButton();
checkAnswer.getToggles().add(rB);
hBox = new HBox();
tF = new TextField();
delAnswer = new Button("Löschen");
delAnswer.setId(Integer.toString(counter));
hBox.getChildren().addAll(rB, tF, delAnswer);
hBox.setId(Integer.toString(counter));
delAnswer.setOnAction(e -> delAnswer(Integer.parseInt(hBox.getId())));
System.out.println(delAnswer.getId());
vBox.getChildren().addAll(hBox);
counter++;
}
public void delAnswer(int e){
vBox.getChildren().remove(delAnswer.getId());
}
i tried this one above but i realized, that all the delAnswers-Buttons have the same ID: the number of how often i pressed the add-Button.
Is there any solution where i can just select that one i pressed with that dynamic way? Cause i don't kow how often somebody will press or delete something.
Thanks
hbox is a field and this is why always the HBox last added is used. (hBox is evaluated, when lambda body is executed, not at the time of the lambda creation). This would be different, if you used a (effectively) final local variable:
final HBox hBoxLocal = hBox;
delAnswer.setOnAction(e -> delAnswer(Integer.parseInt(hBoxLocal.getId())));
However I'd like to present a different solution which would allow you to use the same EventHandler<ActionEvent> for all delete Buttons:
You can get the Node that triggered the event using getSource. From this Node you can get the parent, which is the HBox. You can remove this from the VBox using the remove(Object) method
delAnswer.setOnAction(e -> {
// get button
Node source = (Node) e.getSource();
// remove parent of button from VBox
vBox.getChildren().remove(source.getParent());
});
I think your problem is that you give the same event to all your button,Begin by creating a list that stores your buttons and then increments the value of the ID after affecting it to an item :
List<Button> buttons = new ArrayList<>();
/*
Create Button and call IDEvt method to create new event
for each button
*/
private void IDEvt(Button btn){
btn.setId(String.valueOf(IDRank));
btn.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
System.out.println(btn.getId());
}
});
IDRank++;
}
in the program that I am writing at the moment, I have 2 JFrames (each in a different class, each has a different purpose, however you could consider the widget frame to be a slave of some sort), one is a main window, and the other is a 'widget' that pops up upon hitting a button in the main window.
I only want one copy of the widget open at one time. I am currently doing this through boolean variables under an actionPerformed action listener. Below is the action listener for the main window.
public void actionPerformed(ActionEvent e) {
if(getOpenWidget() == false){
System.out.println(getOpenWidget()); //test line
widget.initialize(); // please note that the instance "widget" is declared just after "public class MainWindow{" :)
widget.frame.setVisible(true);
setOpenWidget(true);
System.out.println(getOpenWidget() ); // test line
}else{
System.out.println(getOpenWidget());
JOptionPane.showMessageDialog(frame, "There is already an instance of the Booking Widget open.");
}
}
Now the booking widget is open, on the booking widget there is a cancel button. Below here is the action listener for the widget's 'cancel' button.
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
MainWindow ui = new MainWindow();
frame.dispose();
ui.setOpenWidget(false);
}
}
Now, upon hitting my button in the main window again, in theory, the openWidget bool should be false, and allow me to open another window, however in the cancel button action listener, my variable isnt changed. So, am I going about my problem in the right way without making openWidget a static variable?(I should be using getters and setters right?)
What am I doing wrong and what don't I understand about instantiating a new instance of the main window every time I click that button?
Also, my getters and setters are stock standard as follows.
void setOpenWidget(boolean val){
this.openWidget = val;
}
boolean getOpenWidget(){
return this.openWidget;
}
Just pass the reference of MainWindow to the Widget class so that it can update its flag on cancel button.
You are calling setOpenWidget(false) on some other new instance you have created using this line MainWindow ui = new MainWindow();
You should call setOpenWidget(false) using same instance from which you have initialised widget. Pass the reference of MainWindow to widget while creating widget and invoke setOpenWidget(false) using that reference
When you are creating the object of widget within MainWindow you can call it like this:
widget = new Widget(this);
And change the Widget Window Constructor as follows:
MainWindow ui;
public Widget(MainWindow mw)
{
this.ui = mw;
//...initialize btnCancel...
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
ui.setOpenWidget(false);
}
});
//..do all other stuffs here...
}
Hi I use GWT and I have a com.smartgwt.client.widgets.Button that has the following eventHandler:
Button viewCommentsButton = new Button("View ");
viewCommentsButton.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
if (!childrenVisible) {
addChildren();
getParent().setTitle("Close");
} else {
removeChildren();
getParent().setTitle("View");
}
}
});
As you can see I tried getParent().setTitle() method but with no effect. The if works fine so I guess I can't get the reference to my button object but the code compiles and getParent returns a widget so most likely my button.
However, the addChildren and removeChildren methods are working properly but my button has the initial title all the time. Any ideas why? Hope this makes sense.
Any suggestions are welcomed. Thanks.
If you are trying to set the title on viewCommentsButton, call viewCommentsButton.setTitle().
If you are trying to set the text in the button, call viewCommentsButton.setText().
For either of these you'll have to mark the button as final - declare it with final Button viewCommentsButton = ...
The context of getParent() is confusing. getParent(), the way you're using it, will return the parent of the widget in which you're defining all of this, NOT the parent of viewCommentsButton and definitely not viewCommentsButton itself.
Make your button a class variable, rather than a method variable and than you would be able to use it (refer it) inside the click handler.
For example:
viewCommentsButton = new Button("View "); //viewCommentButton is the private member.
viewCommentsButton.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
if (!childrenVisible) {
addChildren();
viewCommentButton.setTitle("Close");
viewCommentButton.setText("Close");
} else {
removeChildren();
viewCommentButton.setTitle("View");
viewCommentButton.setText("View");
}
}
});
You should use setText
setTitle is the "tooltip"