Continues to run but never finishes
The following program will run forever and never finish....:(-:
I know it is because I am using the Scanner to get input, but I do not see what is happening?? :(nerd):
PLEASE HELP ME HERE!!:(handshake):
Code:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class PRG421 extends JFrame{
String[] stuff = {"Shoes", "Jewelery", "Electronics",
"Housewares", "Bathroom"};
JComboBox box = new JComboBox(stuff);
Scanner in1 = new Scanner(System.in);
double pryc;
double prcnt;
double reduct = prcnt / 100;
double salpryc = pryc - reduct;
private JPanel panel = new JPanel();
private JLabel L1, L2, L3, L4;
private JButton B1, B2, B3;
private JTextField txt1, txt2, txt3, txt4;
public PRG421() {
add(panel);
panel.add(box);
setLayout(new FlowLayout());
//L1 = new JLabel("Enter item name");
// add(L1);
txt1 = new JTextField(24);
add(txt1);
// L2 = new JLabel("Enter item price");
// add(L2);
double pryc = in1.nextDouble();
txt2 = new JTextField(24);
add(txt2);
// L3 = new JLabel("Enter sales percentage");
// add(L3);
double prcnt = in1.nextDouble();
txt3 = new JTextField(24);
add(txt3);
// L4 = new JLabel("Sale Price");
// add(L4);
txt4 = new JTextField(40);
add(txt4);
B1 = new JButton("Submit");
add(B1);
B2 = new JButton("Reset");
add(B2);
B3 = new JButton("Exit");
add(B3);
event1 e1 = new event1();
B1.addActionListener(e1);
event2 e2 = new event2();
B2.addActionListener(e2);
event3 e3 = new event3();
B3.addActionListener(e3);
}
public class event1 implements ActionListener{
public void actionPerformed(ActionEvent e1){
txt1.setText("Item price: " + pryc);
txt2.setText("Percentage reduced: " + prcnt);
txt4.setText("Sale price: " + salpryc);
}}
public class event2 implements ActionListener{
public void actionPerformed(ActionEvent e2){
txt1.setText("") ;
txt2.setText("");
txt3.setText("");
}}
public class event3 implements ActionListener{
public void actionPerformed(ActionEvent e3){
System.exit(0);
}}
public static void main(String[] args) {
PRG421 gui = new PRG421();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(480, 480);
gui.setVisible(true);
gui.setTitle("Title");
}
}
This just runs and runs and runs but never completes, and it is driving me nuts here!! :(shake):
Re: Continues to run but never finishes
Why do you need to get input using scanner in Swing application anyway? You can accept user input through the GUI it self. In you program you even don't use the two variables where you store those input.
Re: Continues to run but never finishes
I realize that this is possibly not the best way to do this, but I had already written this program for another purpose before getting this task to add the interface to it, and I am not a GUI expert by FAR!! :(think):
Re: Continues to run but never finishes
When you creating a console application then you'll use the scanner to read the user input. But in GUI you have the component such as JTextField where user can type their input. And then you can use the JTextField's getText() method to get the value.
Re: Continues to run but never finishes
... and either of:
-- add an ActionListener to the JTextField so you can press <Enter> to signify that you've finished entering text
-- add a DocumentListener to the JTextField's Document to update every time the text changes
-- use a JButton with an ActionListener that you can click to signify that all data has been entered
You'll find tutorials linked from the APis of just about every Swing component; they are a good learning resource.
db
Re: Continues to run but never finishes
Quote:
Originally Posted by
wsaryada
use the JTextField's getText() method to get the value.
Thanks wsaryada, I have been pretty much rearanging this entire thing to use the built in Java get functions to make it work right!! :coffee:
Grrrreat call yo.... :(clap):
Re: Continues to run but never finishes
Oh yeah, and thanks DarrylBurke as well!! :(handshake):
I am adding listeners as well to get things pumping.... :(nerd):