JButton Value If Else Conditon with JSpinner doesn't work properly - java

I'm trying to make a timer that has a jbutton called Short Break and a jbutton called Customize.
By clicking the default Short Break button the time will be set to 5.00 minutes.
But the user can customize the time with the help of JSpinner by clicking on the customize button.
But if the user wants to reduce the short break time by clicking on the customize button, then clicking on the short break button after customizing will show the customized time.
But after customizing, the customized time is shown, but clicking on the short break button without customizing does not show the default value. Means that the if condition only work but the else condition doesn't.
My JSpinner Code:
spinnerShortBreak.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
value2 = Integer.parseInt(String.valueOf(spinnerShortBreak.getValue()));
}
});
The Short Break button Code:
btnShortBreak.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
timer.stop();
second = 00;
minute = 05;
if (spinnerShortBreak !=null && spinnerShortBreak.getModel().getValue().equals(value2)) {
lblMinute.setText(String.valueOf(value2));
}
else if(spinnerShortBreak == null && spinnerShortBreak.getValue() == null) {
btnStartStop.setText("Start");
lblMinute.setText("05");
lblSecond.setText("00");
}
}
});

I find a solution. If i check the value is equal to 0 or greater than 0 the code will work perfectly.
here the button code:
btnShortBreak.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
timer.stop();
if(value2 > 0){
second = 00;
lblMinute.setText("0"+String.valueOf(value2));
btnStartStop.setText("Start");
lblSecond.setText("00");
}
else if(value2 == 0) {
second = 00;
minute = 05;
btnStartStop.setText("Start");
lblMinute.setText("05");
lblSecond.setText("00");
}
}
});

Related

Make code continue from same place in action performed

//boutton
ent.setBounds(490,400,100,50);
ent.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String text;
text=chatbox.getText().toLowerCase();
chatarea.append("\t\t\t\t"+text+" <- YOU \n");
chatbox.setText("");
if(text.contains("hii") || text.contains("hey") || text.contains("hi"))
{
bot("hey there");
bot("how can i help you?");
}
else if(text.contains("info") || text.contains("help") || text.contains("i need information") || text.contains("help me"))
{
bot("sure i am here to help you");
bot("what you want to know about?");
bot("1 info about anything?");
bot("2 info about place?");
bot("enter your choice : ");
if(text.contains("1")|| text.contains("info about anything") || text.contains("number 1") || text.contains("first one") || text.contains("first"))
{
bot("sure which blood group info you are looking for?");
}else if(text.contains("2") || text.contains("info about place") || text.contains("number 2") || text.contains("second one") || text.contains("second"))
else
{
bot("I Don't Understand you");
}
}
}
});
}
private void bot(String string)
{
chatarea.append("BOT -> "+string+"\n");
}
public static void main(String[] args)
{
new bot();
}
}
I am making a chat bot in which when I click on the JButton it takes the value from JTextField and posts it on a JTextArea. But the problem is I want my code to be continue not stop but every time click on button it start the code from starting.
How to make code continue from same place in action performed when I second time click on the button?
Add a boolean member to your botclass to keep track of the state (first time / not first time) and initialise it to true
boolean isFirstTimeButtonWasPressed = true
public void actionPerformed(ActionEvent e) {
if (isFirstTimeButtonWasPressed) {
//do stuff that should only happens the firs time
//...
isFirstTimeButtonWasPressed = false;
}
//do stuff that should be done every time the button is pressed
});

Timer doesn't work when another button is pressed (applet)

I'm making an applet which has two buttons: step back and step forward.
I have the same timer in every button to execute an animation. If I push step forward it works fine, and the animation runs, but if i push step back, the animation doesn't run or is bad runned (in a wrong position and velocity).
I guess the problem is the timer doesn't stop correctly and is running when start the timer again, but I donĀ“t know how to solve it.
This is the code of the step forward button:
//Code of the button "Paso a Paso"
this.botonPasoAPaso = new JButton("Paso a paso");
this.botonPasoAPaso.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//Avoid timer to accelerate
if (timer != null && timer.isRunning()) {
timer.stop();
}
//Code of the timer, makes an animation
timer = new Timer(35, new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (pasoAPaso <= listaPaquetes.size()) {
Paquete p = listaPaquetes.get(pasoAPaso);
p.animar();
panelGrafo.removeAll();
panelGrafo.updateUI();
panelGrafo.setPaquete(p);
panelGrafo.setAnimar(true);
panelGrafo.repaint();
}
}
});
timer.start();
pasoAPaso++;
}
});
This is the code of the step back button:
this.botonAtras.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (timer != null && timer.isRunning()) {
timer.stop();
}
timer = new Timer(35, new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (pasoAPaso < listaPaquetes.size() && pasoAPaso>=0) {
Paquete p = listaPaquetes.get(pasoAPaso);
p.animar();
panelGrafo.removeAll();
panelGrafo.updateUI();
panelGrafo.setPaquete(p);
panelGrafo.setPasoAPaso(pasoAPaso);
panelGrafo.setAnimar(true);
panelGrafo.repaint();
//Actualizar matriz del render para pintar celdas
RenderTabla.matrizTotal = new Cuadrado[pasos.get(pasoAPaso).length][pasos.get(pasoAPaso)[0].length];
RenderTabla.matrizTotal = pasos.get(pasoAPaso);
tabla.introducirDatos(pasos.get(pasoAPaso),false);
}
}
});
timer.start();
pasoAPaso--;
}
});
This is a video of the applet to see what's happening
If you see the video, when I click the "Paso a Paso" (Step forward) button, a truck is animated, but when I click the "rewind" (Step back) button, it doesn't work (appears a truck but is not correctly animated), and if i click again nothing happens.
Thanks.

How to implement timer to make text visible for a certain time and then replace it with another text in Java?

I am creating a mobile phone keypad. I used an array to create buttons.
For one of my buttons, I want "Dialling..." to appear on the JTextField for about 2 seconds on the click and then replace it with "No Signal" for about 2 more seconds and then clear the JTextField.
All this should be done by just 1 click. I know it is to do with timers but I can't seem to implement it.
I have something like this at the moment but it doesn't seem to be working.
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand() == "Dial")
{
timer.start();
counter++;
if(counter ==1)
{
text1.setText("Dialling . . .");
counter = 0;
}
if(e.getsource() == timer)
{
text1.setText("No Signal");
timer.stop();
}
}
Try this
public void actionPerformed(ActionEvent e) {
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand() == "Dial")
{
text1.setText("Dialling . . .");
}
}
};
Timer timer = new Timer(1000, taskPerformer); // delay one sec
timer.setRepeats(false);
timer.start();
text1.setText("No Signal");
}

Showing the same form many times

I am trying to show the form many times here in this example 10 times, Can someone help me in doing it?
In the below example i am showing only button to keep it simple, along with the button i will add other components like textbox etc..., In the below example, i am getting the error- times should be made final. If i make it final then, i wont be able to write times = times - 1.
private void showForm(int times){
if(times >= 1){
JButton btn = new JButton("ADD");
container.add(btn);
times = times - 1;
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
showForm(times);
}
});
}
}
Just write it like this:
private void showForm(final int times){
if(times >= 1){
JButton btn = new JButton("ADD");
container.add(btn);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
showForm(times - 1);
}
});
}
}
If by pressing the button you want new buttons to appear, minus 1 every time (this is what I understand), to actually make it work you also need to add revalidate() and a loop
private void showForm(final int times) {
if (times >= 1) {
for (int i=0; i<times; i++) {
JButton btn = new JButton("ADD");
container.add(btn);
container.revalidate();
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
showForm(times-1);
}
});
}
}
}
else forget the loop but keep revalidate (or you won't see any visible changes)

Java: Double-Click a JSlider to Reset

I have a JSlider that sets the speed of my metronome, from 40 - 200, where 120 is the default, in the middle.
When the user clicks the metronome button, the metronome plays at the speed displayed on the JSlider - the user drags the slider to the right, the speed of the metronome increases, and it decreases if they slide it to the left.
How do I add functionality so that if the user double-clicks on the JSlider button, it defaults back to 120 - in the middle?
Here is my code:
public Metronome() {
tempoChooser = new JSlider();
metronomeButton = new JToggleButton();
JLabel metText = new JLabel("Metronome:");
add(metText);
...
tempoChooser.setMaximum(200);
tempoChooser.setMinimum(40);
tempoChooser.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
tempoChooserStateChanged(evt);
}
});
add(tempoChooser);
...
}
private void tempoChooserStateChanged(javax.swing.event.ChangeEvent evt) {
final int tempo = tempoChooser.getValue();
if (((JSlider) evt.getSource()).getValueIsAdjusting()) {
setMetronomeButtonText(tempo);
} else {
processTempoChange(tempo);
}
}
thanks in advance!
This should help you out: http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
You need to read up on that and implement MouseListener. You can use int getClickCount() to count how many times the user has clicked, which will help you read double clicks.
Hope this helps!
Even though I dont see a question, my gues is you are looking for MouseListener.
Not simple job, you have to add javax.swing.Timer and listening if during fixed period Mouse cliked once or twice times, for example
I recently wrote something similar so I could differentiate between single and double left mouse-button clicks:
private Timer timer;
#Override
public void mouseClicked(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1){
if (timer == null) {
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() { // timer expired before another click received, therefore = single click
this.cancel();
timer = null;
/* single-click actions in here */
}
}, (Integer) Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval"));
}
else { // received another click before previous click (timer) expired, therefore = double click
timer.cancel();
timer = null;
/* double-click actions in here */
}
}
}

Categories