Results 1 to 3 of 3
  1. #1
    jrJava is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default Problems Using JTextFields

    I am having great difficulty with the code below which is suppose to calculate the average of 8 judges scores (excluding the lowest and highest scores). One of the biggest problems is that I can not seem to pass the info from a JTextField (judgeT) into a array of type double (scoreFinal), I tried to do this using the method below call setScores.

    Java Code:
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class JudgingGUI extends JFrame 
    {
       private static final int WIDTH = 800;
       private static final int HEIGHT = 600;
       private final int xCoordinate = 10;
       private final int yCoordinate = 10;
       
       private int length;
       private double scorePrime;
       public static double[] scoreFinal;
       private JLabel contestantL;
       
       private ScoreButtonHandler sbHandler;
       private ResetButtonHandler rbHandler; 
       
       private JTextField contestant,contestantScore;
       private JLabel contestantName,scoreL;
       
       JButton scoreB, resetB;
       JLabel [ ] labelAll;
       JLabel space;
       JLabel space2;
       JLabel space3;
       JTextField [ ] judgeT;
        
       public JudgingGUI()
       {
           setSize(WIDTH,HEIGHT);
           setLocation(xCoordinate,yCoordinate);
           setLayout(new FlowLayout());
           
           int length = 8;
         
           space2 = new JLabel("          ");                                                                                           
           space = new JLabel("                        ");
           add(space);
           
           scoreB = new JButton("Compute Score");
           sbHandler= new ScoreButtonHandler();
           scoreB.addActionListener(sbHandler);
           add(scoreB);  
           
           resetB = new JButton("Rest Scores");
           rbHandler = new ResetButtonHandler();
           resetB.addActionListener(sbHandler);
           add(resetB);
           
           contestantName = new JLabel("Name of Contestant");
           contestant = new JTextField(25);
           add(contestantName);
           add(contestant);
          
           add(space);
             
           labelAll = new JLabel[8];
           judgeT = new JTextField[8];
           scoreFinal = new double[8];
           text = new String[8];
           
           for(int h=0; h<length; h++)
           {
               labelAll[h] = new JLabel("Judge" +(h+1));
               add(labelAll[h]);
               judgeT[h] = new JTextField(5);
               add(judgeT[h]);
           }
          
           scoreL = new JLabel("Contestant's Score");
           add(scoreL);
           contestantScore = new JTextField(7);
           add(contestantScore);
           
           setScores(scoreFinal, length);
        }
         public void calculateScore(double values[],int size, double sum)
        {
            sum=0;
            double lowest;
            lowest = values[0];
            for (int j = 0; j<=size-1;j++)
            {
                if (values[j]<lowest)
                    lowest = values[j];
            }
    
            double high;
            high = values[0];
            for (int k = 0; k<=size-1;k++)
            {
                if (values[k] > high)
                high = values[k];
            }
            
            for (int index=0;index<values.length; index++)
            {
                sum = sum + values[index];
            }
            
            sum=sum-lowest-high;   
          }  
        
       public void setScores(double [] scores, int size)
       {
           scores = new double[size];
           for(int m=0; m<size; m++)
           {
                scoreFinal[m] = Double.parseDouble(judgeT[m].getText());
               
           }  
       }
        
       private class ScoreButtonHandler implements ActionListener
       {
            public void actionPerformed(ActionEvent e)
            {    
                
                contestantScore.setText("" + scorePrime);    
            }
       }
       
       private class ResetButtonHandler implements ActionListener
       {
           public void actionPerformed(ActionEvent e)
           {
               
           }
       }
    }
    
    public class AddGUI
    {
        
        public static void main(String [] args)
        {
            JudgingGUI example = new JudgingGUI();
            example.setVisible(true);
        }
    
        
    }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 02-01-2011 at 04:23 AM. Reason: Moderator Edit: Code tags added

  2. #2
    Join Date
    Dec 2008
    Location
    Manchester England
    Posts
    14
    Rep Power
    0

    Default

    i made a few modifications.

    Java Code:
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class JudgingGUI extends JFrame implements ActionListener
    {
       private static final int WIDTH = 800;
       private static final int HEIGHT = 600;
       private final int xCoordinate = 10;
       private final int yCoordinate = 10;
       
       private int length;
       private double scorePrime;
       // public static double[] scoreFinal; why static ?
       public double[] scoreFinal;
       private JLabel contestantL;
       
       //private ScoreButtonHandler sbHandler;
       //private ResetButtonHandler rbHandler; 
       
       private JTextField contestant,contestantScore;
       private JLabel contestantName,scoreL;
       
       JButton scoreB, resetB;
       JLabel [ ] labelAll;
       JLabel space;
       JLabel space2;
       JLabel space3;
       JTextField [ ] judgeT;
        
       public JudgingGUI()
       {
           setSize(WIDTH,HEIGHT);
           setLocation(xCoordinate,yCoordinate);
           setLayout(new FlowLayout());
           
           //int length = 8; what this, length is already declared
           length=8;
           scoreFinal = new double[length]; // dont forget to grab some memory
           
           space2 = new JLabel("          ");                                                                                           
           space = new JLabel("                        ");
           add(space);
           
           scoreB = new JButton("Compute Score");
           scoreB.addActionListener(this);
           //sbHandler= new ScoreButtonHandler();
           //scoreB.addActionListener(sbHandler);
           add(scoreB);  
           
           resetB = new JButton("Rest Scores");
           //rbHandler = new ResetButtonHandler();
           //resetB.addActionListener(sbHandler);
           add(resetB);
           
           contestantName = new JLabel("Name of Contestant");
           contestant = new JTextField(25);
           add(contestantName);
           add(contestant);
          
           add(space);
             
           labelAll = new JLabel[8];
           judgeT = new JTextField[8];
           scoreFinal = new double[8];
           //text = new String[8];
           
           for(int h=0; h<length; h++)
           {
               labelAll[h] = new JLabel("Judge" +(h+1));
               add(labelAll[h]);
               judgeT[h] = new JTextField(5);
               add(judgeT[h]);
           }
          
           scoreL = new JLabel("Contestant's Score");
           add(scoreL);
           contestantScore = new JTextField(7);
           add(contestantScore);
           
           //setScores(scoreFinal, length);
        }
       
        
       public void setScores(double [] scores, int size)
       {
           scores = new double[size];
           for(int m=0; m<size; m++)
           {
                scoreFinal[m] = Double.parseDouble(judgeT[m].getText());
                System.out.println(" "+scoreFinal[m]);
               
          }  
       }
        
      
            public void actionPerformed(ActionEvent e)
            { 
                Object source = e.getSource();
                if (source  == scoreB)
                {
                    setScores(scoreFinal, length);
                    System.out.println(length);
                    contestantScore.setText("" + scorePrime);
                }else{
                    //resetB code
                    //when you add an action listener
                }
            }
    }
       
    public class AddGUI
    {
        
        public static void main(String [] args)
        {
            JudgingGUI example = new JudgingGUI();
            example.setVisible(true);
        }
    
        
    }
    implemented actionlistener interface on the class.
    You seem confused about button events, check em out.
    Make sure you allocate space for your arrays, try using arraylists.
    Also you dont need to add parameters representing array length, its built in
    i.e arr.length, also the var length is an instance variable, it's got scope to all methods of the class.

    Also if you have several events to deal with the getSource() method is quite handy. Also remember if a textbox has no value the numberformat exception is gonna get thrown. Looks liked you rushing, slow down and work slower testing little bits as you go along. This prog could of been tested with just 2 textboxes and then when working add the rest, sure you can finish it off from here.
    Last edited by crystalClear; 02-02-2011 at 02:42 PM.

  3. #3
    jrJava is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

Similar Threads

  1. get values from JTextFields input
    By cselic in forum Java 2D
    Replies: 3
    Last Post: 07-27-2010, 10:35 PM
  2. Java GUI, reading an Array Of JTextFields
    By markious in forum New To Java
    Replies: 3
    Last Post: 03-09-2010, 11:29 PM
  3. How to add lines to a JFrame full of JTextFields
    By Dumisan in forum AWT / Swing
    Replies: 2
    Last Post: 02-07-2010, 10:12 PM
  4. Problem with JTextFields not null
    By romina in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 06:17 AM
  5. JTextFields with username & password.
    By Eric in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 12:41 AM

Posting Permissions

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