Results 1 to 6 of 6
  1. #1
    theBurgh22 is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Unhappy Java GUI problem

    Hey there, I have to create a program that implements a voting maching using GUI. I have the window set up good enough for my liking. I am just having trouble enabling certain buttons at a certain time. Here are my classes and text files I am using. I have posted where I need help in comments in my code about enabling certain buttons and what not.. thanks for you help in advance.

    my main class:
    Java Code:
    import java.util.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Assign4 extends JPanel{
    	private Ballot [] ballots;
    	
    	public Assign4(String file) throws IOException{
    		ballots = new Ballot[10];
    		ballots[0] = new Ballot(file);
    		//ballots[0].setFile(file);
    		//ballots[0].castBallot(file);
    	}
    	
    	public static void main(String[] args) throws IOException {
    		String filename = args[0];
    		Assign4 assignment = new Assign4(filename);
    	}
    }

    the Ballot class
    [CODE]import java.util.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.lang.String;
    import java.util.StringTokenizer;

    public class Ballot extends JPanel {

    private ControlListener theListener;
    private String file;
    private JFrame theWindow;
    private JPanel myFrame;
    private JPanel [] thePanel;
    private JPanel votePanel;
    private JPanel [] myLabels;
    private JLabel office;
    private JButton [] candidates = new JButton[10];
    private JButton login, castVote;
    private StringTokenizer tokens;
    private int numBallots;
    private int count = 0;

    public Ballot(String file) throws IOException {
    //calls the castBallot() method.
    castBallot(file);
    //creates a JFrame
    theWindow = new JFrame("E-Vote Version 1.0");
    //creates a JPanel
    myFrame = new JPanel();
    theWindow.setDefaultCloseOperation(JFrame.DO_NOTHI NG_ON_CLOSE);

    //this layout out along with the following for loops
    //puts my buttons and labels to the myFrame panel
    myFrame.setLayout(new GridLayout(2, numBallots));
    for(int i = 0; i < numBallots; i++)
    myFrame.add(myLabels[i]);
    for(int j = 0; j < numBallots; j++)
    myFrame.add(thePanel[j]);

    //the next four lines add the myFrame panel and the
    //vote panel to theWindow JFrame, sets it size
    //and sets it visible
    theWindow.add(myFrame, BorderLayout.CENTER);
    theWindow.add(votePanel, BorderLayout.SOUTH);
    theWindow.setSize(500,450);
    theWindow.setVisible(true);
    }

    public void castBallot(String file) throws IOException {
    //creates my votePanel which will hold the login and
    //castVote buttons
    votePanel = new JPanel();

    //creates the two buttons login and cast vote
    login = new JButton("Login to Vote");
    castVote = new JButton("Cast your Vote");

    //adds both buttons to the votePanel JPanel
    votePanel.add(login);
    votePanel.add(castVote);

    //sets the login button so that it is enabled
    //and the castVote button so that it is disabled
    login.setEnabled(true);
    castVote.setEnabled(false);

    //creates a scanner and reads from a file typed in the
    //command line and reads how many ballots there are
    Scanner fScan = new Scanner(new File(file));
    numBallots = Integer.parseInt(fScan.nextLine());

    //creates new panel and new label objects according to
    //the size indicated in the file of ballots
    thePanel = new JPanel[numBallots];
    myLabels = new JPanel[numBallots];

    //creates a controlListener object to link button clicks
    //to the ActionListener
    theListener = new ControlListener();

    //this loop and nested loop reads in the offices and adds
    //them to the myLabels panel then the candidates and adds
    //them to the thePanel panel
    for(int i = 0; i < numBallots; i++) {
    String officeCand = fScan.nextLine();
    tokens = new StringTokenizer(officeCand);
    String offices = tokens.nextToken(":");
    office = new JLabel(offices);
    myLabels[i] = new JPanel();
    myLabels[i].add(office);

    thePanel[i] = new JPanel();

    for(int j = 0; tokens.hasMoreTokens(); j++) {
    String candidat = tokens.nextToken(",");
    candidates[j] = new JButton(candidat);
    candidates[j].addActionListener(theListener);
    candidates[j].setEnabled(false);
    thePanel[i].add(candidates[j]);
    count++;
    }
    }

    //links the two buttons, login and castVote, to the
    //controlListener class (event handler)
    login.addActionListener(theListener);
    castVote.addActionListener(theListener);
    }

    //public void setFile(String filename){
    // System.out.println("File: " + filename);
    // this.file = filename;
    //}

    private class ControlListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    String voter;
    Component comp = (Component) e.getSource();

    //if the login button is clicked, these actions are
    //to take place
    if(comp == (JButton)login) {
    try {
    //create a scanner that reads from the voters.txt file
    Scanner inScan = new Scanner(new File("voters.txt"));
    voter = inScan.nextLine();
    StringTokenizer token = new StringTokenizer(voter);
    String id = token.nextToken(":");

    //prompts the user to enter his or her ID to test if they are registerd
    String input = JOptionPane.showInputDialog("Please enter your ID:");
    do {

    //if their input matches the first ID in the file, it then
    //reads in the the user's name and checks to see if they have
    //voted yet (false if not, true if they have)
    if(input.equals(id)) {
    String name = token.nextToken(":");
    boolean valid;
    String TorF = token.nextToken();
    if (TorF.equals("false")) {
    JOptionPane.showMessageDialog(null, name + ", you have logged in successfully!");
    login.setEnabled(false);
    castVote.setEnabled(true);

    //i need to enable all of the candidate buttons here, but this is where
    //i'm having trouble, only a few are enabled...
    for(int index = 0; index < count; index++) {
    candidates[index].setEnabled(true);
    }

    //then i have to all the user to vote for the candidate of their
    //choice for each office. once they make their choices they push the
    //"castVote" button.

    //then i must update the office files with the votes that the user chose
    //and update the voter file so that, that user's status is
    //set to false
    }
    else if(TorF.equals("true"))
    JOptionPane.showMessageDialog(null, "Hi " + name + ", you have voted already.");
    }

    //if the ID doesn't match the first ID of the file, it should search
    //the next line for it, and so on until it finds a match or
    //it reaches the end of the file
    else {
    voter = inScan.nextLine();
    id = token.nextToken(":");
    }
    } while (!input.equals(token));
    }
    catch (Exception bad) {
    voter = null;
    }
    }
    }
    }
    }CODE]

    my ballot.txt file:
    Java Code:
    3
    Best Quarterback:Ben Roethlisburger,Tom Brady,Peyton Manning,Drew Brees,Philip Rivers
    Best Comedy:Old School,Dumb and Dumber,Billy Madison
    Favorite City:New York,Philadelphia,Pittsburgh,Chicago
    my voters.txt file
    Java Code:
    1234:Herb Johnson:false
    1111:Big John:false
    2222:John Deer:false
    3333:Dave Beer:false

  2. #2
    theBurgh22 is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default

    sorry the Ballot.java file was messed up...

    Java Code:
    import java.util.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.lang.String;
    import java.util.StringTokenizer;
    
    public class Ballot extends JPanel {
    
    	private ControlListener theListener;
    	private String file;
    	private JFrame theWindow;
    	private JPanel myFrame;
    	private JPanel [] thePanel;
    	private JPanel votePanel;
    	private JPanel [] myLabels;
    	private JLabel office;
    	private JButton [] candidates = new JButton[10];
    	private JButton login, castVote;
    	private StringTokenizer tokens;
    	private int numBallots;
    	private int count = 0;
    	
    	public Ballot(String file) throws IOException {
    		//calls the castBallot() method.
    		castBallot(file);
    		//creates a JFrame
    		theWindow = new JFrame("E-Vote Version 1.0");
    		//creates a JPanel
    		myFrame = new JPanel();
    		theWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    		
    		//this layout out along with the following for loops
    		//puts my buttons and labels to the myFrame panel
    		myFrame.setLayout(new GridLayout(2, numBallots));
    		for(int i = 0; i < numBallots; i++) 
    			myFrame.add(myLabels[i]);
    		for(int j = 0; j < numBallots; j++) 
    			myFrame.add(thePanel[j]);
    			
    		//the next four lines add the myFrame panel and the
    		//vote panel to theWindow JFrame, sets it size
    		//and sets it visible
    		theWindow.add(myFrame, BorderLayout.CENTER);
    		theWindow.add(votePanel, BorderLayout.SOUTH);
    		theWindow.setSize(500,450);
    		theWindow.setVisible(true);
    	}
    	
    	public void castBallot(String file) throws IOException {
    		//creates my votePanel which will hold the login and 
    		//castVote buttons
    		votePanel = new JPanel();
    		
    		//creates the two buttons login and cast vote
    		login = new JButton("Login to Vote");
    		castVote = new JButton("Cast your Vote");
    		
    		//adds both buttons to the votePanel JPanel
    		votePanel.add(login);
    		votePanel.add(castVote);
    		
    		//sets the login button so that it is enabled
    		//and the castVote button so that it is disabled
    		login.setEnabled(true);
    		castVote.setEnabled(false);
    		
    		//creates a scanner and reads from a file typed in the 
    		//command line and reads how many ballots there are
    		Scanner fScan = new Scanner(new File(file));
    		numBallots = Integer.parseInt(fScan.nextLine());
    		
    		//creates new panel and new label objects according to
    		//the size indicated in the file of ballots
    		thePanel = new JPanel[numBallots];
    		myLabels = new JPanel[numBallots];
    		
    		//creates a controlListener object to link button clicks
    		//to the ActionListener
    		theListener = new ControlListener();
    		
    		//this loop and nested loop reads in the offices and adds
    		//them to the myLabels panel then the candidates and adds 
    		//them to the thePanel panel
    		for(int i = 0; i < numBallots; i++) {
    			String officeCand = fScan.nextLine();
    			tokens = new StringTokenizer(officeCand);
    			String offices = tokens.nextToken(":");
    			office = new JLabel(offices);
    			myLabels[i] = new JPanel();
    			myLabels[i].add(office);
    			
    			thePanel[i] = new JPanel();
    			
    			for(int j = 0; tokens.hasMoreTokens(); j++) {
    				String candidat = tokens.nextToken(",");
    				candidates[j] = new JButton(candidat);
    				candidates[j].addActionListener(theListener);
    				candidates[j].setEnabled(false);
    				thePanel[i].add(candidates[j]);
    				count++;
    			}
    		}
    		
    		//links the two buttons, login and castVote, to the 
    		//controlListener class (event handler)
    		login.addActionListener(theListener);
    		castVote.addActionListener(theListener);
    	}
    	
    	//public void setFile(String filename){
    	//		System.out.println("File: " + filename);
    	//		this.file = filename;
    	//}
    	
    	private class ControlListener implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			String voter; 
    			Component comp = (Component) e.getSource();
    			
    			//if the login button is clicked, these actions are 
    			//to take place
    			if(comp == (JButton)login) {
    				try {
    					//create a scanner that reads from the voters.txt file
    					Scanner inScan = new Scanner(new File("voters.txt"));
    					voter = inScan.nextLine();
    					StringTokenizer token = new StringTokenizer(voter);
    					String id = token.nextToken(":");
    					
    					//prompts the user to enter his or her ID to test if they are registerd
    					String input = JOptionPane.showInputDialog("Please enter your ID:");
    					do {
    					
    						//if their input matches the first ID in the file, it then
    						//reads in the the user's name and checks to see if they have
    						//voted yet (false if not, true if they have)
    						if(input.equals(id)) {
    							String name = token.nextToken(":");
    							boolean valid;
    							String TorF = token.nextToken();
    							if (TorF.equals("false")) {
    								JOptionPane.showMessageDialog(null, name + ", you have logged in successfully!");
    								login.setEnabled(false);
    								castVote.setEnabled(true);
    								
    								//i need to enable all of the candidate buttons here, but this is where
    								//i'm having trouble, only a few are enabled...
    								for(int index = 0; index < count; index++) {
    									candidates[index].setEnabled(true);
    								}
    								
    								//then i have to all the user to vote for the candidate of their
    								//choice for each office. once they make their choices they push the 
    								//"castVote" button. 
    								
    								//then i must update the office files with the votes that the user chose
    								//and update the voter file so that, that user's status is
    								//set to false
    							}
    							else if(TorF.equals("true")) 
    								JOptionPane.showMessageDialog(null, "Hi " + name + ", you have voted already.");
    						}
    						
    						//if the ID doesn't match the first ID of the file, it should search
    						//the next line for it, and so on until it finds a match or 
    						//it reaches the end of the file
    						else {
    							voter = inScan.nextLine();
    							id = token.nextToken(":");
    						}
    					} while (!input.equals(token));
    				}
    				catch (Exception bad) {
    					voter = null;	
    				}
    			}
    		}
    	}
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I think that you need to go about this differently and more flexibly. I'd recommend that you create a class that extends JPanel, say called QuestionPanel that displays the question string on the top (BorderLayout.NORTH) in a JLabel, and displays a JPanel with a column of JRadioButton possible answers in a GridLayout. Add the JRadioButtons to a ButtonGroup so that only one can be selected at a time. Then give the class a method, say setEnabled(boolean enabled), and in this method iterate through the JRadioButtons enabling or disabling them depending on the boolean passed in. You would also give it a method say, getSelection, that returns the String selected (or "" if no Strings are selected). You could also give it a constructor that accepts a String, question, and an array of String, answers, so you could easily use this same JPanel for all questions.

    Luck!
    Last edited by Fubarable; 11-18-2010 at 09:54 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    but your current problem is that each time you create a column of candidate JButtons, you over-write the previously created JButtons, probably because you use "j" as the candidate array index and not "count". But if you fix this, you'll find another error, that your candidate array is too small.

    Again, I recommend dividing and conquering -- making smaller classes to divide up the problem and avoid code repetition.

  5. #5
    shadowfax57 is offline Member
    Join Date
    Aug 2010
    Posts
    8
    Rep Power
    0

    Default

    So, what grade did you get on the CS401 assignment?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default Re: Java GUI problem

    Closing this thread.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Problem with Java...
    By mc6415 in forum New To Java
    Replies: 10
    Last Post: 02-22-2010, 12:24 AM
  2. Java problem help!
    By wexgal in forum New To Java
    Replies: 3
    Last Post: 10-26-2009, 12:52 PM
  3. Need help with a Java problem.
    By firestarter in forum New To Java
    Replies: 2
    Last Post: 10-04-2009, 12:24 AM
  4. JAVA and XML Problem
    By jackchang in forum XML
    Replies: 4
    Last Post: 02-22-2009, 08:28 PM
  5. java SE 6 problem
    By techlance in forum Java Applets
    Replies: 1
    Last Post: 06-28-2007, 10:10 AM

Posting Permissions

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