Reputation: 37
I am currently having two problems both at which i have been trying to solve for the past 3 hours.
I cant get the input--;
to decrement if input is not == to 0
I cant get the JTextField input
to update the the value i assign to it once the program is running. Ill type 22 in the running program click start and it will go to "test99". Pictures is an example of how i entered the value 66 then i pressed start and test99 came up instead of test66
I hope I was able to explain my problem to an extent you will be able to understand. I have read a lot of documentation about action listeners but currently the idea wont click for me. Any constructive criticism is welcome.
I also simplified my problem down as best as I could.
package test;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import javax.swing.JButton;
public class test {
private JFrame frame;
private JButton btnStart;
/**
*
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test window = new test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
JLabel Output = new JLabel("Time left: ");
Output.setHorizontalAlignment(SwingConstants.CENTER);
frame.getContentPane().add(Output, BorderLayout.CENTER);
JTextField Input = new JTextField();
btnStart = new JButton("start");
Input.setText("99");
int input = (int) (Double.parseDouble(Input.getText()));
Input.setHorizontalAlignment(SwingConstants.CENTER);
frame.getContentPane().add(Input, BorderLayout.SOUTH);
Input.setColumns(10);
frame.getContentPane().add(btnStart, BorderLayout.NORTH);
frame.setBounds(100, 100, 300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Input.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
Timer t = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Output.setText("test" + input);
if (input == 0) {
((Timer) e.getSource()).stop();
}
input--;
}
});
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t.start();
}
});
}
}
Upvotes: 1
Views: 578
Reputation: 2759
There are several criticisms I have for your code:
int input = (int) (Double.parseDouble(Input.getText()));
Why use Double.parseDouble
and then cast to an int
when you can just use Integer.parseInt()
?JTextField
to 99, then parse that value once and then use it in your timer. You never update this value so it is always 99. Instead of this you should parse the text in the input JTextField
every time the user presses "Start" and update the int
value that your timer uses.See my corrected solution below:
import javax.swing.*;
import java.awt.*;
import java.util.Timer;
import java.util.TimerTask;
public class Test {
private Test() {
initialize();
}
public static void main(String[] args) {
EventQueue.invokeLater(Test::new);
}
private void initialize() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
JPanel timePanel = new JPanel(new FlowLayout());
timePanel.add(new JLabel("Time left: "));
JLabel timeOutput = new JLabel("");
timePanel.add(timeOutput);
frame.add(timePanel, BorderLayout.CENTER);
JPanel inputPanel = new JPanel(new FlowLayout());
JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");
stopButton.setEnabled(false);
JTextField timeField = new JTextField(5);
inputPanel.add(startButton);
inputPanel.add(stopButton);
inputPanel.add(timeField);
frame.add(inputPanel, BorderLayout.SOUTH);
Timer timer = new Timer();
startButton.addActionListener(e -> {
startButton.setEnabled(false);
stopButton.setEnabled(true);
timer.scheduleAtFixedRate(new TimerTask() {
int time = Integer.parseInt(timeField.getText());
@Override
public void run() {
if(time < 0) timer.cancel();
timeOutput.setText(String.valueOf(time--));
}
}, 0, 1000);
});
stopButton.addActionListener(e -> {
timer.cancel();
stopButton.setEnabled(false);
startButton.setEnabled(true);
timeOutput.setText("");
});
frame.pack();
frame.setVisible(true);
}
}
Upvotes: 0
Reputation: 938
I recommand to declare your input variables not in your function, but in your class. Otherwise you run into scope problems. Example:
public class test {
private JFrame frame;
private JButton btnStart;
private int input;
private JTextField Input;
//...
}
Should fix the issue :)
I am not enterily sure about the second issue but if you want to count down from the entered value, you have to update your action listener:
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
input = (int) (Double.parseDouble(Input.getText()));
t.start();
}
});
Upvotes: 1