Like
#FXML
void start() {
// for game loop
while(!winning) {
if (attack.clicked()) attack();
else if (defend.clicked()) defend();
}
}
Can it possible to do this? Thanks.
You can assisgn true or false to a flag when when button click(at the button's click handler method). Your sintax may be different at click handler, but your code like this.
private boolean isAttackButtonClicked = false;
public void attackButtonclickHandler(ClickEvent e){
...
isAttackButtonClicked = true;
...
}
Related
In some cases a need my JButton to appear as if it were pressed. This depends on some boolean.
I tried to create my own ButtonModel that overrides the default isPressed() method but in that way the button appears pressed only in case the mouse pointer is on top of it (without pressing a mouse button). I need it to appear pressed also if the mouse is somewhere else.
So far I tried this:
class MyButtonModel extends DefaultButtonModel
{
private boolean appearPressed;
#Override
public boolean isPressed()
{
return super.isPressed() || appearPressed;
}
}
I cannot use a JToggleButton or something similar.
My button derives from another class that implements some additional features and derives itself from JButton.
UPDATE:
I'm running on Windows 10 and use the WindowsClassic Look&Feel.
you also need to override isArmed():
class MyButtonModel extends DefaultButtonModel
{
private boolean appearPressed = true;
#Override
public boolean isPressed()
{
return super.isPressed() || appearPressed;
}
#Override
public boolean isArmed() {
return super.isArmed() || appearPressed;
}
}
The partial reply was given by #Philipp Li and #camickr. Thank you.
My button model needs to override isArmed() in order to obtain the desired result.
However, once the button is pressed, it doesn't respond anymore.
Digging in the source of DefaultButtonModel, I found this:
public void setPressed(boolean b)
{
if ((isPressed() == b) || (!isEnabled()))
return;
...
}
This method is invoked by AbstractButton.doClick() in order to notify the various ActionListeners.
In other words, if the overridden method isPressed() returns true because I want to make the button appear pressed, a successive click on that button will be ignored.
A similar thing occurs within DefaultButtonModel.setArmed().
Therefore, I implemented my button model as follows:
class MyButtonModel extends DefaultButtonModel
{
boolean appearPressed = false;
private boolean withinSetPressedMethod = false;
private boolean withinSetArmedMethod = false;
#Override
public void setPressed(boolean pressed)
{
withinSetPressedMethod = true;
super.setPressed(pressed);
withinSetPressedMethod = false;
}
#Override
public void setArmed(boolean armed)
{
withinSetArmedMethod = true;
super.setArmed(armed);
withinSetArmedMethod = false;
}
#Override
public boolean isPressed()
{
if (withinSetPressedMethod)
return (super.isPressed());
else
return (super.isPressed() || appearPressed);
}
#Override
public boolean isArmed()
{
if (withinSetArmedMethod)
return (super.isArmed());
else
return (super.isArmed() || appearPressed);
}
} // class MyButtonModel
I am trying to make a game and I made a Button class using the JButton library.
This code is in the button class:
#Override public void mouseClicked(MouseEvent e) { pressed = true; }
public boolean getPressed(){
return pressed;
}
Then in the main java file:
Button playButton = new Button("PLAY", frame);
frame.add(playButton);
while (true){
if (playButton.getPressed())
System.out.println("test");
}
It never seems to print test even though I checked and the boolean value is being changed
I am unfamiliar with JButton. But it seems that in your if statement in your while loop, there is no condition. Please reply if this helps!
i had two classes, one is Main.java another is Button.java
So in Main.java, i need to confirm while Jbutton in Button.java is clicked, then Main.java will do something
in Button.java
public void actionPerformed(ActionEvent e) {
if(e.getSource()==Jsend)
{
String userinput = Jusertxt.getText();
setUserText(userinput);//setUserText is a method for Main.java can get a String from user input.
Jusertxt.setText("");
}
}
what is the statement i need to write in Main.java to check button is clicked in Button.java?
here is some code in Main.java
while(true){
if(//in here i need to ensure button is clicked){
} }
To receive notification button is pressed, you must register listener in Main.java. With it you can synchronize a local variable to store the status of the button (if it is clicked). This variable can use later to determine whether the button is pressed (once).
Edit Another option is the variable part of Button.java and to implement a public method isClicked()
Put a boolean value within the button, use a getter to retrieve the value of the boolean e.g
Example :
//Within main
Button button = new Button(); // I wouldn't recommend using button as a
class name by the way, will get very confusing
if(button.getPressed){
// do stuff
}
Your button code:
public void actionPerformed(ActionEvent e) {
if(e.getSource()==Jsend)
{
String userinput = Jusertxt.getText();
setUserText(userinput);//setUserText is a method for Main.java can get a String from user input.
Jusertxt.setText("");
isPressed = true;
}
}
public boolean getPressed(){
return isPressed;
}
I have some doubts about the use of the methods wait() and notify(). I have the next code which has some button events, the first time the user pushes the button it has to stop printing, and the second time it restarts printing again. I understand that is better to use Runnable instead of Thread, but I have to use Thread because of the requirements. The code works fine the first time the button is pushed but the second time it doesn´t, I want to use the wait() and the notify, but i don´t know how to do it with this particular code.
class Thr extends Thread{
private int count = 0;
private long pause;
private boolean canPrint = true;
private JTextArea textArea;
Thr(long miliseconds,JTextArea text){
pause = miliseconds;
textArea = text;
}
public void pushedButton(){
if(canPrint)
this.canPrint = false;
else
this.canPrint = true;
}
public void run()
{
while(this.canPrint)
{
try
{
this.printCounter();
Thread.sleep(pause);
this.count++;
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
public void printCounter(){
String time;
time = Integer.toString(count);
textArea.setText(time);
}
}
class Interface extends JFrame implements ActionListener{
private JTextArea textArea,textArea2;
private JButton button;
private Thr thread,threadEvent;
Interface()
{
textArea = new JTextArea(10,7);
textArea2 = new JTextArea(10,7);
thread = new Thr(2000,textArea);
threadEvent = new Thr(1000,textArea2);
button = new JButton("Pausar/Reanudar");
this.getContentPane().add(button,BorderLayout.SOUTH);
this.getContentPane().add(textArea,BorderLayout.WEST);
this.getContentPane().add(textArea2,BorderLayout.EAST);
thread.start();
threadEvent.start();
button.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
threadEvent.pushedButton();
}
}
public class MensajesHilos {
public static void main(String[] args){
Interface i = new Interface();
i.setTitle("Control Threads");
i.setBounds(200, 200, 300, 240);
i.setVisible(true);
}
}
The way you have coded, if you want to achieve the desired result,
I feel the modification need to be done in run method,
public void run()
{
while(true)
{
if(this.canPrint){
try
{
this.printCounter();
Thread.sleep(pause);
this.count++;
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
in this way, your Thread will never go dead and toggle printing based on canPrint boolean value.
Also, make sure to declare canPrint variable volatile, so that changes to it will be directly written to main memory and reflected immediately.
"button event doesn´t work properly"
This is false, if you put a print statement in the actionPerformed method, you will see that it is called every time you press the button.
By the way note that you can simplify this
if(canPrint)
this.canPrint = false;
else
this.canPrint = true;
To
this.canPrint = !this.canPrint;
Note that it is a good practice to always put #Override anotation on top of overriden method.
#Override
public void actionPerformed(ActionEvent event)
{
threadEvent.pushedButton();
}
Now why don't you get the expected result ?
You ommit to call thread.pushedButton, so the canPrint will only be reseted in the threadEvent object, and will never be in thread.
Note that once the boolean are set to false, you will exit the loop and the process won't start back after even if you re-set the boolean value to true. This example will works using while(true) however, you should change the true to any sentinel value to handle the exit of the program as this will loop forever.
#Override
public void run()
{
while(true)
{
if(this.canPrint)
{
this.printCounter();
this.count++;
}
try
{
Thread.sleep(pause);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Also, make sure that pause is never 0 else you will eat all of the computer process.
Note that, as other stated, you should declare variables that are accessed in thread as volatile (canPrint) in your case.
I have created a popup box that extends DialogBox and uses a cellTable that contains a list of values, one of which will be selected and inserted into a textBox.
-I have an onSelectionChange handler which is fired when one of the rows is clicked.
-I have an onDoubleClick handler which is fired when the same rows are double clicked.
both work when the other is commented out. But when they are both in the live code, whichever one is written first gets overwritten by the other one and no longer gets called.
Any way around this?
Code snipbit:
final SingleSelectionModel<popUpBoxContent> selectionModel= new <popUpBoxContent>();
cellTable.setSelectionModel(selectionModel);
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler(){
public void onSelectionChange (selectionChangeEvent event){
//Do something
}});
final SingleSelectionModel<popUpBoxContent> selectionModel2= new <popUpBoxContent>();
cellTable.setSelectionModel(selectionMode2);
cellTable.addDomHandler(new DoubleClickHandler(){
public void onDoubleClick(final DoubleClickEvent event){
//Do something else
}},
DoubleClickEvent.getType());
Thank you!
Yes they get overwritten from what I can see in the snippet. Assuming "popUpBoxContent" is the data type with which the CellTable (I presume cellTable is a CellTable) is being populated you could try this and see if it works:
final SingleSelectionModel<PopUpBoxContent> selectionModel = new SingleSelectionModel<PopUpBoxContent>();
cellTable.setSelectionModel(selectionModel);
cellTable.addDomHandler(new DoubleClickHandler() {
public void onDoubleClick(final DoubleClickEvent event) {
PopUpBoxContent selected = selectionModel.getSelectedObject();
if (selected != null) {
System.out.println("double clicked");
}
}
},
DoubleClickEvent.getType());
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
public void onSelectionChange(SelectionChangeEvent event) {
System.out.println("clicked");
}
});