In gui there are two fields which add two numbers on button click and there is code logic to check if the fields are empty but what i want is when user click on the button it should display a progress bar according to milliseconds progress should go from 1 to 100 then it complete the task and auto reset the progress bar.
public class Main {
public static void main(String[] args) {
Progress pg = new Progress();
}
}
public class Progress extends JFrame implements ActionListener {
private JTextField t1;
private JTextField t2;
private JTextField t3;
private JButton b1;
private JProgressBar bar;
Timer t;
int interval = 1000;
int i = 0;
public Progress() {
setLayout(new FlowLayout());
t1 = new JTextField(20);
t2 = new JTextField(20);
t3 = new JTextField(20);
b1 = new JButton("OK");
b1.addActionListener(this);
t = new Timer(interval, this);
bar = new JProgressBar();
bar.setStringPainted(true);
bar.setValue(0);
add(t1);
add(t2);
add(t3);
add(b1);
add(bar);
setSize(600, 480);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
if (t1.getText().isEmpty() || t2.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "Empty Fields");
}
else {
int w = Integer.parseInt(t1.getText());
int x = Integer.parseInt(t2.getText());
int res = w + x;
t3.setText("" + res);
}
}
}
}