Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Beginner Help (Action Listener)

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginner Help (Action Listener)

    Not sure what I'm doing wrong. My JButtons are not working. I've been trying to figure this out for quite a while. Any suggestions would be appreciated. Program is supposed to compute the score and reset the JTextFields. The actionlistener is a little messy, a better quicker way to make it work would be appreciated. Thank you.

     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class judgingGUI extends JFrame implements ActionListener
     
    {
         private final int FRAME_WIDTH = 775;
         private final int FRAME_HEIGHT = 300;
         private final int X_ORIGIN = 100;
         private final int Y_ORIGIN = 400;
     
     
        JTextField [] judges;
        JLabel [] labels;
        JButton reset_scores, comp_score;
        JTextField contestant, score, j1,j2,j3,j4,j5,j6,j7,j8,j9;;
        JLabel label;
        double score_a, scores[];
     
       public judgingGUI()
       {
            super("Judging");
            setLayout(new FlowLayout());
            setSize(FRAME_WIDTH,FRAME_HEIGHT);
            setLocation(X_ORIGIN,Y_ORIGIN);
     
     
     
     
            label = new JLabel("Name of Contestant", JLabel.CENTER); 
            contestant = new JTextField(18);
            contestant.setEditable(true);
            contestant.setBackground(Color.white);
            add(label);
            add(contestant);
     
            JTextField [] judges = new JTextField[8];
            JLabel [] labels = new JLabel[8];
     
            for(int i = 0;i < judges.length;i++)
            {
     
     
                labels[i] = new JLabel("judge" +(i+1), JLabel.LEFT);
                judges[i] = new JTextField(3);
     
                labels[i].setVerticalAlignment(JLabel.TOP); 
     
                judges[i].setBackground(Color.white);
                judges[i].setForeground(Color.black);
                judges[i].addActionListener(this);
                judges[i].setEditable(true);
                add(labels[i]);
                add(judges[i]);
     
     
            }
     
               comp_score = new JButton("Compute Score");
               comp_score.setBackground(Color.black);
               comp_score.setForeground(Color.white);
               comp_score.addActionListener(this);
               add(comp_score);
     
               label = new JLabel("Contestant's Score", JLabel.LEFT); 
               label.setVerticalAlignment(JLabel.TOP); 
               score = new JTextField(10);
               score.setEditable(false);
               score.setBackground(Color.white);
               add(score);
               add(label);
     
               label = new JLabel("Reset Scores for Next Contestant", JLabel.LEFT);
               reset_scores = new JButton("Reset");
               reset_scores.setBackground(Color.gray);
               reset_scores.setForeground(Color.red);
               reset_scores.addActionListener(this);
     
               add(reset_scores);
     
       }
     
        public void setScores(double [] scores) 
        {
                this.score_a = score_a;
        }
        public double [] getScores()
        {
                return scores;
        }
       public void actionPerformed(ActionEvent evt)
       {
                Object source = evt.getSource();
                if (source == comp_score)
       {
            scores = j1;    
            j1.setEditable(true);
            j2.setEditable(true);
            j3.setEditable(true);
            j4.setEditable(true);
            j5.setEditable(true);
            j6.setEditable(true);
            j7.setEditable(true);
            j8.setEditable(true);
     
        }
        else if (source == reset_scores)
        {
     
                j1.setText("");
                j1.setEditable(true);
                j2.setText("");
                j3.setText("");
                j4.setText("");
                j5.setText("");
                j6.setText("");
                j7.setText("");
                j8.setText("");
                contestant.setText("");
                j1.setEditable(true);
                j2.setEditable(true);
                j3.setEditable(true);
                j4.setEditable(true);
                j5.setEditable(true);
                j6.setEditable(true);
                j7.setEditable(true);
                j8.setEditable(true);
                contestant.setEditable(true);
     
            }
     
        }
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Beginner Help (Action Listener)

    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Help (Action Listener)

    Thanks that thread helped and my JButtons are functional accept for my compute button. I'm trying to call the calculateScores()-method in the my Judging class so it will total all the scores minus the min and max. Both classes are below. Please Help as this has been very frustrating pinpointing my error. Everything compiles it just doesn't run correctly.

    GUI class:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
     
    public class GUI extends JFrame implements ActionListener
    {
     
     
         private final int FRAME_WIDTH = 775;
         private final int FRAME_HEIGHT = 300;
         private final int X_ORIGIN = 100;
         private final int Y_ORIGIN = 400;
     
     
        private JTextField [] judges=new JTextField[8];
        private JLabel [] labels;
        JButton btnReset, btnCompute;
        private String coName;
        private JTextField contestant, scr;
        private JLabel label;
        double score_a, scores[], sc1;
        Judging judging=new Judging(8, "contestant");
     
     
       public GUI()
       {
            super("Judging");
            setLayout(new FlowLayout());
            setSize(FRAME_WIDTH,FRAME_HEIGHT);
            setLocation(X_ORIGIN,Y_ORIGIN);
     
     
     
     
            label = new JLabel("Name of Contestant", JLabel.CENTER); 
            contestant = new JTextField(18);
            contestant.setEditable(true);
            contestant.setBackground(Color.white);
            add(label);
            add(contestant);
     
            JLabel [] labels = new JLabel[8];
     
            for(int i = 0;i < judges.length;i++)
            {
     
     
                labels[i] = new JLabel("judge" +(i+1), JLabel.LEFT);
                judges[i] = new JTextField(3);
     
                labels[i].setVerticalAlignment(JLabel.TOP); 
     
                judges[i].setBackground(Color.white);
                judges[i].setForeground(Color.black);
                judges[i].addActionListener(this);
                judges[i].setEditable(true);
                add(labels[i]);
                add(judges[i]);
     
     
            }
     
               btnCompute = new JButton("Compute Score");
               btnCompute.setBackground(Color.black);
               btnCompute.setForeground(Color.white);
               btnCompute.addActionListener(this);
               add(btnCompute);
     
               label = new JLabel("Contestant's Score", JLabel.LEFT); 
               label.setVerticalAlignment(JLabel.TOP); 
               scr = new JTextField(10);
               scr.setEditable(false);
               scr.setBackground(Color.white);
               add(scr);
               add(label);
     
               label = new JLabel("Reset Scores for Next Contestant", JLabel.LEFT);
               btnReset = new JButton("Reset");
               btnReset.setBackground(Color.gray);
               btnReset.setForeground(Color.red);
               btnReset.addActionListener(this);
               add(btnReset);
     
        }
     
        public void setScores(double [] scores) 
        {
                this.scores = scores;
        }
        public double [] getScores()
        {
                return scores;
        }
     
     
        public void actionPerformed(ActionEvent e) 
        {
     
     
            Object source = e.getSource();
            double total= judging.calculateScore();
     
                if (source == btnCompute) 
                {
     
                    for (int i = 0; i < judges.length; ++i) 
                    {
                        judges[i].setEditable(true);
                    }
                } 
                else if (source == btnReset) 
                {
                    for (int i = 0; i < judges.length; ++i) 
                    {
                        judges[i].setText("");
                    }
                        contestant.setText("");
     
                    for (int i = 0; i < judges.length; ++i) 
                    {
                        judges[i].setEditable(true);
                    }
                        contestant.setEditable(true);
     
                 }
     
        }   
     
    }

    Judging Class
    import java.util.*;
     
    public class Judging
    {
     
        private double [] scores;
        private double total;
        private int n;
        private String contestant;
     
        public Judging(int n, String contestant)
        {
     
            this.n = n;
            this.contestant = contestant;
            scores = new double[n];
            total = 0;
     
        }    
        public void getData(Scanner console)
        {   
            boolean ok;
     
            if (n > scores.length)
            {
                System.out.println("The size of the array does not equal the "+
                    "scores asked for");
                System.exit(0);
            }        
     
                for(int i=0;i<n;i++)
                {
                    do
                    {
                        System.out.print("Enter the score for the judge number "+(i+1)+": ");
                            scores [i]= console.nextDouble();           
     
                        if (scores[i] < 1 || scores[i] > 10)
                        System.out.println("Sorry!  The score must be between 1 and 10");
     
                }
                    while (scores[i] < 1 || scores[i] > 10);
                }
                    console.nextLine();             
     
         }
     
           public  double calculateScore()
           {
            int largest, smallest;
     
     
             if (n > scores.length)
            {
                System.out.println("The size of the array does not equal the "+
                    "scores asked for");
                System.exit(0);
            }
     
            if (n < 3)
            {
                System.out.println("There must be at least 3 scores");
                return 0;
            }
     
            largest = findMax();
            smallest = findMin();
     
            total = 0;
     
            for(int i = 0; i < n; ++i)
                    total += scores[i];
     
            total -= (scores[smallest] + scores[largest]);        
     
           return total;
        }
     
        public double getTotal()
        {
            return total;
        }
     
        public double [] getScores()
        {
            return scores;
        }
     
        public String getContestant()
        {
            return contestant;
        }
     
        public void setContestant(String newContestant)
        {
            contestant = newContestant;
        }
     
        public void setScores(double [] scores)
        {
            this.scores = scores;
        }
     
     
         private  int findMin()
          {
     
     
             if (n <= 0 || n > scores.length)
             {
                   System.out.println("You have sent the wrong"+
                          " value for the number of elements in the array for"+
                          " findMin. -1 is being returned");
                   return - 1;
              }
              if (n < 0 || 0 > scores.length)
              {
                     System.out.println("You have sent the "+
                          "wrong value for the starting element to search in "+
                          "the array for findMin.  -1 is being returned");
                    return - 1;
              }
              int minIndex = 0;
              double minValue = scores[0];
     
              for(int i = 0 +1; i < n; ++i)
                    if (scores[i] < minValue)
                    {
                        minValue = scores[i];
                        minIndex = i;
                    }
     
              return minIndex;      
         }
     
         private  int findMax()
         {
              if (n <= 0 || n > scores.length)
             {
                   System.out.println("You have sent the wrong"+
                          " value for the number of elements in the array for"+
                          " findMax. -1 is being returned");
                   return - 1;
              }
              if (n < 0 || n > scores.length)
              {
                     System.out.println("You have sent the "+
                           "wrong value for the starting element to search in "+
                           "the array for findMax.  -1 is being returned");
                     return - 1;
              }
     
              int maxIndex = n-1;
              double maxValue = scores[n-1];
     
              for(int i = n-1; i > 0; --i)
                    if (scores[i] > maxValue)
                    {
                        maxValue = scores[i];
                        maxIndex = i;
                    }
     
              return maxIndex;      
         }
    }

Similar Threads

  1. Action Listener
    By kray66 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 19th, 2010, 03:26 PM
  2. Problem with Action Listener
    By JonoScho in forum AWT / Java Swing
    Replies: 4
    Last Post: March 19th, 2010, 01:03 AM
  3. Key Listener not working in jinternalframe
    By furqankhan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2010, 09:48 AM
  4. Key listener help
    By airsim15 in forum AWT / Java Swing
    Replies: 2
    Last Post: December 13th, 2009, 03:15 PM
  5. Need Help Action Listener....
    By vhizent23 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 9th, 2009, 01:46 PM