Results 1 to 3 of 3
Thread: Problems Using JTextFields
- 02-01-2011, 03:21 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
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.
Moderator Edit: Code tags addedJava 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); } }Last edited by Fubarable; 02-01-2011 at 03:23 AM. Reason: Moderator Edit: Code tags added
- 02-02-2011, 07:28 AM #2
i made a few modifications.
implemented actionlistener interface on the class.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); } }
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 01:42 PM.
- 02-03-2011, 01:55 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
get values from JTextFields input
By cselic in forum Java 2DReplies: 3Last Post: 07-27-2010, 09:35 PM -
Java GUI, reading an Array Of JTextFields
By markious in forum New To JavaReplies: 3Last Post: 03-09-2010, 10:29 PM -
How to add lines to a JFrame full of JTextFields
By Dumisan in forum AWT / SwingReplies: 2Last Post: 02-07-2010, 09:12 PM -
Problem with JTextFields not null
By romina in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 05:17 AM -
JTextFields with username & password.
By Eric in forum AWT / SwingReplies: 2Last Post: 07-01-2007, 11:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks