Results 1 to 7 of 7
  1. #1
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Question 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??
    PLEASE HELP ME HERE!!
    Java 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!!

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default 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.

  3. #3
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Default 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!!

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default 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.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Smile Re: Continues to run but never finishes

    Quote Originally Posted by wsaryada View Post
    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!!
    Grrrreat call yo....

  7. #7
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Default Re: Continues to run but never finishes

    Oh yeah, and thanks DarrylBurke as well!!
    I am adding listeners as well to get things pumping....

Similar Threads

  1. Calling a method on mainthread when another thread finishes
    By gauravfzd in forum Threads and Synchronization
    Replies: 5
    Last Post: 08-27-2012, 06:13 PM
  2. Replies: 2
    Last Post: 04-30-2012, 03:29 PM
  3. Replies: 1
    Last Post: 12-20-2010, 09:43 PM
  4. Replies: 0
    Last Post: 12-19-2010, 01:50 PM
  5. Replies: 10
    Last Post: 11-29-2010, 02:02 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •