JavaFX TextField override key UP/DOWN behaviour - java

I'm designing a custom time control with a TextField and want to implement the function to increase the hours or minutes when pressing the UP-Key.
This is how i intended to do it:
textField.setOnKeyPressed(event -> {
int caretPos = editableNode.getCaretPosition();
switch (event.getCode()) {
case ESCAPE:
getSkinnable().reset();
event.consume();
break;
case UP:
if (!getSkinnable().isInvalid()){
if (caretPos < 3) {
getSkinnable().increaseHour();
selectHour();
} else {
getSkinnable().increaseMinute();
selectMinute();
}
}
event.consume();
break;
}
});
The problem here is that the UP-Key on default sets the caret to the beginning of the text.
I already found this post: JavaFX Textfield - Override default behaviour when up key is released
But the event that sets the caret at the beginning of the text comes before my eventhandler so this did not help me.
Does anyone know how I can "override" this behaviour?

The solution of #James_D resolved my issue.
textField.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
int caretPos = editableNode.getCaretPosition();
switch (event.getCode()) {
case ESCAPE:
getSkinnable().reset();
event.consume();
break;
case UP:
if (!getSkinnable().isInvalid()){
if (caretPos < 3) {
getSkinnable().increaseHour();
selectHour();
} else {
getSkinnable().increaseMinute();
selectMinute();
}
}
event.consume();
break;
}
});

Related

Java predator-prey simulation with GUI can't run simulation properly

I'm trying to add a GUI to the predator-prey simulation. It can allow users to choose which simulation(species involved) they want to do, set the simulation field size they want, and show the progress of the simulation and result.
I asked a question before, and I kinda figure it out what caused the problem.
But now I encounter another question:
How do I let the simulation buttons use the same object method which generate button use, but also don't generate the field again?
As the code shown below,
(1)after I generate the field and press "Reset" button, it will generate the field again then reset simulation(show up a new field window with different data);
(2)after I generate the field and press "Next step" button, it will generate the field again then show next step simulation(show up a new field window and don't inherit the initial data I generate);
(3)after I generate the field and press "long run step" button, it will generate the field again then show next hundred step simulation result(show up a new field window and don't inherit the initial data I generate and don't show the progress);
private class ButtonHandler implements ActionListener{//Generate field
public void actionPerformed(ActionEvent e){
String depthstr = depthtxt.getText();
String widthstr = widthtxt.getText();
int depth = Integer.parseInt(depthstr);
int width = Integer.parseInt(widthstr);
switch(version) {//Different specices simulation choosed by user
case 0:
Simulator set = new Simulator();
set.Simulator(depth,width);
break;
case 1:
SimulatorRF setRF = new SimulatorRF();
setRF.SimulatorRF(depth,width);
break;
case 2:
SimulatorRW setRW = new SimulatorRW();
setRW.SimulatorRW(depth,width);
break;
...
case 25:
SimulatorFWBH setFWBH = new SimulatorFWBH();
setFWBH.SimulatorFWBH(depth,width);
break;
}
reset_butt.setEnabled(true);
nextstep_butt.setEnabled(true);
longstep_butt.setEnabled(true);
}
}
private class SimulateOption implements ActionListener{
public void actionPerformed(ActionEvent e){
switch(version) {
case 0:
Simulator set = new Simulator();
set.Simulator(depth,width);
if(e.getSource() == reset_butt){
set.reset();
}
else if(e.getSource() == nextstep_butt){
set.simulateOneStep();
}
else if(e.getSource() == longstep_butt){
set.runLongSimulation();
}
break;
case 1:
SimulatorRF setRF = new SimulatorRF();
setRF.SimulatorRF(depth,width);
if(e.getSource() == reset_butt){
setRF.reset();
}
else if(e.getSource() == nextstep_butt){
setRF.simulateOneStep();
}
else if(e.getSource() == longstep_butt){
setRF.runLongSimulation();
}
break;
case 2:
SimulatorRW setRW = new SimulatorRW();
setRW.SimulatorRW(depth,width);
if(e.getSource() == reset_butt){
setRW.reset();
}
else if(e.getSource() == nextstep_butt){
setRW.simulateOneStep();
}
else if(e.getSource() == longstep_butt){
setRW.runLongSimulation();
}
break;
...
case 25:
SimulatorFWBH setFWBH = new SimulatorFWBH();
setFWBH.SimulatorFWBH(depth,width);
if(e.getSource() == reset_butt){
setFWBH.reset();
}
else if(e.getSource() == nextstep_butt){
setFWBH.simulateOneStep();
}
else if(e.getSource() == longstep_butt){
setFWBH.runLongSimulation();
}
break;
}
reset_butt.setEnabled(true);
nextstep_butt.setEnabled(true);
longstep_butt.setEnabled(true);
}
}
}
Is there a way to fix my code to solve the problem?
I hope that
(1)after I click the "reset" button, it reset the field I generated (don't show up a new field window)
(2)after I click the "next step" button, it simulate next step of the field I generated (inherit the initial data I generate and don't show up a new field window)
(3)after I click the "next hundred step" button, it simulate next hundred step of the field I generated and show the process (inherit the initial data I generate and don't show up a new field window)
Here are my full codes if you are intrested:
https://github.com/KasmaJC/predator-prey-simulation-with-GUI
*There is a BlueJ project rar. at button
Well I figure it out myself again...
I put all of my Classes into a package(name as PredatorPreySimulation), and change all simulator classes' method into static method.
By doing so, I don't need to create an instance of the class to call a static method, and I can let the simulation buttons use the same method which generate button use, but also don't generate the field again.
In this case, I can use the code below:
import PredatorPreySimulation.Simulation;
import PredatorPreySimulation.SimulationRF;
...
and
public class ButtonHandler implements ActionListener{//Generate field
public void actionPerformed(ActionEvent e){
String depthstr = depthtxt.getText();
String widthstr = widthtxt.getText();
int depth = Integer.parseInt(depthstr);
int width = Integer.parseInt(widthstr);
switch(version) {//Different specices simulation choosed by user
case 0:
Simulator.Simulator(depth,width);
break;
case 1:
SimulatorRF.SimulatorRF(depth,width);
break;
...
case 25:
SimulatorFWBH.SimulatorFWBH(depth,width);
break;
}
reset_butt.setEnabled(true);
nextstep_butt.setEnabled(true);
longstep_butt.setEnabled(true);
}
}
private class SimulateOption implements ActionListener{
public void actionPerformed(ActionEvent e){
switch(version) {
case 0:
if(e.getSource() == reset_butt){
Simulator.reset();
}
else if(e.getSource() == nextstep_butt){
Simulator.simulateOneStep();
}
else if(e.getSource() == longstep_butt){
Simulator.runLongSimulation();
}
break;
case 1:
if(e.getSource() == reset_butt){
SimulatorRF.reset();
}
else if(e.getSource() == nextstep_butt){
SimulatorRF.simulateOneStep();
}
else if(e.getSource() == longstep_butt){
SimulatorRF.runLongSimulation();
}
break;
...
case 25:
if(e.getSource() == reset_butt){
SimulatorFWBH.reset();
}
else if(e.getSource() == nextstep_butt){
SimulatorFWBH.simulateOneStep();
}
else if(e.getSource() == longstep_butt){
SimulatorFWBH.runLongSimulation();
}
break;
}
reset_butt.setEnabled(true);
nextstep_butt.setEnabled(true);
longstep_butt.setEnabled(true);
}
}
}
I might be bad at expressing questions... I'll try to improve my asking skill ...

Show previous text even after click event on Android

Here is my image button Click action below.
It will show "1 null" after clicked, while holding the button it shows rightAnswer. I want to illustrate the saved string of questionLabel after finishing the holding button.
What I want to do is that define "sharedFact" before Action_down happens and keep that text to show after the user button untouched.
The "rightAnswer" should be only shown while the user holding imageButton
I tried to use normal String text like "test", it works, however String sharedFact = questionLabel.getText().toString();
↑ is not working like what I thought, what is the problem and how to fix?
findViewById(R.id.image_button_check).setOnTouchListener((v, event) -> {
String sharedFact = null;
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
sharedFact = questionLabel.getText().toString();
questionLabel.setText(rightAnswer);
break;
case MotionEvent.ACTION_UP:
//Here is only test "1" is for identify this code is working
//How can I save questionLabel Text before action down but inside click action?
questionLabel.setText(1 + sharedFact);
break;
}
return false;
});
move your definition from inside the function to a class data member.
String sharedFact = null;
onCreate(){
// your code
findViewById(R.id.image_button_check).setOnTouchListener((v, event) -> {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
sharedFact = questionLabel.getText().toString();
questionLabel.setText(rightAnswer);
break;
case MotionEvent.ACTION_UP:
//Here is only test "1" is for identify this code is working
//How can I save questionLabel Text before action down but inside click action?
questionLabel.setText(1 + sharedFact);
sharedFact = null
break;
}
return false;
});
}

Button needs to be clicked twice. Any ideas why?

Basically what I'm trying to do is setting the color of the buttons. The "ganzjahrbutton" includes the other buttons. Therefore I wanna change the color of all the buttons (besides the ganzjahrbutton) to the grey color (162,162,162) if it's clicked.
If any of the other buttons is clicked, they should turn green and the ganzjahrbutton should become grey again. It kinda works, but the buttons need to be pressed twice.
Does anybody have an idea why?
switch (v.getId()){
case R.id.ganzjahrbutton:
ganzjahrbtnstate = !ganzjahrbtnstate;
if (ganzjahrbtnstate==true){
ganzjahrbutton.setBackgroundColor(Color.rgb(30,168,1));
fruhlungbutton.setBackgroundColor(Color.rgb(162,162,162));
sommerbutton.setBackgroundColor(Color.rgb(162,162,162));
herbstbutton.setBackgroundColor(Color.rgb(162,162,162));
winterbutton.setBackgroundColor(Color.rgb(162,162,162));
}
else {ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162));}
break;
case R.id.fruhlingbutton:
fruhlingbtnstate = !fruhlingbtnstate;
if (fruhlingbtnstate==true){fruhlungbutton.setBackgroundColor(Color.rgb(30,168,1));
ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162)); }
else {fruhlungbutton.setBackgroundColor(Color.rgb(162,162,162));}
break;
case R.id.sommerbutton:
sommerbtnstate = !sommerbtnstate;
if (sommerbtnstate==true){sommerbutton.setBackgroundColor(Color.rgb(30,168,1));
ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162)); }
else {sommerbutton.setBackgroundColor(Color.rgb(162,162,162));}
break;
case R.id.herbstbutton:
herbstbtnstate = !herbstbtnstate;
if (herbstbtnstate==true){herbstbutton.setBackgroundColor(Color.rgb(30,168,1));
ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162)); }
else {herbstbutton.setBackgroundColor(Color.rgb(162,162,161));}
break;
case R.id.winterbutton:
winterbtnstate = !winterbtnstate;
if (winterbtnstate==true){winterbutton.setBackgroundColor(Color.rgb(30,168,1));
ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162)); }
else {winterbutton.setBackgroundColor(Color.rgb(162,162,162));}
break;
Move the lines where you change the boolean states to the end of cases just before break statements
case R.id.ganzjahrbutton:
****FROM HERE*****ganzjahrbtnstate = !ganzjahrbtnstate;****
if (ganzjahrbtnstate==true){
ganzjahrbutton.setBackgroundColor(Color.rgb(30,168,1));
fruhlungbutton.setBackgroundColor(Color.rgb(162,162,162));
sommerbutton.setBackgroundColor(Color.rgb(162,162,162));
herbstbutton.setBackgroundColor(Color.rgb(162,162,162));
winterbutton.setBackgroundColor(Color.rgb(162,162,162));
}
else {ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162));}
TO HERE*****ganzjahrbtnstate = !ganzjahrbtnstate;****
break;

Using a switch statement for a JComboBox

I have a JComboBox set up as shown below:
private String[] boxChoices = {"option 1", "option 2"};
JcomboBox box = new JCombobox(boxChoices);
box.addItemListener()
{
public void itemStateChanged(ItemEvent event)
{
int selection = box.getSelectedIndex();
switch (selection)
{
case 0: JOptionPane.showMessageDialog(null, "you have selected option 1");
break;
case 1: JOptionPane.showMessageDialog(null, "you have selected option 2");
break;
default: break;
}
}
}
My issue is that when I pick an option the message will be shown twice instead of once. For example if I choose Option 1 the following would appear:
you have selected option 1
you have selected option 1
What is causing this to happen?
In addition to #Blip's answer, you can also use actionListener. An actionEvent for JComboBox is only triggered once when you change a selection.
box.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
int selection = box.getSelectedIndex();
switch (selection) {
case 0:
JOptionPane.showMessageDialog(null, "you have selected option 1");
break;
case 1:
JOptionPane.showMessageDialog(null, "you have selected option 2");
break;
default:
break;
}
}
});
This behaviour occurs because Item listener is called 2 times because of selection of any item in the JComboBox. The first is called for deselection of previously selected item and the second time it is called for selection of the new item.
You can filter this by using a if clause to reflect the actual event you want to catch i.e. Selection or deselection :
if(event.getStateChange() == ItemEvent.SELECTED)
OR
if(event.getStateChange() == ItemEvent.DESELECTED)
based on your preference of choice of state change you want to trap.
I'm not sure but it could be because you're using an anonnymous listener.
Implement the ItemListener and add it.

Converting Java code to work on android troubles? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i'm having trouble making some java code i have work with android studio, the main problems i'm having is turning keyboard inputs into swipes and presses from the user, this is my current code for keyboard inputs:
class TAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
if (!isStarted || curPiece.getShape() == foodShapes.NoShape) {
return;
}
int keycode = e.getKeyCode();
if (keycode == 'p' || keycode == 'P') {
pause();
return;
}
if (isPaused)
return;
switch (keycode) {
case KeyEvent.VK_LEFT:
tryMove(curPiece, curX - 1, curY);
break;
case KeyEvent.VK_RIGHT:
tryMove(curPiece, curX + 1, curY);
break;
case KeyEvent.VK_DOWN:
tryMove(curPiece.rotateRight(), curX, curY);
break;
case KeyEvent.VK_UP:
tryMove(curPiece.rotateLeft(), curX, curY);
break;
case KeyEvent.VK_SPACE:
dropDown();
break;
case 'd':
oneLineDown();
break;
case 'D':
oneLineDown();
break;
}
any help would be appreciated, thankyou.
This page is available within the android documentation and tutorials....
public class MainActivity extends Activity {
...
// This example shows an Activity, but you would use the same approach if
// you were subclassing a View.
#Override
public boolean onTouchEvent(MotionEvent event){
int action = MotionEventCompat.getActionMasked(event);
switch(action) {
case (MotionEvent.ACTION_DOWN) :
Log.d(DEBUG_TAG,"Action was DOWN");
return true;
case (MotionEvent.ACTION_MOVE) :
Log.d(DEBUG_TAG,"Action was MOVE");
return true;
case (MotionEvent.ACTION_UP) :
Log.d(DEBUG_TAG,"Action was UP");
return true;
case (MotionEvent.ACTION_CANCEL) :
Log.d(DEBUG_TAG,"Action was CANCEL");
return true;
case (MotionEvent.ACTION_OUTSIDE) :
Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
"of current screen element");
return true;
default :
return super.onTouchEvent(event);
}
}
I would suggest, however, that you use Google in the future, before seeking assistance on stack overflow. The attached link was located via Google on the first page of results for "android - detect input gestures within your app". In addition, I believe that I have come across a question that was directly related to this and it was answered in full.
Please remember; Stack Overflow is not here so that you can be lazy - its here so that you can seek assistance when it is actually required..

Categories

Resources