Write to text file through a java GUI - java

I have created a java GUI that works as a 6-question questionnaire. For each of the six question, I have 4 choices , i, ii, iii, iv for the user to choose, and then type out one of the four choices in a text box, then click a button "enter" to go to the next question. After all six questions have been answered, it will jump to a finish page.
I want to write the input value (in the text box) of each question to a text file. So After clicking on the "enter button" to answer all six questions I can see something like "i ii iii i ii iii" in a text file.
Is there a way to do it?
Here is my code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class HTML extends Applet implements ActionListener
{
private TextField question;
private Button enter, start;
int count = 0;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
String text, input;
private Label intro1, intro2;
private Label qone1, qone2, qone3, qone4, qone5, qone6;
private Label qtwo1, qtwo2, qtwo3, qtwo4, qtwo5, qtwo6;
private Label qthree1, qthree2, qthree3, qthree4, qthree5, qthree6;
private Label qfour1, qfour2, qfour3, qfour4, qfour5, qfour6;
private Label qfive1, qfive2, qfive3, qfive4, qfive5, qfive6;
private Label qsix1, qsix2, qsix3, qsix4, qsix5, qsix6;
private Label finish1, finish2, finish3;
public void init()
{
setLayout(null);
start = new Button ("Start");
question = new TextField(10);
enter = new Button ("Enter");
if (count == 0)
{
setBackground( Color.yellow);
intro1 = new Label("Target Advertising", Label.CENTER);
intro1.setFont(new Font("Times-Roman", Font.BOLD, 16));
intro2 = new Label("Welcome to this questionnaire. In order to show the most appropriate advertisement, we would like to know more about your personal preferences.");
add(intro1);
add(intro2);
intro1.setBounds(0,0,800,20);
intro2.setBounds(15,20,800,20);
add(start);
start.setBounds(370,60,70,23);
start.addActionListener(this);
}
if(count == 1)
{
setBackground( Color.yellow );
qone1 = new Label("Question 1", Label.LEFT);
qone1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qone2 = new Label("On average, How many hours do you spend on playing sports every week?");
qone3 = new Label("i.0-2");
qone4 = new Label("ii.3-6");
qone5 = new Label("iii.7-10");
qone6 = new Label("iv.10+");
add(qone1);
add(qone2);
add(qone3);
add(qone4);
add(qone5);
add(qone6);
qone1.setBounds(15,0,800,20);
qone2.setBounds(15,20,800,15);
qone3.setBounds(15,60,800,15);
qone4.setBounds(15,80,800,15);
qone5.setBounds(15,100,800,15);
qone6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if (count == 2)
{
qtwo1 = new Label("Question 2", Label.LEFT);
qtwo1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qtwo2 = new Label("On average, How many hours do you spend on qsixening to music every week?");
qtwo3 = new Label("i. 0-4 ");
qtwo4 = new Label("ii. 5-10");
qtwo5 = new Label("iii. 11-20");
qtwo6 = new Label("iv. 20+");
add(qtwo1);
add(qtwo2);
add(qtwo3);
add(qtwo4);
add(qtwo5);
add(qtwo6);
qtwo1.setBounds(15,20,800,15);
qtwo2.setBounds(15,40,800,15);
qtwo3.setBounds(15,60,800,15);
qtwo4.setBounds(15,80,800,15);
qtwo5.setBounds(15,100,800,15);
qtwo6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 3)
{
qthree1 = new Label("Question 3", Label.LEFT);
qthree1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qthree2 = new Label("On average, How many hours do you spend on using computers every week?");
qthree3 = new Label("i.0-2");
qthree4 = new Label("ii.3-10");
qthree5 = new Label("iii.11-15");
qthree6 = new Label("iv.20+");
add(qthree1);
add(qthree2);
add(qthree3);
add(qthree4);
add(qthree5);
add(qthree6);
qthree1.setBounds(15,20,800,20);
qthree2.setBounds(15,40,800,15);
qthree3.setBounds(15,60,800,15);
qthree4.setBounds(15,80,800,15);
qthree5.setBounds(15,100,800,15);
qthree6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 4)
{
qfour1 = new Label("Question 4", Label.LEFT);
qfour1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qfour2 = new Label("On average, How many hours do you spend on groceries every week?");
qfour3 = new Label("i.0-2");
qfour4 = new Label("ii.3-10");
qfour5 = new Label("iii.11-15");
qfour6 = new Label("iv.20+");
add(qfour1);
add(qfour2);
add(qfour3);
add(qfour4);
add(qfour5);
add(qfour6);
qfour1.setBounds(15,20,800,15);
qfour2.setBounds(15,40,800,15);
qfour3.setBounds(15,60,800,15);
qfour4.setBounds(15,80,800,15);
qfour5.setBounds(15,100,800,15);
qfour6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 5)
{
qfive1 = new Label("Question 5", Label.LEFT);
qfive1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qfive2 = new Label("On average, How many hours do you spend on watching TV every week?");
qfive3 = new Label("i.0-2");
qfive4 = new Label("ii.3-10");
qfive5 = new Label("iii.11-15");
qfive6 = new Label("iv.20+");
add(qfive1);
add(qfive2);
add(qfive3);
add(qfive4);
add(qfive5);
add(qfive6);
qfive1.setBounds(15,20,800,15);
qfive2.setBounds(15,40,800,15);
qfive3.setBounds(15,60,800,15);
qfive4.setBounds(15,80,800,15);
qfive5.setBounds(15,100,800,15);
qfive6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 6)
{
qsix1 = new Label("Question 6", Label.LEFT);
qsix1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qsix2 = new Label("On average, How many times do you spend with family every week?");
qsix3 = new Label("i.0-2");
qsix4 = new Label("ii.3-10");
qsix5 = new Label("iii.11-15");
qsix6 = new Label("iv.20+");
add(qsix1);
add(qsix2);
add(qsix3);
add(qsix4);
add(qsix5);
add(qsix6);
qsix1.setBounds(15,20,800,15);
qsix2.setBounds(15,40,800,15);
qsix3.setBounds(15,60,800,15);
qsix4.setBounds(15,80,800,15);
qsix5.setBounds(15,100,800,15);
qsix6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
finish1 = new Label("Thank You." , Label.CENTER);
finish1.setFont(new Font("Times-Roman", Font.BOLD, 50));
finish2 = new Label("Questionnaire Completed.", Label.CENTER);
finish2.setFont(new Font("Times-Roman", Font.BOLD, 50));
add(finish1);
add(finish2);
finish1.setBounds(0,200,800,60);
finish2.setBounds(0,300,800,60);
}
}
public void actionPerformed(ActionEvent ae)
{
String button = ae.getActionCommand();
text = question.getText();
b = 0;
c = 0;
if (count == 6)
{
input = text.toUpperCase();
remove(enter);
remove(question);
question.setText("");
remove(qsix1);
remove(qsix2);
remove(qsix3);
remove(qsix4);
remove(qsix5);
remove(qsix6);
if(input.equals("OL"))
{
b = 1;
count = 7;
init();
}
else
{
b = 2;
count = 7;
init();
}
}
if (count == 5)
{
input = text.toUpperCase();
remove(enter);
remove(question);
question.setText("");
remove(qfive1);
remove(qfive2);
remove(qfive3);
remove(qfive4);
remove(qfive5);
remove(qfive6);
if(input.equals("BR"))
{
b = 1;
count = 6;
init();
}
else
{
b = 2;
count = 6;
init();
}
}
if (count == 4)
{
input = text.toLowerCase();
remove(enter);
remove(question);
question.setText("");
remove(qfour1);
remove(qfour2);
remove(qfour3);
remove(qfour4);
remove(qfour5);
remove(qfour6);
}
if(input.equals("no"))
{
b = 1;
count = 5;
init();
}
else
{
b = 2;
count = 5;
init();
}
}
if (count == 3)
{
input = text.toLowerCase();
remove(enter);
remove(question);
question.setText("");
remove(qthree1);
remove(qthree2);
remove(qthree3);
remove(qthree4);
remove(qthree5);
remove(qthree6);
if(input.equals("black"))
{
b = 1;
count = 4;
init();
}
else
{
b = 2;
count = 4;
init();
}
}
if (count == 2)
{
input = text.toLowerCase();
remove(enter);
remove(question);
question.setText("");
remove(qtwo1);
remove(qtwo2);
remove(qtwo3);
remove(qtwo4);
remove(qtwo5);
remove(qtwo6);
if(input.equals("yes"))
{
b = 1;
count = 3;
init();
}
else
{
b = 2;
count = 3;
init();
}
}
if (count == 1)
{
input = text.toUpperCase();
remove(enter);
remove(question);
question.setText("");
remove(qone1);
remove(qone2);
remove(qone3);
remove(qone4);
remove(qone5);
remove(qone6);
if(input.equals("i"))
{
b = 1;
count = 2;
init();
}
else
{
b = 1;
count = 1;
init();
}
}
if (count == 0)
{
remove(intro1);
remove(intro2);
remove(start);
count = 1;
init();
}
this.validate();
}
}

Sure, something like
OutputStream out = new FileOutputStream("/path/to/your/file");
out.write(answer.getBytes());
out.close();

Related

How can I clear the JTextField and have it ready for input immediately without requiring a click or other action?

I posted my full code below so you guys could have a solid understanding of what I want. When I click send, the text field clears but is not ready for input. I have to click for the input. I want it to be ready for the input immediately and textfield.setText("") will not work. Any ideas?
package javaapplication2;
import java.util.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.io.*;
import javax.swing.*;//class to use to create GUI's
import javax.swing.border.Border;
import javax.swing.plaf.FontUIResource;
/**
*
* #author Jordan Anthony Hangman with dictionary
*/
public class JavaApplication2 extends JFrame{
static JLabel[] labels = new JLabel[20];//labels for displaying words under 20 characters
static int counter = 0;
static int badguess = 0;//keeps track of wrong guesses
static String wordbank = " Letter Bank:";
static char[] guesses = new char[26];
static int nguesses = 0;
static int amountc = 0;
public JavaApplication2(){
}
public static void main(String[] args) throws FileNotFoundException, IOException {
//Loading images to use for hangman game
ImageIcon hanginit = new ImageIcon("hangmaninit.jpg");
ImageIcon hang1 = new ImageIcon("hangman1.jpg");
ImageIcon hang2 = new ImageIcon("hangman2.jpg");
ImageIcon hang3 = new ImageIcon("hangman3.jpg");
ImageIcon hang4 = new ImageIcon("hangman4.jpg");
ImageIcon hang5 = new ImageIcon("hangman5.jpg");
ImageIcon hang6 = new ImageIcon("hangman6.jpg");
//initializing array for index values
int wordcount = 10;
String[] words1 = new String[wordcount];
int[] wordssel = new int[10];
for (int j=0; j<10; j++){
wordssel[j] = -1;
}
//setting instructions
String Instructions = "To start the game, click on the new game button. You get 6 incorrect guesses for every word. Once you get 6 guesses, the game will end. Click the end game button to go back to the home page. If you want to play again, you can click New Game and you will be able to guess a different word. After every game, a message will appear to tell you how many words you have guesses correctly. When all the words are played, a dialog box will be shown that says Game Over. Exit this box to exit the game.";
//reading in the words
int number = 0;
try(BufferedReader br = new BufferedReader(new FileReader("words.txt")))
{
String line = br.readLine();
while(line != null){
words1[number] = line;
number++;
line = br.readLine();
}
}
//randomizes words
boolean flag = false;
int rand = 0;
int count = 0;
int wordcounter = 0;
while(!flag){
Random random = new Random();
rand = random.nextInt(wordcount);
count = 0;
for (int o=0; o<wordcount; o++){
if(wordssel[o] != rand){
count++;
}
}
if(count == wordcount){
wordssel[wordcounter] = rand;
wordcounter++;
}
if(wordcounter == wordcount){
flag = true;
}
}
String[] randwords = new String[10];
for(int o=0; o<10; o++){
randwords[wordssel[o]] = words1[o];
}
//initializing label
for(int i=0; i<20; i++){
labels[i] = new JLabel(" ");
labels[i].setFont(new Font("Serif", Font.BOLD, 30));
}
//initializing guessedchar array
for(int i=0; i<26; i++){
guesses[i] = ' ';
}
//value for window size
int xlim = 1200;
int ylim = 1200;
int xlim2 = 1600;
int ylim2 = 1600;
int xlim3 = 900;
int ylim3 = 600;
//value for x and y centers
int xcent = xlim/2 - 20;
int ycent = ylim/2;
int xcent2 = xlim2/2;
int ycent2 = ylim2/2 - 20;
int xcent3 = xlim3/2;
int ycent3 = ylim3/2 - 20;
//creating frames
JFrame frame1 = new JFrame("Hangman");//creates a new frame or window
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// I thiunk exits when clicks on red x
frame1.setSize(xlim, ylim); //size of window in pixels
JFrame frame2 = new JFrame("Hangman");//creates a new frame or window
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// I thiunk exits when clicks on red x
frame2.setSize(xlim2, ylim2); //size of window in pixels
JFrame frame3 = new JFrame();
frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame3.setSize(xlim3, ylim3);
//change font size for label, button, and textfield
UIManager.put("Button.font", new FontUIResource(new Font("Dialog", Font.BOLD, 24)));
UIManager.put("Label.font", new FontUIResource(new Font("Dialog", Font.PLAIN, 24)));
UIManager.put("TextField.font", new FontUIResource(new Font("Dialog", Font.PLAIN, 24)));
UIManager.put("TextArea.font", new FontUIResource(new Font("Dialog", Font.PLAIN, 24)));
//creates buttons
JButton button1 = new JButton("New Game?");
button1.setBounds(xcent - 90, ycent - 165, 180, 130);//(xstart, ytop, width, height)
JButton button2 = new JButton("End Game?");
button2.setBounds(xcent - 90, ycent - 65, 180, 130);//(xstart, ytop, width, height)
JButton button3 = new JButton("Instructions");
button3.setBounds(xcent - 120, ycent, 240, 100);
JButton button4 = new JButton("Main Menu");
button4.setBounds(xcent - 100, ycent + 100, 200, 100);
//creating border
Border border = BorderFactory.createLineBorder(Color.BLACK, 4);
//creating labels
JLabel labelresult = new JLabel("");//label for win or losing game
labelresult.setBounds(400, 400, 400, 40);
JLabel labelhang = new JLabel();//label for hangman images
labelhang.setBounds(450, 375, 677, 620);
JLabel label3 = new JLabel("Hello");
label3.setBounds(200, 260, 500, 40);
JLabel label4 = new JLabel("Hello");
label4.setBounds(440, 1300, 800, 40);
JLabel label5 = new JLabel("Hello");
label5.setBounds(590, 1300, 800, 40);
JLabel label6 = new JLabel("Game Over");
label6.setBounds(380, 250, 200, 40);
JLabel lettersw = new JLabel();
JLabel label7 = new JLabel("How To Play");
label7.setBounds(500, 50, 200, 40);
JLabel label8 = new JLabel("Error");
label8.setBounds(590, 1300, 800, 40);
label8.setText("You can only enter one letter at a time.");
JLabel label9 = new JLabel("Error");
label9.setBounds(674, 1300, 800, 40);
label9.setText("You must enter a letter");
JLabel label10 = new JLabel(wordbank);
label10.setBounds(xcent2 - 200, 220, 400, 40);
JLabel label11 = new JLabel("You have already entered this letter.");
label11.setBounds(608, 1300, 800, 40);
JLabel label12 = new JLabel("You must exit game to guess.");
label12.setBounds(639, 1300, 800, 40);
//creating text area and setting properties
JTextArea texta1 = new JTextArea();
texta1.setOpaque(false);
texta1.setLineWrap(true);
texta1.setWrapStyleWord(true);
texta1.setBounds(100, 100, 1000, 600);
texta1.setText(Instructions);
//adding items to frames
frame1.add(button1);
frame1.add(button3);
frame2.add(button2);
frame2.add(labelhang);
frame3.add(label6);
frame2.add(label4);
frame2.add(label5);
frame3.add(label6);
frame1.add(label7);
frame1.add(texta1);
frame1.add(button4);
frame2.add(label8);
frame2.add(label9);
frame2.add(label10);
frame2.add(label11);
frame2.add(label12);
//set item properties
labelresult.setVisible(false);
button2.setVisible(false);
labelhang.setIcon(hanginit);
label4.setVisible(false);
label5.setVisible(false);
label7.setVisible(false);
texta1.setVisible(false);
button4.setVisible(false);
label8.setVisible(false);
label9.setVisible(false);
label10.setBorder(border);
label11.setVisible(false);
label12.setVisible(false);
//set frame properties
frame1.setLayout(null);
frame1.setVisible(true);//makes the frame visible to the user
frame3.setLayout(null);
//When End Game button is pressed
button2.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
//end of game conditions
label5.setVisible(false);
label4.setVisible(false);
label12.setVisible(false);
button2.setVisible(false);
frame2.setVisible(false);
wordbank = " Letter Bank:";
badguess = 0;
amountc = 0;
if(counter == wordcount){
frame3.setVisible(true);//frame after all words gone through
}
else{
labelhang.setIcon(hanginit);
frame1.setVisible(true);
}
}
});
//When Instructions button is pressed
button3.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
button1.setVisible(false);
button3.setVisible(false);
label7.setVisible(true);
texta1.setVisible(true);
button4.setVisible(true);
}
});
//When Main Menu is pressed
button4.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
button1.setVisible(true);
button4.setVisible(false);
button2.setVisible(true);
label7.setVisible(false);
texta1.setVisible(false);
button3.setVisible(true);
}
});
//When New Game is press
button1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
//set item properties
frame2.setLayout(null);
frame2.setVisible(true);
frame1.setVisible(false);
button2.setVisible(false);
label8.setVisible(false);
label9.setVisible(false);
wordbank = " Letter Bank:";
label10.setText(wordbank);
//initializes labels and word
for(int i=0; i<20; i++){
labels[i].setText(" ");
}
String currentword = randwords[counter];
counter++;
//initializing previos guesses
for(int i=0; i<26; i++){
guesses[i] = ' ';
}
nguesses = 0;
//creating panel and panel components always create panels and all components in the same place that they are being added
JButton send = new JButton("Send");
JButton clear = new JButton("clear");
JTextField tf = new JTextField(1);
JLabel label = new JLabel("Enter Guess");
JPanel panel = new JPanel();
panel.add(label);
panel.add(tf);
panel.add(send);
panel.add(clear);
panel.setBounds(xcent2 - 250, ylim2 - 102, 500, 40);
frame2.add(panel);
//placing labels for letters
int letters = currentword.length();
int spacing2 = 800-(int)(letters*12.5);
for (int i=0; i<letters; i++){
labels[i].setText("_");
int spacing = 25 * i;
labels[i].setBounds(spacing2 + spacing, 1100, 25, 40);
frame2.add(labels[i]);
}
//when clear button is pressed
clear.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
tf.setText("");
}
});
//when send button is pressed
send.addActionListener(new ActionListener(){
int amountc1 = 0;//keeps track of correctly guessed amount of words
int counter2 = 0;//keeps track of amount of correct letter guesses for each word
int letters2 = 0;
#Override
public void actionPerformed(ActionEvent e){
//initialize word and user guess
String currentword = randwords[counter-1];
letters2 = currentword.length();
char[] wordcurrent = new char[20];
wordcurrent = currentword.toCharArray();
String userguess = tf.getText();
userguess = userguess.toLowerCase();
tf.setText("");
//set properties
label4.setVisible(false);
label5.setVisible(false);
label8.setVisible(false);
label9.setVisible(false);
label11.setVisible(false);
//checking guess with word
boolean flag2 = false;
boolean flag3 = false;
//checking if word is already solved and user is entering data
System.out.println(badguess);
System.out.println(amountc);
System.out.println(nguesses);
if(nguesses > badguess + amountc){
label4.setVisible(false);
label5.setVisible(false);
label12.setVisible(true);
nguesses++;
flag2 = true;
}
else if(userguess.length() > 1){
label8.setVisible(true);
flag2 = true;
nguesses++;
}
else if(userguess.length() < 1){
label9.setVisible(true);
flag2 = true;
nguesses++;
}
else{
char[] guessuser = userguess.toCharArray();
char guessedchar = guessuser[0];
int charval = (int)guessedchar;//97-122 lowercase, 65-90 for uppercase
//checks for previous guesses
for(int t=0; t<=nguesses; t++){
if(guessedchar == guesses[t]){
flag3 = true;
break;
}
}
nguesses++;
//checking word against guess
for(int i=0; i<letters2; i++){
if(!flag3){
if(((charval>96 & charval<123))){
guesses[nguesses] = guessedchar;
if(wordcurrent[i] == guessedchar){
labels[i].setText(userguess);
labels[i].setVisible(true);
flag2 = true;
counter2++;
nguesses++;
amountc++;
}
}
else{
label9.setVisible(true);
flag2 = true;
}
}
else{
label11.setVisible(true);
flag2 = true;
}
}
}
//checks if full word is guessed correctly
if(counter2 == letters2){
this.amountc1++;
nguesses++;
button2.setVisible(true);
String correctword = "Well done! You have solved " + amountc1 + " out of " + counter;
label5.setText(correctword);
label5.setVisible(true);
counter2 = 0;
if(counter == wordcount){
label3.setText(correctword);
}
}
//checks for wrong guess and changes hangman picture accordingly
if (flag2 == false){
badguess++;
switch(badguess){
case 1:
wordbank += " " + userguess;
label10.setText(wordbank);
labelhang.setIcon(hang1);
break;
case 2:
wordbank += ", " + userguess;
label10.setText(wordbank);
labelhang.setIcon(hang2);
break;
case 3:
wordbank += ", " + userguess;
label10.setText(wordbank);
labelhang.setIcon(hang3);
break;
case 4:
wordbank += ", " + userguess;
label10.setText(wordbank);
labelhang.setIcon(hang4);
break;
case 5:
wordbank += ", " + userguess;
label10.setText(wordbank);
labelhang.setIcon(hang5);
break;
case 6:
nguesses++;
wordbank += ", " + userguess;
label10.setText(wordbank);
labelhang.setIcon(hang6);
button2.setVisible(true);
String incorrectword = "Sorry, The correct word was " + currentword + " You have solved " + amountc1 + " out of " + counter;
label4.setText(incorrectword);
label4.setVisible(true);
this.counter2 = 0;
if(counter == wordcount){
label3.setText(incorrectword);
}
break;
default:
break;
}
}
else{
nguesses--;
}
}
});
}
});
}
}
To focus text field after setText(""); method you can use the following,
textFieldName.requestFocus();
To center the text field you can use this code,
textFieldName.setHorizontalAlignment(JTextField.CENTER);

Creating a new JFrame inside actionPerformed() method , and then how can i perform action with this JFrame's JButton

I'm very new to java.My question is,is it possible to use JButton v,v1 ? Inside this actionPerformed() method ? If it is possible,then how ?
Thanks in advance .
PlaceOrder.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class PlaceOrder extends JFrame implements ActionListener
{
JButton confirm,cancel,logOut;
JRadioButton rb1,rb2,rb3,rb4,rb5,rb6,rb7,rb8;
JTextField r1,r2,r3,r4,r5,r6,r7,r8;
JLabel Item,Quantity;
Food f[] = new Food[10];
double total = 0;
public PlaceOrder()
{
f[0] = new Food("Chicken Burger",120);
f[1] = new Food("Chicken BBQ Burger",150);
f[2] = new Food("Chicken BBQ Cheese Burger",170);
f[3] = new Food("Beef Burger",130);
f[4] = new Food("Beef BBQ Burger",160);
f[5] = new Food("Pizza",180);
f[6] = new Food("Coffee",80);
f[7] = new Food("Soft Drinks(Can)",35);
setSize(700,700);
setVisible(true);
setLayout(null);
rb1 = new JRadioButton("Chicken Burger");
rb2 = new JRadioButton("Chicken BBQ Burger");
rb3 = new JRadioButton("Chicken BBQ Cheese Burger");
rb4 = new JRadioButton("Beef Burger");
rb5 = new JRadioButton("Beef BBQ Burger");
rb6 = new JRadioButton("Pizza");
rb7 = new JRadioButton("Coffee");
rb8 = new JRadioButton("Soft Drinks(Can)");
rb1.setBounds(150,100,150,30);
add(rb1);
rb2.setBounds(150,130,150,30);
add(rb2);
rb3.setBounds(150,160,150,30);
add(rb3);
rb4.setBounds(150,190,150,30);
add(rb4);
rb5.setBounds(150,220,150,30);
add(rb5);
rb6.setBounds(150,250,150,30);
add(rb6);
rb7.setBounds(150,280,150,30);
add(rb7);
rb8.setBounds(150,310,150,30);
add(rb8);
r1 = new JTextField();
r2 = new JTextField();
r3 = new JTextField();
r4 = new JTextField();
r5 = new JTextField();
r6 = new JTextField();
r7 = new JTextField();
r8 = new JTextField();
Item = new JLabel("Item");
Quantity = new JLabel("Quantity");
Item.setBounds(160,20,100,50);
add(Item);
Quantity.setBounds(360,20,100,50);
add(Quantity);
r1.setBounds(350,100,150,20);
add(r1);
r2.setBounds(350,130,150,20);
add(r2);
r3.setBounds(350,160,150,20);
add(r3);
r4.setBounds(350,190,150,20);
add(r4);
r5.setBounds(350,220,150,20);
add(r5);
r6.setBounds(350,250,150,20);
add(r6);
r7.setBounds(350,280,150,20);
add(r7);
r8.setBounds(350,310,150,20);
add(r8);
confirm = new JButton("Order Status");
cancel = new JButton("Cancel Order");
logOut = new JButton("LogOut");
confirm.setBounds(150,500,120,30);
add(confirm);
cancel.setBounds(380,500,120,30);
add(cancel);
logOut.setBounds(550,50,80,30);
add(logOut);
confirm.addActionListener(this);
cancel.addActionListener(this);
logOut.addActionListener(this);
}
public void actionPerformed(ActionEvent a)
{
if(a.getSource() == confirm)
{
JFrame YourOrder = new JFrame();
YourOrder.setSize(700,800);
YourOrder.setLayout(null);
YourOrder.setVisible(true);
JLabel confirmation = new JLabel("Your Order");
confirmation.setBounds(250,30,150,30);
YourOrder.add(confirmation);
JLabel s[] = new JLabel[3];
s[0] = new JLabel("Item");
s[0].setBounds(100,100,50,30);
YourOrder.add(s[0]);
s[1] = new JLabel("Quantity");
s[1].setBounds(250,100,50,30);
YourOrder.add(s[1]);
s[2] = new JLabel("Total");
s[2].setBounds(400,100,50,30);
YourOrder.add(s[2]);
JLabel l,l1,l2;
JLabel netTotal = new JLabel("Net Amount = ");
netTotal.setBounds(400,550,100,30);
YourOrder.add(netTotal);
JButton v,v1;
v = new JButton("Change");
v.setBounds(100,600,100,30);
YourOrder.add(v);
v.addActionListener(this);
v1 = new JButton("Confirm Order");
v1.setBounds(400,600,120,30);
YourOrder.add(v1);
v1.addActionListener(this);
if(rb1.isSelected() && r1.getText() != "")
{
total = total + ((Integer.parseInt(r1.getText()))*(f[0].getFoodPrice()));
l = new JLabel(rb1.getText());
l.setBounds(100,150,100,30);
YourOrder.add(l);
l1 = new JLabel(r1.getText());
l1.setBounds(250,150,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r1.getText()))*(f[0].getFoodPrice())));
l2.setBounds(400,150,100,30);
YourOrder.add(l2);
}
if(rb2.isSelected() && r2.getText() != "")
{
total = total + ((Integer.parseInt(r2.getText()))*(f[1].getFoodPrice()));
l = new JLabel(rb2.getText());
l.setBounds(100,200,100,30);
YourOrder.add(l);
l1 = new JLabel(r2.getText());
l1.setBounds(250,200,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r2.getText()))*(f[1].getFoodPrice())));
l2.setBounds(400,200,100,30);
YourOrder.add(l2);
}
if(rb3.isSelected() && r3.getText() != "")
{
total = total + ((Integer.parseInt(r3.getText()))*(f[2].getFoodPrice()));
l = new JLabel(rb3.getText());
l.setBounds(100,250,100,30);
YourOrder.add(l);
l1 = new JLabel(r3.getText());
l1.setBounds(250,250,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r3.getText()))*(f[2].getFoodPrice())));
l2.setBounds(400,250,100,30);
YourOrder.add(l2);
}
if(rb4.isSelected() && r4.getText() != "")
{
total = total + ((Integer.parseInt(r4.getText()))*(f[3].getFoodPrice()));
l = new JLabel(rb4.getText());
l.setBounds(100,300,100,30);
YourOrder.add(l);
l1 = new JLabel(r4.getText());
l1.setBounds(250,300,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r4.getText()))*(f[3].getFoodPrice())));
l2.setBounds(400,300,100,30);
YourOrder.add(l2);
}
if(rb5.isSelected() && r5.getText() != "")
{
total = total + ((Integer.parseInt(r5.getText()))*(f[4].getFoodPrice()));
l = new JLabel(rb5.getText());
l.setBounds(100,350,100,30);
YourOrder.add(l);
l1 = new JLabel(r5.getText());
l1.setBounds(250,350,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r5.getText()))*(f[4].getFoodPrice())));
l2.setBounds(400,350,100,30);
YourOrder.add(l2);
}
if(rb6.isSelected() && r6.getText() != "")
{
total = total + ((Integer.parseInt(r6.getText()))*(f[5].getFoodPrice()));
l = new JLabel(rb6.getText());
l.setBounds(100,400,100,30);
YourOrder.add(l);
l1 = new JLabel(r6.getText());
l1.setBounds(250,400,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r6.getText()))*(f[5].getFoodPrice())));
l2.setBounds(400,400,100,30);
YourOrder.add(l2);
}
if(rb7.isSelected() && r7.getText() != "")
{
total = total + ((Integer.parseInt(r7.getText()))*(f[6].getFoodPrice()));
l = new JLabel(rb7.getText());
l.setBounds(100,450,100,30);
YourOrder.add(l);
l1 = new JLabel(r7.getText());
l1.setBounds(250,450,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r7.getText()))*(f[6].getFoodPrice())));
l2.setBounds(400,450,100,30);
YourOrder.add(l2);
}
if(rb8.isSelected() && r8.getText() != "")
{
total = total + ((Integer.parseInt(r8.getText()))*(f[7].getFoodPrice()));
l = new JLabel(rb8.getText());
l.setBounds(100,500,100,30);
YourOrder.add(l);
l1 = new JLabel(r8.getText());
l1.setBounds(250,500,100,30);
YourOrder.add(l1);
l2 = new JLabel(String.valueOf((Integer.parseInt(r8.getText()))*(f[7].getFoodPrice())));
l2.setBounds(400,500,100,30);
YourOrder.add(l2);
}
JLabel pay = new JLabel(String.valueOf(total));
pay.setBounds(500,550,100,30);
YourOrder.add(pay);
if(a.getSource() == v)
{
YourOrder.setVisible(false);
}
else if(a.getSource() == v1)
{
JOptionPane.showMessageDialog(this,"Printing Invoice ...");
}
}
else if(a.getSource() == cancel)
{
this.setVisible(false);
PlaceOrder o1 = new PlaceOrder();
}
else
{
this.setVisible(false);
}
}
}
Food.java
class Food
{
private String foodName;
private double foodPrice;
public Food(String foodName,double foodPrice)
{
this.foodName = foodName;
this.foodPrice = foodPrice;
}
public String getFoodName()
{
return this.foodName;
}
public double getFoodPrice()
{
return this.foodPrice;
}
}
These two classes ..
My guess: you want to be able to refer to the local variables in your code image above, v and v1, but being local to the method, their scope or "visibility" is also limited to the method. If so, then get that code out of the actionPerformed and instead into its own class, one that creates a JPanel that is set up as you desire. You can then make v and v1 fields of the class, and allow outside code access to necessary properties via getter methods.
If you need more detailed help and less guesses, again please post a viable mcve. If you do this, we can work with and even sometimes help enhance your code.
Other issues: avoid null layouts and setBounds since these lead to rigid and hard to enhance GUI's that look bad on most platforms other than your own.
Also check out The Use of Multiple JFrames, Good/Bad Practice?
Edit
That confirm order window should be a modal JDialog and not a JFrame. You want the program to stop and not go forward until the user has dealt with it, which is what a modal dialog will do. Also, I would use JSpinner and not JTextFields to get the quantity information. Why? You must always assume that the user is an idiot, and using a JSpinner you would prevent the user from entering non-numeric or other incorrect information.

Binary Search, Array Index Out of Bounds Java

Firstly, I would like to say I am not asking for an answer more so advice so I can learn from this problem. Every time I run my code I get Array Index Out of Bounds on Line 254 of the ArrayOperations java file.
if (SearchKey == tempArray[middle])
I have added as many checks as I possibly can to this code and also have commented it as much as possible for your ease. Once again I am looking for advice on this not just a direct answer if possible. The Error always seems to give me the negative version of 1/2 of my data items
ex: 100,000 data items, Array Index Out of Bounds: -49,999.
import javax.swing.*;
// File-related imports
import java.io.IOException;
import java.util.Scanner;
import java.io.File;
public class ArrayOperations
{
// File Parameters
String DataFilePath;
String DataFileName;
String KeysFilePath;
String KeysFileName;
int NumberOfDataItems;
int NumberOfKeys;
int N;
int BucketHashArraySize;
int NoBuckets;
//Array
int[] OriginalArray = new int[1000000];
int[] SortedArray = new int[1000000];
int[] HashedArray = new int[2000000];
int[] BucketHashedArray = new int[2000000];
int[] KeysArray = new int[1000000];
long SSAverageAccessTime;
long SSAverageCompSuc;
long SSAverageCompFailed;
long SSNumberKeysSuc;
long SSNumberKeysFailed ;
long BSAverageAccessTime;
long BSAverageCompSuc ;
long BSAverageCompFailed;
long BSNumberKeysSuc ;
long BSNumberKeysFailed ;
long HSAverageAccessTime;
long HSAverageCompSuc ;
long HSAverageCompFailed;
long HSNumberKeysSuc ;
long HSNumberKeysFailed ;
public ArrayOperations()
{
// File Parameters
DataFilePath = null;
DataFileName = null;
KeysFilePath = null;
KeysFileName = null;
NumberOfDataItems=0;
NumberOfKeys =0;
N =0;
BucketHashArraySize = 0;
NoBuckets =0;
// Statistics
SSAverageAccessTime = 0;
SSAverageCompSuc = 0;
SSAverageCompFailed = 0;
SSNumberKeysSuc = 0;
SSNumberKeysFailed = 0;
}
public void ReadDataFile() throws IOException
{
JFileChooser chooser = new JFileChooser();
chooser.setDialogType(JFileChooser.OPEN_DIALOG );
chooser.setDialogTitle("Open Data File");
int returnVal = chooser.showOpenDialog(null);
if( returnVal == JFileChooser.APPROVE_OPTION)
{
DataFilePath = chooser.getSelectedFile().getPath();
DataFileName = chooser.getSelectedFile().getName();
}
// read data file and copy it to original array
if (DataFilePath != null)
{
try
{
int index = 0;
Scanner integerTextFile = new Scanner(new File(DataFilePath));
while (integerTextFile.hasNext())
{
// read the next integer
OriginalArray[index] = integerTextFile.nextInt();
index++;
}
// end of file detected
integerTextFile.close();
NumberOfDataItems = index;
}
catch (IOException ioe)
{
System.exit(0);
}
}
else
NumberOfDataItems = 0;
}
public void ReadKeysFile() throws IOException
{
JFileChooser chooser = new JFileChooser();
chooser.setDialogType(JFileChooser.OPEN_DIALOG );
chooser.setDialogTitle("Open Keys File");
int returnVal = chooser.showOpenDialog(null);
if( returnVal == JFileChooser.APPROVE_OPTION)
{
KeysFilePath = chooser.getSelectedFile().getPath();
KeysFileName = chooser.getSelectedFile().getName();
}
// read data file and copy it to original array
if (KeysFilePath != null)
{
try
{
int index = 0;
Scanner integerTextFile = new Scanner(new File(KeysFilePath));
while (integerTextFile.hasNext())
{
// read the next integer
KeysArray[index]= integerTextFile.nextInt();
index++;
}
// end of file detected
integerTextFile.close();
NumberOfKeys = index;
}
catch (IOException ioe)
{
System.exit(0);
}
}
else
NumberOfKeys = 0;
}
public void SequentialSearch()
{
SSAverageAccessTime = 0;
SSAverageCompSuc = 0;
SSAverageCompFailed = 0;
SSNumberKeysSuc = 0;
SSNumberKeysFailed = 0;
int SearchKey;
int TotalNumberOfComparisons;
long startTime = System.nanoTime();
boolean found = false;
for (int k=0; k<NumberOfKeys; k++)
{
found = false;
SearchKey = KeysArray[k];
TotalNumberOfComparisons = 0;
for (int d=0; d<NumberOfDataItems; d++)
{
TotalNumberOfComparisons++;
if (SearchKey == OriginalArray[d])
{
found = true;
}
if (found)break;
}
if(found)
{
SSAverageCompSuc = SSAverageCompSuc + TotalNumberOfComparisons;
SSNumberKeysSuc ++;
}
else
{
SSAverageCompFailed = SSAverageCompFailed + TotalNumberOfComparisons;
SSNumberKeysFailed ++;
}
}
long estimatedTime = System.nanoTime() - startTime;
if (NumberOfKeys != 0)
SSAverageAccessTime = Math.round((estimatedTime/NumberOfKeys));
else
SSAverageAccessTime = 0;
if(SSNumberKeysSuc != 0)
SSAverageCompSuc = Math.round (SSAverageCompSuc / SSNumberKeysSuc) ;
else
SSAverageCompSuc = 0;
if (SSNumberKeysFailed != 0)
SSAverageCompFailed = Math.round (SSAverageCompFailed / SSNumberKeysFailed) ;
else
SSNumberKeysFailed = 0;
return;
}
public void BinarySearch()
{
// makes a temporary array the length of the number of data items we gave it int[] tempArray = new int[NumberOfDataItems];
// copies a portion of the original array (origionalArray = new int[1000000] above) that has the data inputs
// we are copying only the portion of the original array the was given the data inputs when we opened the data item .txt
for (int i = 0; i < NumberOfDataItems; i ++)
{
tempArray[i]=OriginalArray[i];
}
// takes the temporary array and sorts it
java.util.Arrays.sort(tempArray);
BSAverageAccessTime = 0;
BSAverageCompSuc = 0;
BSAverageCompFailed = 0;
BSNumberKeysSuc = 0;
BSNumberKeysFailed = 0;
int SearchKey;
int TotalNumberOfComparisons;
long startTime = System.nanoTime();
boolean found = false;
int low = 0;
int high = tempArray.length-1;
// sets the midpoint
int middle = (low-high)/2;
for(int k = 0; k<NumberOfKeys; k++)
{
SearchKey = KeysArray[k];
TotalNumberOfComparisons = 0;
for (int d=0; d< Math.log(tempArray.length -1); d++)
{
TotalNumberOfComparisons++;
// makes sure low doesn't go out of bounds
if (low < 0)
{
low = 0;
}
// make sure high doesn't go out of bounds
if(high >= tempArray.length)
{
high = tempArray.length-1;
}
// checks the midpoint against the key we are using
if (SearchKey == tempArray[middle])
{
found = true;
}
// makes sure that if the midpoint doesn't equal the key we get false
else
if (low == high && SearchKey != tempArray[middle])
{
found = false;
}
// if the key is greater than the middle we check the upper portion
else
if (SearchKey > tempArray[middle])
{
low = (middle +1);
}
// if the key is less than the middle we check the lower portion
else
if (SearchKey < tempArray[middle])
high = (middle -1);
if (found)break;
}
if(found)
{
BSAverageCompSuc = BSAverageCompSuc + TotalNumberOfComparisons;
BSNumberKeysSuc ++;
}
else
{
BSAverageCompFailed = BSAverageCompFailed + TotalNumberOfComparisons;
BSNumberKeysFailed ++;
}
}
long estimatedTime = System.nanoTime() - startTime;
if (NumberOfKeys != 0)
BSAverageAccessTime = Math.round((estimatedTime/NumberOfKeys));
else
BSAverageAccessTime = 0;
if(BSNumberKeysSuc != 0)
BSAverageCompSuc = Math.round (BSAverageCompSuc / BSNumberKeysSuc) ;
else
BSAverageCompSuc = 0;
if (BSNumberKeysFailed != 0)
BSAverageCompFailed = Math.round (BSAverageCompFailed / BSNumberKeysFailed) ;
else
BSNumberKeysFailed = 0;
return;
}
public void HashedSearch()
{
HSAverageAccessTime = 0;
HSAverageCompSuc = 0;
HSAverageCompFailed = 0;
HSNumberKeysSuc = 0;
HSNumberKeysFailed = 0;
int SearchKey;
int TotalNumberOfComparisons;
long startTime = System.nanoTime();
boolean found = false;
}
public int FindPrime()
{
return 0;
}
public void Initialize()
{
}
public void BHSearch()
{
}
}
Here is the Java GUI Code:
// GUI-related imports
import java.awt.*;
import java.awt.event.*;
// File-related imports
import java.io.IOException;
public class Project04 extends Frame implements ActionListener
{
ArrayOperations arr = new ArrayOperations();
String command = "";
public static void main(String[] args)
{
Frame frame = new Project04();
frame.setResizable(false);
frame.setSize(900,620);
frame.setVisible(true);
}
public Project04()
{
setTitle("Search Routines");
// Create Menu Bar and menu items
MenuBar mb = new MenuBar();
setMenuBar(mb);
// Create Menu Group Labeled "File"
Menu FileMenu = new Menu("File");
// Add it to Menu Bar
mb.add(FileMenu);
// Create Menu Items
// Add action Listener
// Add to "File" Menu Group
MenuItem miOpen = new MenuItem("Open");
miOpen.addActionListener(this);
FileMenu.add(miOpen);
MenuItem miExit = new MenuItem("Exit");
miExit.addActionListener(this);
FileMenu.add(miExit);
// Create Menu Group Labeled "File"
Menu SearchMenu = new Menu("Search");
// Add it to Menu Bar
mb.add(SearchMenu);
// Create Menu Items
// Add action Listener
// Add to "Search" Menu Group
MenuItem miSequentialSearch = new MenuItem("Sequential Search");
miSequentialSearch.addActionListener(this);
SearchMenu.add(miSequentialSearch);
MenuItem miBinarySearch = new MenuItem("Binary Search");
miBinarySearch.addActionListener(this);
SearchMenu.add(miBinarySearch);
MenuItem miHashedSearch = new MenuItem("Hashed Search");
miHashedSearch.addActionListener(this);
SearchMenu.add(miHashedSearch);
WindowListener l = new WindowAdapter()
{
public void windowClosing(WindowEvent ev)
{
System.exit(0);
}
public void windowActivated(WindowEvent ev)
{
repaint();
}
public void windowStateChanged(WindowEvent ev)
{
repaint();
}
};
ComponentListener k = new ComponentAdapter()
{
public void componentResized(ComponentEvent e)
{
repaint();
}
};
// register listeners
this.addWindowListener(l);
this.addComponentListener(k);
}
//******************************************************************************
// called by windows manager whenever the application window performs an action
// (select a menu item, close, resize, ....
//******************************************************************************
public void actionPerformed (ActionEvent ev)
{
// figure out which command was issued
command = ev.getActionCommand();
// take action accordingly
if("Open".equals(command))
{
try
{
arr.ReadDataFile();
arr.ReadKeysFile();
}
catch (IOException ioe)
{
System.exit(0);
}
repaint();
}
else
if("Exit".equals(command))
{
System.exit(0);
}
else
if("Sequential Search".equals(command))
{
arr.SequentialSearch();
repaint();
}
if("Binary Search".equals(command))
{
arr.BinarySearch();
repaint();
}
if("Hashed Search".equals(command))
{
arr.HashedSearch();
repaint();
}
if("Bucket Hashed Search".equals(command))
{
arr.BHSearch();
repaint();
}
}
//********************************************************
// called by repaint() to redraw the screen
//********************************************************
public void paint(Graphics g)
{
if("Open".equals(command))
{
// Acknowledge that file was opened
if (arr.DataFileName != null)
{
g.drawString("File -- "+arr.DataFileName+" -- was successfully opened", 300, 200);
g.drawString("Number of Data Items = "+Integer.toString(arr.NumberOfDataItems), 330, 250);
}
else
{
g.drawString("NO Data File is Open", 300, 200);
}
if (arr.KeysFileName != null)
{
g.drawString("File -- "+arr.KeysFileName+" -- was successfully opened", 300, 300);
g.drawString("Number of Keys = "+Integer.toString(arr.NumberOfKeys), 330, 350);
}
else
{
g.drawString("NO Keys File is Open", 300, 300);
}
return;
}
if("Sequential Search".equals(command) || "Binary Search".equals(command)||
"Hashed Search".equals(command) || "Bucket Hashed Search".equals(command) )
{
g.drawRect(100, 100, 700, 420);
g.drawString("Experiment", 135, 200);
g.drawLine(250,100,250,520);
g.drawString("Data File Attributes -- "+Integer.toString(arr.NumberOfDataItems)+" Data Items", 450, 130);
g.drawString("Key File Attributes -- "+Integer.toString(arr.NumberOfKeys)+" Keys", 450, 155);
g.drawLine(250,175,800,175);
g.drawString("Measured Criteria", 475,190 );
g.drawLine(250,215,800,215);
g.drawString("Successful Search", 410, 230);
g.drawString("Unccessful Search", 640, 230);
g.drawLine(360,245,800,245);
g.drawLine(100,310,800,310);
g.drawString("Sequential Search", 110, 325);
g.drawLine(100,340,800,340);
g.drawString("Average", 285, 260);
g.drawString("Access", 285, 275);
g.drawString("Time", 290, 290);
g.drawLine(360,215,360,520);
g.drawString("#", 400, 260);
g.drawString("Keys", 400, 275);
g.drawString("Found", 390, 290);
g.drawLine(470,245,470,520);
g.drawString("Average", 500, 260);
g.drawString("No. Of", 500, 275);
g.drawString("Comparisons", 485, 290);
g.drawLine(580,215,580,520);
g.drawString("# ", 620, 260);
g.drawString("Keys", 620, 275);
g.drawString("Not Found", 600, 290);
g.drawLine(690,245,690,520);
g.drawString("Average", 720, 260);
g.drawString("No. Of", 720, 275);
g.drawString("Comparisons", 700, 290);
g.drawString(Long.toString(arr.SSAverageAccessTime), 255, 325);
g.drawString(Long.toString(arr.SSNumberKeysSuc), 365, 325);
g.drawString(Long.toString(arr.SSAverageCompSuc), 475, 325);
g.drawString(Long.toString(arr.SSNumberKeysFailed), 585, 325);
g.drawString(Long.toString(arr.SSAverageCompFailed), 695, 325);
g.drawString("Binary Search", 110, 355);
g.drawLine(100,370,800,370);
g.drawString(Long.toString(arr.BSAverageAccessTime), 255, 355);
g.drawString(Long.toString(arr.BSNumberKeysSuc), 365, 355);
g.drawString(Long.toString(arr.BSAverageCompSuc), 475, 355);
g.drawString(Long.toString(arr.BSNumberKeysFailed), 585, 355);
g.drawString(Long.toString(arr.BSAverageCompFailed), 695, 355);
g.drawString("Hashed Search", 110, 385);
g.drawLine(100,400,800,400);
g.drawString(Long.toString(arr.HSAverageAccessTime), 255, 385);
g.drawString(Long.toString(arr.HSNumberKeysSuc), 365, 385);
g.drawString(Long.toString(arr.HSAverageCompSuc), 475, 385);
g.drawString(Long.toString(arr.HSNumberKeysFailed), 585, 385);
g.drawString(Long.toString(arr.HSAverageCompFailed), 695, 385);
// add code to display results for other searches
}
}
}
You are doing a wrong calculation
change below:
int middle = (low-high)/2;
to:
int middle = (high-low)/2;
low is always less than high so you are getting a negative number and that negative index will not exist in your array.
Although I have not checked any other possible problems or the algorithm's validity, the above is the solution to the problem you are having

Checkbox Compilation Error

I am to stimulate a java applet of a menu with checkboxes and checkboxgroups, with calories listed next to each food choice. My problem is I cannot figure out why I keep getting this error message when i compile it:
AnAppletWithCheckboxes.java:188: illegal forward reference
int crepeVal = Integer.parseInt(textField.getText());
I get that for every line that I have is converting the textField into an int, so hence there are 17 errors. Any idea of why will be appreciated!
Here is my Code:: it is very long because there are plenty checkboxes and textFields. I commented Out My place of Error! :
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class AnAppletWithCheckboxes extends Applet implements ItemListener {
public void init() {
setLayout(new GridLayout(1, 0));
CheckboxGroup dinnerType = new CheckboxGroup();
standard = new Checkbox("standard", dinnerType, false);
standard.addItemListener(this);
deluxe = new Checkbox("deluxe", dinnerType, true);
deluxe.addItemListener(this);
CheckboxGroup appetizers = new CheckboxGroup();
crepe = new Checkbox("crepe", appetizers, false);
crepe.addItemListener(this);
quiche = new Checkbox("quiche", appetizers, false);
quiche.addItemListener(this);
dumpling = new Checkbox("dumpling", appetizers, false);
dumpling.addItemListener(this);
textField = new TextField("300");
textField.setEditable(false);
textField2 = new TextField("330");
textField2.setEditable(false);
textField3 = new TextField("98");
textField3.setEditable(false);
CheckboxGroup soupOrSalad = new CheckboxGroup();
soup = new Checkbox("Soup", soupOrSalad, false);
soup.addItemListener(this);
CheckboxGroup soups = new CheckboxGroup();
cream = new Checkbox("cream", soups, false);
cream.addItemListener(this);
broth = new Checkbox("broth", soups, false);
broth.addItemListener(this);
gumbo = new Checkbox("gumbo", soups, false);
gumbo.addItemListener(this);
textField4 = new TextField("190");
textField4.setEditable(false);
textField5 = new TextField("20");
textField5.setEditable(false);
textField6 = new TextField("110");
textField6.setEditable(false);
salad = new Checkbox("Salad", soupOrSalad, false);
salad.addItemListener(this);
CheckboxGroup salads = new CheckboxGroup();
tossed = new Checkbox("tossed", salads, false);
tossed.addItemListener(this);
caesar = new Checkbox("caesar", salads, false);
caesar.addItemListener(this);
croutons = new Checkbox("croutons");
croutons.addItemListener(this);
lite = new Checkbox("lite dressing");
lite.addItemListener(this);
textField7 = new TextField("35");
textField7.setEditable(false);
textField8 = new TextField("90");
textField8.setEditable(false);
textField9 = new TextField("60");
textField9.setEditable(false);
textField10 = new TextField("50");
textField10.setEditable(false);
CheckboxGroup entrees = new CheckboxGroup();
chicken = new Checkbox("chicken", entrees, false);
chicken.addItemListener(this);
beef = new Checkbox("beef", entrees, false);
beef.addItemListener(this);
lamb = new Checkbox("lamb", entrees, false);
lamb.addItemListener(this);
fish = new Checkbox("fish", entrees, false);
fish.addItemListener(this);
textField11 = new TextField("200");
textField11.setEditable(false);
textField12 = new TextField("170");
textField12.setEditable(false);
textField13 = new TextField("65");
textField13.setEditable(false);
textField14 = new TextField("150");
textField14.setEditable(false);
CheckboxGroup deserts = new CheckboxGroup();
pie = new Checkbox("pie", deserts, false);
pie.addItemListener(this);
fruit = new Checkbox("fruit", deserts, false);
fruit.addItemListener(this);
sherbet = new Checkbox("sherbet", deserts, false);
sherbet.addItemListener(this);
textField15 = new TextField("80");
textField15.setEditable(false);
textField16 = new TextField("60");
textField16.setEditable(false);
textField17 = new TextField("107");
textField17.setEditable(false);
calories = new Button("Calories");
calTextField = new TextField("0"+total);
calTextField.setEditable(false);
setLayout(new GridLayout(0, 1));
Panel p = new Panel();
add(p);
p.add(standard);
p.add(deluxe);
appetizerPanel = new Panel();
add(appetizerPanel);
label = new Label("Appetizer");
appetizerPanel.add(label);
appetizerPanel.add(crepe);
appetizerPanel.add(textField);
appetizerPanel.add(quiche);
appetizerPanel.add(textField2);
appetizerPanel.add(dumpling);
appetizerPanel.add(textField3);
soupPanel = new Panel();
add(soupPanel);
soupPanel.add(soup);
soupPanel.add(cream);
soupPanel.add(textField4);
soupPanel.add(broth);
soupPanel.add(textField5);
soupPanel.add(gumbo);
soupPanel.add(textField6);
saladPanel = new Panel();
add(saladPanel);
saladPanel.add(salad);
saladPanel.add(tossed);
saladPanel.add(textField7);
saladPanel.add(caesar);
saladPanel.add(textField8);
saladPanel.add(croutons);
saladPanel.add(textField9);
saladPanel.add(lite);
saladPanel.add(textField10);
entreePanel = new Panel();
add(entreePanel);
label2 = new Label("Entree");
entreePanel.add(label2);
entreePanel.add(chicken);
entreePanel.add(textField11);
entreePanel.add(beef);
entreePanel.add(textField12);
entreePanel.add(lamb);
entreePanel.add(textField13);
entreePanel.add(fish);
entreePanel.add(textField14);
desertPanel = new Panel();
add(desertPanel);
label3 = new Label("Desert");
desertPanel.add(label3);
desertPanel.add(pie);
desertPanel.add(textField15);
desertPanel.add(fruit);
desertPanel.add(textField16);
desertPanel.add(sherbet);
desertPanel.add(textField17);
caloriesPanel = new Panel();
add(caloriesPanel);
caloriesPanel.add(calories);
caloriesPanel.add(calTextField);
}
public void LabelChange(Label b) {
if (b ==label3)
b.setForeground(Color.lightGray);
else
label3.setForeground(Color.black);
}
/* This is where i get those errors!! */
int crepeVal = Integer.parseInt(textField.getText());
int quicheVal = Integer.parseInt(textField2.getText());
int dumplingVal = Integer.parseInt(textField3.getText());
int creamVal = Integer.parseInt(textField4.getText());
int brothVal = Integer.parseInt(textField5.getText());
int gumboVal = Integer.parseInt(textField6.getText());
int tossedVal = Integer.parseInt(textField7.getText());
int caesarVal = Integer.parseInt(textField8.getText());
int croutonsVal = Integer.parseInt(textField9.getText());
int liteVal = Integer.parseInt(textField10.getText());
int chickenVal = Integer.parseInt(textField11.getText());
int beefVal = Integer.parseInt(textField12.getText());
int lambVal = Integer.parseInt(textField13.getText());
int fishVal = Integer.parseInt(textField14.getText());
int pieVal = Integer.parseInt(textField15.getText());
int fruitVal = Integer.parseInt(textField16.getText());
int sherbetVal = Integer.parseInt(textField17.getText());
public boolean action(Event evt, Object whatAction){
if(!(evt.target instanceof Button)){
return false;
}
else {
calorieCount();
return true;
}
}
public void calorieCount () {
if (crepe.getState())
crepeVal += total;
else
crepeVal = 0;
if (quiche.getState())
quicheVal += total;
else
quicheVal = 0;
if (dumpling.getState())
dumplingVal += total;
else
dumplingVal= 0;
if (cream.getState())
creamVal += total;
else
creamVal = 0;
if (broth.getState())
brothVal += total;
else
brothVal = 0;
if (gumbo.getState())
gumboVal += total;
else
gumboVal = 0;
if (tossed.getState())
tossedVal += total;
else
tossedVal = 0;
if (caesar.getState())
caesarVal += total;
else
caesarVal = 0;
if (croutons.getState())
croutonsVal += total;
else
croutonsVal = 0;
if (lite.getState())
liteVal += total;
else
liteVal = 0;
if (chicken.getState())
chickenVal += total;
else
chickenVal = 0;
if (beef.getState())
beefVal += total;
else
beefVal = 0;
if (lamb.getState())
lambVal += total;
else
lambVal = 0;
if (fish.getState())
fishVal += total;
else
fishVal = 0;
if (pie.getState())
pieVal += total;
else
pieVal = 0;
if (fruit.getState())
fruitVal += total;
else
fruitVal = 0;
if (sherbet.getState())
sherbetVal += total;
else
sherbetVal = 0;
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == standard || e.getSource() == deluxe)
handleDinnerType((Checkbox)e.getSource());
else if (e.getSource() == soup || e.getSource() == salad)
handleSoupSaladChoice((Checkbox)e.getSource());
}
void handleDinnerType(Checkbox selectedType) {
boolean enabled;
if (selectedType == standard){
enabled = false;
LabelChange(label3);
}
else{
enabled = true;
LabelChange(label);
}
soup.setEnabled(enabled);
salad.setEnabled(enabled);
pie.setEnabled(enabled);
fruit.setEnabled(enabled);
sherbet.setEnabled(enabled);
cream.setEnabled(enabled);
broth.setEnabled(enabled);
gumbo.setEnabled(enabled);
tossed.setEnabled(enabled);
caesar.setEnabled(enabled);
croutons.setEnabled(enabled);
lite.setEnabled(enabled);
}
void handleSoupSaladChoice(Checkbox selectedCourse) {
boolean soupEnabled, saladEnabled;
if (selectedCourse == soup) {
soupEnabled = true;
saladEnabled = false;
soup.setForeground(Color.BLACK);
salad.setForeground(Color.lightGray);
}
else {
soupEnabled = false;
saladEnabled = true;
soup.setForeground(Color.lightGray);
salad.setForeground(Color.BLACK);
}
cream.setEnabled(soupEnabled);
broth.setEnabled(soupEnabled);
gumbo.setEnabled(soupEnabled);
tossed.setEnabled(saladEnabled);
caesar.setEnabled(saladEnabled);
croutons.setEnabled(saladEnabled);
lite.setEnabled(saladEnabled);
}
Label
label, label2, label3;
Panel
appetizerPanel, soupPanel, saladPanel, entreePanel, desertPanel, caloriesPanel;
Checkbox
standard, deluxe,
soup, salad,
crepe, quiche, dumpling,
cream, broth, gumbo,
tossed, caesar,
croutons, lite,
chicken, beef, lamb, fish,
pie, fruit, sherbet;
Button calories;
int total = 0;
TextField
textField, textField2, textField3,
textField4, textField5, textField6,
textField7, textField8, textField9,
textField10, textField11, textField12,
textField13, textField14, textField15,
textField16, textField17, calTextField;
}
In Java, you are not allowed to refer to a field a in an initializer of another field b if b is declared before a in the source code. That is, this is allowed:
int b = 3;
int a = b;
but this is not allowed:
int a = b;
int b = 3;
By the way, if Java had some magic way of figuring out that textField had to be initialized before crepeVal, you would still get a NullPointerException since textField is null after object construction.

GRADING SYSTEM using JComboBox for the entry of grades; Jlabel, JTextFirld,JFrame; JButton for events; all in JFrame

here is my code for our finals. a grading system were the grades are all in 24 JComboBox. some with numbers till 10, 20 and 30. each quarter they are computed and shown in a JTextfield. when the four quarters are complete a Jbutton "COMPUTE ALL" calculates the entire data and will show in another frame the student's grade and weather he or she have passed or failed. this code works good with some problems: (1) my Jbutton "CLEAR ALL" doesn't work. it should show the default number "0" when clicked and the Jtextfields must be empty-(THIS ALREADY WORKS "TEXTFIELD TO EMPTY". (2) i want to hide the second frame when the Jbutton for compute all is clicked (f2.hide();f3.show();) but it keeps saying (cannot find symbol
symbol: variable f2cannot find symbol) (3) and lastly in my 3rd frame where the student's final grade and final rating are shown i cant really have it shown in a message dialog stating weather he or she passed or failed. THANKS. sorry for the poor coding.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class forfinals extends JFrame{
JFrame f1 = new JFrame ("HELLO");
JLabel Lb1 = new JLabel ("Enter your name");
JLabel Lb2 = new JLabel ("QUIZZES (20)");
JLabel Lb3 = new JLabel ("ASSIGNMENTS (10)");
JLabel Lb4 = new JLabel ("PROJECTS (20)");
JLabel Lb5 = new JLabel ("PARTICIPATION (10)");
JLabel Lb6 = new JLabel ("ATTENDANCE(10)");
JLabel Lb7 = new JLabel ("MAJOR EXAM (30)");
JLabel Lb8 = new JLabel ("Western College SY: 2011-2012");
JLabel Lb9 = new JLabel ("Final Grade");
JLabel Lb10 = new JLabel ("PRELIM");
JLabel Lb11 = new JLabel ("MIDTERM");
JLabel Lb12 = new JLabel ("PREFINAL");
JLabel Lb13 = new JLabel ("FINALS");
JLabel label1 = new JLabel ("YOUR FINAL GRADE");
JLabel label2 = new JLabel ("YOU EARNED THE RATING");
JTextField txt1 = new JTextField (15);
JTextField txt2 = new JTextField (2);
JTextField txt3 = new JTextField (2);
JTextField txt4 = new JTextField (2);
JTextField txt5 = new JTextField (2);
JTextField finalg = new JTextField (5);
JTextField finalr = new JTextField (5);
JButton Btn1 = new JButton ("OK");
JButton Btn2 = new JButton ("CANCEL");
JButton Btn3 = new JButton ("COMPUTE");
JButton Btn4 = new JButton ("COMPUTE");
JButton Btn5 = new JButton ("COMPUTE");
JButton Btn6 = new JButton ("COMPUTE");
JButton jcompute = new JButton ("COMPUTE ALL");
JButton jclear = new JButton ("CLEAR ALL");
JButton jexit = new JButton ("EXIT");
JButton finalb = new JButton ("OK");
//prelim
JComboBox cb1 = new JComboBox();
JComboBox cb2 = new JComboBox();
JComboBox cb3 = new JComboBox();
JComboBox cb4 = new JComboBox();
JComboBox cb5 = new JComboBox();
JComboBox cb6 = new JComboBox();
//midterm
JComboBox cb7 = new JComboBox();
JComboBox cb8 = new JComboBox();
JComboBox cb9 = new JComboBox();
JComboBox cb10 = new JComboBox();
JComboBox cb11 = new JComboBox();
JComboBox cb12 = new JComboBox();
//prefinal
JComboBox cb13 = new JComboBox();
JComboBox cb14 = new JComboBox();
JComboBox cb15 = new JComboBox();
JComboBox cb16 = new JComboBox();
JComboBox cb17 = new JComboBox();
JComboBox cb18 = new JComboBox();
//finals
JComboBox cb19 = new JComboBox();
JComboBox cb20 = new JComboBox();
JComboBox cb21 = new JComboBox();
JComboBox cb22 = new JComboBox();
JComboBox cb23 = new JComboBox();
JComboBox cb24 = new JComboBox();
public forfinals(){
f1.getContentPane().setLayout(null);
f1.setSize (300,350);
f1.getContentPane().add(Lb1);
f1.getContentPane().add(txt1);
f1.getContentPane().add(Btn1);
f1.getContentPane().add(Btn2);
Lb1.setBounds(40,70,100,75);
txt1.setBounds(150,90,100,30);
Btn1.setBounds(40,170,100,40);
Btn2.setBounds(150,170,100,40);
Btn1.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
String x;
x = txt1.getText();
JFrame f2 = new JFrame (x);
f2.getContentPane().setLayout(null);
f2.setSize (830,600);
f1.hide();
f2.show();
f2.getContentPane().add(Lb2);
f2.getContentPane().add(Lb3);
f2.getContentPane().add(Lb4);
f2.getContentPane().add(Lb5);
f2.getContentPane().add(Lb6);
f2.getContentPane().add(Lb7);
f2.getContentPane().add(Lb8);
f2.getContentPane().add(Lb9);
f2.getContentPane().add(Lb10);
f2.getContentPane().add(Lb11);
f2.getContentPane().add(Lb12);
f2.getContentPane().add(Lb13);
f2.getContentPane().add(jcompute);
f2.getContentPane().add(jclear);
f2.getContentPane().add(jexit);
Lb2.setBounds(30,120,90,70);
Lb3.setBounds(30,170,110,70);
Lb4.setBounds(30,220,90,70);
Lb5.setBounds(30,270,120,70);
Lb6.setBounds(30,320,100,70);
Lb7.setBounds(30,370,110,70);
Lb8.setBounds(280,20,230,20);
Lb9.setBounds(30,420,80,70);
Lb10.setBounds(190,60,100,100);
Lb11.setBounds(315,60,100,100);
Lb12.setBounds(440,60,100,100);
Lb13.setBounds(570,60,100,100);
jcompute.setBounds(660,160,120, 60);
jclear.setBounds(660,260,120, 60);
jexit.setBounds(660,360,120,60);
//PRELIM
f2.getContentPane().add(cb1);
f2.getContentPane().add(cb2);
f2.getContentPane().add(cb3);
f2.getContentPane().add(cb4);
f2.getContentPane().add(cb5);
f2.getContentPane().add(cb6);
f2.getContentPane().add(Btn3);
f2.getContentPane().add(txt2);
txt2.setEditable(false);
cb1.setBounds(190,140,50,30);
cb2.setBounds(190,190,50,30);
cb3.setBounds(190,240,50,30);
cb4.setBounds(190,290,50,30);
cb5.setBounds(190,340,50,30);
cb6.setBounds(190,390,50,30);
Btn3.setBounds(170,490,95,40);
txt2.setBounds(190,440,55,35);
int numbers_to_add_max = 10;
for (int i = 0; i <= numbers_to_add_max; i++) {
cb2.addItem(new Integer(i));
cb4.addItem(new Integer(i));
cb5.addItem(new Integer(i));
}
int numbers = 20;
for (int i = 0; i <= numbers; i++) {
cb1.addItem(new Integer(i));
cb3.addItem(new Integer(i));
}
int numbers_to_add = 30;
for (int i = 0; i <= numbers_to_add; i++) {
cb6.addItem(new Integer(i));
}
//MIDTERM
f2.getContentPane().add(cb7);
f2.getContentPane().add(cb8);
f2.getContentPane().add(cb9);
f2.getContentPane().add(cb10);
f2.getContentPane().add(cb11);
f2.getContentPane().add(cb12);
f2.getContentPane().add(Btn4);
f2.getContentPane().add(txt3);
txt3.setEditable(false);
cb7.setBounds(315,140,50,30);
cb8.setBounds(315,190,50,30);
cb9.setBounds(315,240,50,30);
cb10.setBounds(315,290,50,30);
cb11.setBounds(315,340,50,30);
cb12.setBounds(315,390,50,30);
Btn4.setBounds(295,490,95,40);
txt3.setBounds(315,440,55,35);
int nu = 10;
for (int i = 0; i <= nu; i++) {
cb8.addItem(new Integer(i));
cb10.addItem(new Integer(i));
cb11.addItem(new Integer(i));
}
int num = 20;
for (int i = 0; i <= num; i++) {
cb7.addItem(new Integer(i));
cb9.addItem(new Integer(i));
}
int numb = 30;
for (int i = 0; i <= numb; i++) {
cb12.addItem(new Integer(i));
}
//PREFINAL
f2.getContentPane().add(cb13);
f2.getContentPane().add(cb14);
f2.getContentPane().add(cb15);
f2.getContentPane().add(cb16);
f2.getContentPane().add(cb17);
f2.getContentPane().add(cb18);
f2.getContentPane().add(Btn5);
f2.getContentPane().add(txt4);
txt4.setEditable(false);
cb13.setBounds(440,140,50,30);
cb14.setBounds(440,190,50,30);
cb15.setBounds(440,240,50,30);
cb16.setBounds(440,290,50,30);
cb17.setBounds(440,340,50,30);
cb18.setBounds(440,390,50,30);
Btn5.setBounds(420,490,95,40);
txt4.setBounds(440,440,55,35);
int toaddmax = 10;
for (int i = 0; i <= toaddmax; i++) {
cb14.addItem(new Integer(i));
cb16.addItem(new Integer(i));
cb17.addItem(new Integer(i));
}
int numbersadd = 20;
for (int i = 0; i <= numbersadd; i++) {
cb13.addItem(new Integer(i));
cb15.addItem(new Integer(i));
}
int numbers_toadd = 30;
for (int i = 0; i <= numbers_toadd; i++) {
cb18.addItem(new Integer(i));
}
//FINALS
f2.getContentPane().add(cb19);
f2.getContentPane().add(cb20);
f2.getContentPane().add(cb21);
f2.getContentPane().add(cb22);
f2.getContentPane().add(cb23);
f2.getContentPane().add(cb24);
f2.getContentPane().add(Btn6);
f2.getContentPane().add(txt5);
txt5.setEditable(false);
cb19.setBounds(565,140,50,30);
cb20.setBounds(565,190,50,30);
cb21.setBounds(565,240,50,30);
cb22.setBounds(565,290,50,30);
cb23.setBounds(565,340,50,30);
cb24.setBounds(565,390,50,30);
Btn6.setBounds(545,490,95,40);
txt5.setBounds(565,440,55,35);
int add_max = 10;
for (int i = 0; i <= add_max; i++) {
cb20.addItem(new Integer(i));
cb22.addItem(new Integer(i));
cb23.addItem(new Integer(i));
}
int number = 20;
for (int i = 0; i <= number; i++) {
cb19.addItem(new Integer(i));
cb21.addItem(new Integer(i));
}
int to_add = 30;
for (int i = 0; i <= to_add; i++) {
cb24.addItem(new Integer(i));
}
}
});
Btn2.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
System.exit(0);
}
});
Btn3.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb1Int = Integer.parseInt(cb1.getSelectedItem().toString());
int cb2Int = Integer.parseInt(cb2.getSelectedItem().toString());
int cb3Int = Integer.parseInt(cb3.getSelectedItem().toString());
int cb4Int = Integer.parseInt(cb4.getSelectedItem().toString());
int cb5Int = Integer.parseInt(cb5.getSelectedItem().toString());
int cb6Int = Integer.parseInt(cb6.getSelectedItem().toString());
txt2.setText(String.valueOf(cb1Int + cb2Int + cb3Int + cb4Int + cb5Int + cb6Int));
}
});
Btn4.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb7Int = Integer.parseInt(cb7.getSelectedItem().toString());
int cb8Int = Integer.parseInt(cb8.getSelectedItem().toString());
int cb9Int = Integer.parseInt(cb9.getSelectedItem().toString());
int cb10Int = Integer.parseInt(cb10.getSelectedItem().toString());
int cb11Int = Integer.parseInt(cb11.getSelectedItem().toString());
int cb12Int = Integer.parseInt(cb12.getSelectedItem().toString());
txt3.setText(String.valueOf(cb7Int + cb8Int + cb9Int + cb10Int + cb11Int + cb12Int));
}
});
Btn5.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb13Int = Integer.parseInt(cb13.getSelectedItem().toString());
int cb14Int = Integer.parseInt(cb14.getSelectedItem().toString());
int cb15Int = Integer.parseInt(cb15.getSelectedItem().toString());
int cb16Int = Integer.parseInt(cb16.getSelectedItem().toString());
int cb17Int = Integer.parseInt(cb17.getSelectedItem().toString());
int cb18Int = Integer.parseInt(cb18.getSelectedItem().toString());
txt4.setText(String.valueOf(cb13Int + cb14Int + cb15Int + cb16Int + cb17Int + cb18Int));
}
});
Btn6.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb19Int = Integer.parseInt(cb19.getSelectedItem().toString());
int cb20Int = Integer.parseInt(cb20.getSelectedItem().toString());
int cb21Int = Integer.parseInt(cb21.getSelectedItem().toString());
int cb22Int = Integer.parseInt(cb22.getSelectedItem().toString());
int cb23Int = Integer.parseInt(cb23.getSelectedItem().toString());
int cb24Int = Integer.parseInt(cb24.getSelectedItem().toString());
txt5.setText(String.valueOf(cb19Int + cb20Int + cb21Int + cb22Int + cb23Int + cb24Int));
}
});
jcompute.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
String prelim, midterm, prefinal, finals, total;
double a, b, c, d, tg;
prelim = txt2.getText();
midterm = txt3.getText();
prefinal = txt4.getText();
finals = txt5.getText();
a = Double.parseDouble(prelim);
b = Double.parseDouble(midterm);
c = Double.parseDouble(prefinal);
d = Double.parseDouble(finals);
tg = (a + b + c + d)/4;
total = Double.toString(tg);
finalg.setText(total);
JFrame f3 = new JFrame ("STUDENT FINAL RATING");
f3.getContentPane().setLayout(null);
f3.setSize (350,300);
//the frame2 (f2); the two frames are still visible
f2.hide();
f3.show();
f3.getContentPane().add(label1);
f3.getContentPane().add(label2);
f3.getContentPane().add(finalg);
finalg.setEditable(false);
f3.getContentPane().add(finalr);
finalr.setEditable(false);
f3.getContentPane().add(finalb);
label1.setBounds(70,20,150,70);
label2.setBounds(90,70,200,70);
finalg.setBounds(205,40,50,30);
finalr.setBounds(140,140,50,40);
finalb.setBounds(130,200,70,30);
//EQUIVALENT
double grade, equiv;
grade = Double.parseDouble(finalg.getText());
if(grade>=99.50 && grade<101)
equiv = 1.00;
else if(grade<99.50 && grade>=98.50)
equiv = 1.10;
else if(grade<98.50 && grade>=97.50)
equiv = 1.20;
else if(grade<97.50 && grade>=96.50)
equiv = 1.30;
else if(grade<96.50 && grade>=95.50)
equiv = 1.40;
else if(grade<95.50 && grade>=94.50)
equiv = 1.50;
else if(grade<94.50 && grade>=93.50)
equiv = 1.60;
else if(grade<93.50 && grade>=92.50)
equiv = 1.70;
else if(grade<92.50 && grade>=91.50)
equiv = 1.80;
else if(grade<91.50 && grade>=90.50)
equiv = 1.90;
else if(grade<90.50 && grade>=89.50)
equiv = 2.00;
else if(grade<89.50 && grade>=88.50)
equiv = 2.10;
else if(grade<88.50 && grade>=87.50)
equiv = 2.20;
else if(grade<87.50 && grade>=86.50)
equiv = 2.30;
else if(grade<86.50 && grade>=85.50)
equiv = 2.40;
else if(grade<85.50 && grade>=84.50)
equiv = 2.50;
else if(grade<84.50 && grade>=83.50)
equiv = 2.60;
else if(grade<83.50 && grade>=82.50)
equiv = 2.70;
else if(grade<82.50 && grade>=81.50)
equiv = 2.80;
else if(grade<81.50 && grade>=80.50)
equiv = 2.90;
else if(grade<80.50 && grade>=79.50)
equiv = 3.00;
else if(grade<79.50 && grade>=78.50)
equiv = 3.10;
else if(grade<78.50 && grade>=77.50)
equiv = 3.20;
else if(grade<77.50 && grade>=76.50)
equiv = 3.30;
else if(grade<76.50 && grade>=75.50)
equiv = 3.40;
else if(grade<75.50 && grade>=74.50)
equiv = 3.50;
else
equiv = 5.0;
finalr.setText("" + equiv);
finalr.setEditable(false);
}
});
finalb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "" + equiv);
if (equi >=1.00 && equiv <=3.0) JOptionPane.showMessageDialog(null, " YOU PASSED!");
' else if (equiv >=3.10 && equiv <=75) JOptionPane.showMessageDialog(null, " YOU FAILED!");
}
});
jclear.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
//this part doesn't work. the JComboBox still contains the initialized number even though the "CLEAR ALL" button is clicked
cb1.setSelectedItem("0");
cb2.setSelectedItem("0");
cb3.setSelectedItem("0");
cb4.setSelectedItem("0");
cb5.setSelectedItem("0");
cb6.setSelectedItem("0");
cb7.setSelectedItem("0");
cb8.setSelectedItem("0");
cb9.setSelectedItem("0");
cb10.setSelectedItem("0");
cb11.setSelectedItem("0");
cb12.setSelectedItem("0");
cb13.setSelectedItem("0");
cb14.setSelectedItem("0");
cb15.setSelectedItem("0");
cb16.setSelectedItem("0");
cb17.setSelectedItem("0");
cb18.setSelectedItem("0");
cb19.setSelectedItem("0");
cb20.setSelectedItem("0");
cb21.setSelectedItem("0");
cb22.setSelectedItem("0");
cb23.setSelectedItem("0");
cb24.setSelectedItem("0");
txt2.setText(" ");
txt3.setText(" ");
txt4.setText(" ");
txt5.setText(" ");
}
});
jexit.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
System.exit(0);
}
});
f1.show();
}
public static void main (String args []){
forfinals xx = new forfinals();
}
}
Here is the complete corrected code,
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class forfinals extends JFrame{
JFrame f1 = new JFrame ("HELLO");
JFrame f2 = new JFrame();
double grade, equiv;
JLabel Lb1 = new JLabel ("Enter your name");
JLabel Lb2 = new JLabel ("QUIZZES (20)");
JLabel Lb3 = new JLabel ("ASSIGNMENTS (10)");
JLabel Lb4 = new JLabel ("PROJECTS (20)");
JLabel Lb5 = new JLabel ("PARTICIPATION (10)");
JLabel Lb6 = new JLabel ("ATTENDANCE(10)");
JLabel Lb7 = new JLabel ("MAJOR EXAM (30)");
JLabel Lb8 = new JLabel ("Western College SY: 2011-2012");
JLabel Lb9 = new JLabel ("Final Grade");
JLabel Lb10 = new JLabel ("PRELIM");
JLabel Lb11 = new JLabel ("MIDTERM");
JLabel Lb12 = new JLabel ("PREFINAL");
JLabel Lb13 = new JLabel ("FINALS");
JLabel label1 = new JLabel ("YOUR FINAL GRADE");
JLabel label2 = new JLabel ("YOU EARNED THE RATING");
JTextField txt1 = new JTextField (15);
JTextField txt2 = new JTextField (2);
JTextField txt3 = new JTextField (2);
JTextField txt4 = new JTextField (2);
JTextField txt5 = new JTextField (2);
JTextField finalg = new JTextField (5);
JTextField finalr = new JTextField (5);
JButton Btn1 = new JButton ("OK");
JButton Btn2 = new JButton ("CANCEL");
JButton Btn3 = new JButton ("COMPUTE");
JButton Btn4 = new JButton ("COMPUTE");
JButton Btn5 = new JButton ("COMPUTE");
JButton Btn6 = new JButton ("COMPUTE");
JButton jcompute = new JButton ("COMPUTE ALL");
JButton jclear = new JButton ("CLEAR ALL");
JButton jexit = new JButton ("EXIT");
JButton finalb = new JButton ("OK");
//prelim
JComboBox cb1 = new JComboBox();
JComboBox cb2 = new JComboBox();
JComboBox cb3 = new JComboBox();
JComboBox cb4 = new JComboBox();
JComboBox cb5 = new JComboBox();
JComboBox cb6 = new JComboBox();
//midterm
JComboBox cb7 = new JComboBox();
JComboBox cb8 = new JComboBox();
JComboBox cb9 = new JComboBox();
JComboBox cb10 = new JComboBox();
JComboBox cb11 = new JComboBox();
JComboBox cb12 = new JComboBox();
//prefinal
JComboBox cb13 = new JComboBox();
JComboBox cb14 = new JComboBox();
JComboBox cb15 = new JComboBox();
JComboBox cb16 = new JComboBox();
JComboBox cb17 = new JComboBox();
JComboBox cb18 = new JComboBox();
//finals
JComboBox cb19 = new JComboBox();
JComboBox cb20 = new JComboBox();
JComboBox cb21 = new JComboBox();
JComboBox cb22 = new JComboBox();
JComboBox cb23 = new JComboBox();
JComboBox cb24 = new JComboBox();
public forfinals(){
f1.getContentPane().setLayout(null);
f1.setSize (300,350);
f1.getContentPane().add(Lb1);
f1.getContentPane().add(txt1);
f1.getContentPane().add(Btn1);
f1.getContentPane().add(Btn2);
Lb1.setBounds(40,70,100,75);
txt1.setBounds(150,90,100,30);
Btn1.setBounds(40,170,100,40);
Btn2.setBounds(150,170,100,40);
Btn1.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
String x;
x = txt1.getText();
f2.setTitle(x);
f2.getContentPane().setLayout(null);
f2.setSize (830,600);
f1.hide();
f2.show();
f2.getContentPane().add(Lb2);
f2.getContentPane().add(Lb3);
f2.getContentPane().add(Lb4);
f2.getContentPane().add(Lb5);
f2.getContentPane().add(Lb6);
f2.getContentPane().add(Lb7);
f2.getContentPane().add(Lb8);
f2.getContentPane().add(Lb9);
f2.getContentPane().add(Lb10);
f2.getContentPane().add(Lb11);
f2.getContentPane().add(Lb12);
f2.getContentPane().add(Lb13);
f2.getContentPane().add(jcompute);
f2.getContentPane().add(jclear);
f2.getContentPane().add(jexit);
Lb2.setBounds(30,120,90,70);
Lb3.setBounds(30,170,110,70);
Lb4.setBounds(30,220,90,70);
Lb5.setBounds(30,270,120,70);
Lb6.setBounds(30,320,100,70);
Lb7.setBounds(30,370,110,70);
Lb8.setBounds(280,20,230,20);
Lb9.setBounds(30,420,80,70);
Lb10.setBounds(190,60,100,100);
Lb11.setBounds(315,60,100,100);
Lb12.setBounds(440,60,100,100);
Lb13.setBounds(570,60,100,100);
jcompute.setBounds(660,160,120, 60);
jclear.setBounds(660,260,120, 60);
jexit.setBounds(660,360,120,60);
//PRELIM
f2.getContentPane().add(cb1);
f2.getContentPane().add(cb2);
f2.getContentPane().add(cb3);
f2.getContentPane().add(cb4);
f2.getContentPane().add(cb5);
f2.getContentPane().add(cb6);
f2.getContentPane().add(Btn3);
f2.getContentPane().add(txt2);
txt2.setEditable(false);
cb1.setBounds(190,140,50,30);
cb2.setBounds(190,190,50,30);
cb3.setBounds(190,240,50,30);
cb4.setBounds(190,290,50,30);
cb5.setBounds(190,340,50,30);
cb6.setBounds(190,390,50,30);
Btn3.setBounds(170,490,95,40);
txt2.setBounds(190,440,55,35);
int numbers_to_add_max = 10;
for (int i = 0; i <= numbers_to_add_max; i++) {
cb2.addItem(new Integer(i));
cb4.addItem(new Integer(i));
cb5.addItem(new Integer(i));
}
int numbers = 20;
for (int i = 0; i <= numbers; i++) {
cb1.addItem(new Integer(i));
cb3.addItem(new Integer(i));
}
int numbers_to_add = 30;
for (int i = 0; i <= numbers_to_add; i++) {
cb6.addItem(new Integer(i));
}
//MIDTERM
f2.getContentPane().add(cb7);
f2.getContentPane().add(cb8);
f2.getContentPane().add(cb9);
f2.getContentPane().add(cb10);
f2.getContentPane().add(cb11);
f2.getContentPane().add(cb12);
f2.getContentPane().add(Btn4);
f2.getContentPane().add(txt3);
txt3.setEditable(false);
cb7.setBounds(315,140,50,30);
cb8.setBounds(315,190,50,30);
cb9.setBounds(315,240,50,30);
cb10.setBounds(315,290,50,30);
cb11.setBounds(315,340,50,30);
cb12.setBounds(315,390,50,30);
Btn4.setBounds(295,490,95,40);
txt3.setBounds(315,440,55,35);
int nu = 10;
for (int i = 0; i <= nu; i++) {
cb8.addItem(new Integer(i));
cb10.addItem(new Integer(i));
cb11.addItem(new Integer(i));
}
int num = 20;
for (int i = 0; i <= num; i++) {
cb7.addItem(new Integer(i));
cb9.addItem(new Integer(i));
}
int numb = 30;
for (int i = 0; i <= numb; i++) {
cb12.addItem(new Integer(i));
}
//PREFINAL
f2.getContentPane().add(cb13);
f2.getContentPane().add(cb14);
f2.getContentPane().add(cb15);
f2.getContentPane().add(cb16);
f2.getContentPane().add(cb17);
f2.getContentPane().add(cb18);
f2.getContentPane().add(Btn5);
f2.getContentPane().add(txt4);
txt4.setEditable(false);
cb13.setBounds(440,140,50,30);
cb14.setBounds(440,190,50,30);
cb15.setBounds(440,240,50,30);
cb16.setBounds(440,290,50,30);
cb17.setBounds(440,340,50,30);
cb18.setBounds(440,390,50,30);
Btn5.setBounds(420,490,95,40);
txt4.setBounds(440,440,55,35);
int toaddmax = 10;
for (int i = 0; i <= toaddmax; i++) {
cb14.addItem(new Integer(i));
cb16.addItem(new Integer(i));
cb17.addItem(new Integer(i));
}
int numbersadd = 20;
for (int i = 0; i <= numbersadd; i++) {
cb13.addItem(new Integer(i));
cb15.addItem(new Integer(i));
}
int numbers_toadd = 30;
for (int i = 0; i <= numbers_toadd; i++) {
cb18.addItem(new Integer(i));
}
//FINALS
f2.getContentPane().add(cb19);
f2.getContentPane().add(cb20);
f2.getContentPane().add(cb21);
f2.getContentPane().add(cb22);
f2.getContentPane().add(cb23);
f2.getContentPane().add(cb24);
f2.getContentPane().add(Btn6);
f2.getContentPane().add(txt5);
txt5.setEditable(false);
cb19.setBounds(565,140,50,30);
cb20.setBounds(565,190,50,30);
cb21.setBounds(565,240,50,30);
cb22.setBounds(565,290,50,30);
cb23.setBounds(565,340,50,30);
cb24.setBounds(565,390,50,30);
Btn6.setBounds(545,490,95,40);
txt5.setBounds(565,440,55,35);
int add_max = 10;
for (int i = 0; i <= add_max; i++) {
cb20.addItem(new Integer(i));
cb22.addItem(new Integer(i));
cb23.addItem(new Integer(i));
}
int number = 20;
for (int i = 0; i <= number; i++) {
cb19.addItem(new Integer(i));
cb21.addItem(new Integer(i));
}
int to_add = 30;
for (int i = 0; i <= to_add; i++) {
cb24.addItem(new Integer(i));
}
}
});
Btn2.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
System.exit(0);
}
});
Btn3.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb1Int = Integer.parseInt(cb1.getSelectedItem().toString());
int cb2Int = Integer.parseInt(cb2.getSelectedItem().toString());
int cb3Int = Integer.parseInt(cb3.getSelectedItem().toString());
int cb4Int = Integer.parseInt(cb4.getSelectedItem().toString());
int cb5Int = Integer.parseInt(cb5.getSelectedItem().toString());
int cb6Int = Integer.parseInt(cb6.getSelectedItem().toString());
txt2.setText(String.valueOf(cb1Int + cb2Int + cb3Int + cb4Int + cb5Int + cb6Int));
}
});
Btn4.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb7Int = Integer.parseInt(cb7.getSelectedItem().toString());
int cb8Int = Integer.parseInt(cb8.getSelectedItem().toString());
int cb9Int = Integer.parseInt(cb9.getSelectedItem().toString());
int cb10Int = Integer.parseInt(cb10.getSelectedItem().toString());
int cb11Int = Integer.parseInt(cb11.getSelectedItem().toString());
int cb12Int = Integer.parseInt(cb12.getSelectedItem().toString());
txt3.setText(String.valueOf(cb7Int + cb8Int + cb9Int + cb10Int + cb11Int + cb12Int));
}
});
Btn5.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb13Int = Integer.parseInt(cb13.getSelectedItem().toString());
int cb14Int = Integer.parseInt(cb14.getSelectedItem().toString());
int cb15Int = Integer.parseInt(cb15.getSelectedItem().toString());
int cb16Int = Integer.parseInt(cb16.getSelectedItem().toString());
int cb17Int = Integer.parseInt(cb17.getSelectedItem().toString());
int cb18Int = Integer.parseInt(cb18.getSelectedItem().toString());
txt4.setText(String.valueOf(cb13Int + cb14Int + cb15Int + cb16Int + cb17Int + cb18Int));
}
});
Btn6.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb19Int = Integer.parseInt(cb19.getSelectedItem().toString());
int cb20Int = Integer.parseInt(cb20.getSelectedItem().toString());
int cb21Int = Integer.parseInt(cb21.getSelectedItem().toString());
int cb22Int = Integer.parseInt(cb22.getSelectedItem().toString());
int cb23Int = Integer.parseInt(cb23.getSelectedItem().toString());
int cb24Int = Integer.parseInt(cb24.getSelectedItem().toString());
txt5.setText(String.valueOf(cb19Int + cb20Int + cb21Int + cb22Int + cb23Int + cb24Int));
}
});
jcompute.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
String prelim, midterm, prefinal, finals, total;
double a, b, c, d, tg;
prelim = txt2.getText();
midterm = txt3.getText();
prefinal = txt4.getText();
finals = txt5.getText();
a = Double.parseDouble(prelim);
b = Double.parseDouble(midterm);
c = Double.parseDouble(prefinal);
d = Double.parseDouble(finals);
tg = (a + b + c + d)/4;
total = Double.toString(tg);
finalg.setText(total);
JFrame f3 = new JFrame ("STUDENT FINAL RATING");
f3.getContentPane().setLayout(null);
f3.setSize (350,300);
//the frame2 (f2); the two frames are still visible
f2.hide();
f3.show();
f3.getContentPane().add(label1);
f3.getContentPane().add(label2);
f3.getContentPane().add(finalg);
finalg.setEditable(false);
f3.getContentPane().add(finalr);
finalr.setEditable(false);
f3.getContentPane().add(finalb);
label1.setBounds(70,20,150,70);
label2.setBounds(90,70,200,70);
finalg.setBounds(205,40,50,30);
finalr.setBounds(140,140,50,40);
finalb.setBounds(130,200,70,30);
//EQUIVALENT
grade = Double.parseDouble(finalg.getText());
if(grade>=99.50 && grade<101)
equiv = 1.00;
else if(grade<99.50 && grade>=98.50)
equiv = 1.10;
else if(grade<98.50 && grade>=97.50)
equiv = 1.20;
else if(grade<97.50 && grade>=96.50)
equiv = 1.30;
else if(grade<96.50 && grade>=95.50)
equiv = 1.40;
else if(grade<95.50 && grade>=94.50)
equiv = 1.50;
else if(grade<94.50 && grade>=93.50)
equiv = 1.60;
else if(grade<93.50 && grade>=92.50)
equiv = 1.70;
else if(grade<92.50 && grade>=91.50)
equiv = 1.80;
else if(grade<91.50 && grade>=90.50)
equiv = 1.90;
else if(grade<90.50 && grade>=89.50)
equiv = 2.00;
else if(grade<89.50 && grade>=88.50)
equiv = 2.10;
else if(grade<88.50 && grade>=87.50)
equiv = 2.20;
else if(grade<87.50 && grade>=86.50)
equiv = 2.30;
else if(grade<86.50 && grade>=85.50)
equiv = 2.40;
else if(grade<85.50 && grade>=84.50)
equiv = 2.50;
else if(grade<84.50 && grade>=83.50)
equiv = 2.60;
else if(grade<83.50 && grade>=82.50)
equiv = 2.70;
else if(grade<82.50 && grade>=81.50)
equiv = 2.80;
else if(grade<81.50 && grade>=80.50)
equiv = 2.90;
else if(grade<80.50 && grade>=79.50)
equiv = 3.00;
else if(grade<79.50 && grade>=78.50)
equiv = 3.10;
else if(grade<78.50 && grade>=77.50)
equiv = 3.20;
else if(grade<77.50 && grade>=76.50)
equiv = 3.30;
else if(grade<76.50 && grade>=75.50)
equiv = 3.40;
else if(grade<75.50 && grade>=74.50)
equiv = 3.50;
else
equiv = 5.0;
finalr.setText("" + equiv);
finalr.setEditable(false);
}
});
finalb.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "" + equiv);
if (equiv >=1.00 && equiv <=3.0) JOptionPane.showMessageDialog(null, " YOU PASSED!");
else if (equiv >=3.10 && equiv <=75) JOptionPane.showMessageDialog(null, " YOU FAILED!");
}
});
jclear.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
cb1.setSelectedItem(Integer.valueOf(0));
cb2.setSelectedItem(Integer.valueOf(0));
cb3.setSelectedItem(Integer.valueOf(0));
cb4.setSelectedItem(Integer.valueOf(0));
cb5.setSelectedItem(Integer.valueOf(0));
cb6.setSelectedItem(Integer.valueOf(0));
cb7.setSelectedItem(Integer.valueOf(0));
cb8.setSelectedItem(Integer.valueOf(0));
cb9.setSelectedItem(Integer.valueOf(0));
cb10.setSelectedItem(Integer.valueOf(0));
cb11.setSelectedItem(Integer.valueOf(0));
cb12.setSelectedItem(Integer.valueOf(0));
cb13.setSelectedItem(Integer.valueOf(0));
cb14.setSelectedItem(Integer.valueOf(0));
cb15.setSelectedItem(Integer.valueOf(0));
cb16.setSelectedItem(Integer.valueOf(0));
cb17.setSelectedItem(Integer.valueOf(0));
cb18.setSelectedItem(Integer.valueOf(0));
cb19.setSelectedItem(Integer.valueOf(0));
cb20.setSelectedItem(Integer.valueOf(0));
cb21.setSelectedItem(Integer.valueOf(0));
cb22.setSelectedItem(Integer.valueOf(0));
cb23.setSelectedItem(Integer.valueOf(0));
cb24.setSelectedItem(Integer.valueOf(0));
txt2.setText(" ");
txt3.setText(" ");
txt4.setText(" ");
txt5.setText(" ");
}
});
jexit.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
System.exit(0);
}
});
f1.show();
}
public static void main (String args []){
forfinals xx = new forfinals();
}
}
These were the problems,
1.When you give zero in quotes like "0" its taken as string, whereas Integer is what required.
2.The jFrame f2 was not accessible because it was defined inside another action event.
3.The same was the problem with the variable equiv, thats why it was not accessible in the event of jOptionpane.

Categories