1

My program is simulating voting in the Electoral College. Each state has a certain number of votes. I am having trouble with getting my buttons for each state to return the proper amount of votes to a total, and then printing that total to the screen in a JTextField each time a button for that party is clicked.

My question is should I be using an ItemStateChanged or ActionListener? After some research online, I currently am using an ActionListener, but I can't seem to figure out the final implementation or if this is the best way of going about finishing my program.

ActionListener RadioButtonActionListener = new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e) 
            {
                if(e.getSource() instanceof JRadioButton)
                {
                    if(Democrat.isSelected() == true)
                    {
                        String Count = StateValue.getText();
                        int countInteger = Integer.parseInt(Count);
                        int demoCount = demoCount + countInteger;
                        demoTotal.setText(demoCount);
                    }
                    else if (Democrat.isSelected() == false)
                    {

                    }
                }
            }

        };

Rest of my code:

public class ElectoralCollegeGUI extends JFrame 
{
private static final long serialVersionUID = 1L;
private static int demoVoteCount = 0;
private static int repVoteCount = 0;
private static int undVoteCount = 0;
private JRadioButton Democrat,Republican,Undecided;
private static String whatState;

public ElectoralCollegeGUI()
{
    super("Cast Your Votes");
    //JPanel mainGridPanel = new JPanel();
    setLayout(new GridLayout(22,5));
    JLabel demoVoteLabel = new JLabel("Democrat Votes");
    JTextField demoTotal = new JTextField();
    JLabel repVoteLabel = new JLabel("Republican Votes");
    JTextField repTotal = new JTextField();     
    JLabel undVoteLabel = new JLabel("Undecided Votes");
    JTextField undTotal = new JTextField();




    demoTotal.setEditable(false);
    repTotal.setEditable(false);
    undTotal.setEditable(false);

    add(demoTotal, BorderLayout.SOUTH);
    add(repTotal, BorderLayout.SOUTH);
    add(undTotal,BorderLayout.SOUTH);
    add(demoVoteLabel);
    add(repVoteLabel);
    add(undVoteLabel);

    String[] state = {"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut","Delaware", 
              "Florida" , "Georgia" ,"Hawaii","Idaho", "Illinois", "Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine 1st", 
              "Maine 2nd" ,"Maine Popular","Maryland", "Massachusetts","Michigan","Minnesota","Mississippi","Missouri", 
              "Montant",   "Nebraska 1st",   "Nebraska 2nd",   "Nebraska 3rd", "Nebraska Popular", "Nevada","New Hampshire",
              "New Jersey",   "New Mexico","New York",   "North Carolina", "North Dakota",   "Ohio",   "Oklahoma",
              "Oregon",   "Pennsylvania",   "Rhode Island",   "South Carolina",   "South Dakota",
              "Tennessee",   "Texas",  "Utah",  "Vermont",  "Virginia",  "Washington",
              "West Virginia",  "Wisconsin",  "Wyoming",  "Washington,D.C.",};  

    String[] voteValue = { "9","3","11","6","55","9","7","3","29","16","4","4","20","11","6",
                        "6","8","8","1","1","2","10","11","16","10","6","10","3","1","1","1",
                        "2","6","4","14","5","29","15","3","18","7","7","20","4","9","3","11",
                        "38","6","3","13","12","5","10","3","3"};



    for ( int i = 0; i < 56 ; i++)
    {
        add(new VoteChoice(state[i] , voteValue[i]));
    }

}
    private class VoteChoice extends JPanel
    {
        private static final long serialVersionUID = 1L;

        public VoteChoice(String state, String voteValue)
        {
            setLayout(new FlowLayout());

            JLabel StateName = new JLabel(state);
            JLabel StateValue = new JLabel(voteValue);

            ButtonGroup party;
            party = new ButtonGroup();

            Democrat = new JRadioButton("Democrat");
            Republican = new JRadioButton("Republican");
            Undecided = new JRadioButton("Undecided");

            //adds buttons to party button group
            party.add(Democrat);
            party.add(Republican);
            party.add(Undecided);



            add(StateName, BorderLayout.WEST);
            add(StateValue,BorderLayout.WEST);
            add(Democrat, BorderLayout.EAST);
            add(Republican, BorderLayout.EAST);
            add(Undecided, BorderLayout.EAST);

            RadioButtonActionListener actionListener = new RadioButtonActionListener();
            Democrat.addActionListener(actionListener);
            Republican.addActionListener(actionListener);
            Undecided.addActionListener(actionListener);
user3712626
  • 115
  • 1
  • 9
  • I appreciate that you want to find a solution to your problem, but it isn't really appropriate to delete and repost your question, especially within a 12 hour period, their a lot of people who will still be logging in over the next 24 hours who might be able help, so you may need to be a little patient – MadProgrammer Oct 11 '15 at 23:41
  • If all you want to know is when a button is triggered (or selected in your case), an ActionListener is probably your best choice – MadProgrammer Oct 11 '15 at 23:44
  • You may get better responses if you provide a compilable and runnable example which demonstrates your problem – MadProgrammer Oct 11 '15 at 23:46
  • Thank you, the reason for the repost was I had made a few changes to my code since, more than I thought an edit would be appropriate for as well as changed the wording for my question to make it clearer that issue I am trying to resolve. – user3712626 Oct 11 '15 at 23:55
  • I appreciate that, I might have been better to update the original question, but we're here now ;) – MadProgrammer Oct 11 '15 at 23:59

1 Answers1

0

So, from what I can ascertain from you code snippet, you want to know when changes occur on VoteChoice and update a field in the ElectoralCollegeGUI.

Now you are creating multiple instances of VoteChoice ...

for ( int i = 0; i < 56 ; i++)
{
    add(new VoteChoice(state[i] , voteValue[i]));
}

But the JRadioButton's are instance fields of the ElectoralCollegeGUI class, this is going to mean that the fields will only have context to the last instance of VoteChoice that you created.

Instead, the radio buttons should be instance fields of the VoteChoice class and the VoteChoice should monitoring their states.

When the VoteChoice detects a change, it should trigger an event back to the ElectoralCollegeGUI describing what has changed. While you could reuse one of the many listener types available in the API, you might create your own, and using enums, you could easily determine what has changed...

public interface VoteChoiceListener {
    public enum Party {
        DEMOCRAT,
        REPUBLICAN,
        UNDECIDED;
    }

    public void voteCast(VoiceChoice choice, Party party);
}

You would then register an instance of VoteChoiceListener with each instance of VoteChoice so it can trigger the event when a vote changes

This basically a observer pattern

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Okay that helps, but i guess my question now is, with the listener. How can I have it tell which states buttons are being selected/deselected? Is it legal to pass in my array for the states and their vote values into the Listener? – user3712626 Oct 12 '15 at 04:33
  • Realistically, all you (seem) to want to know is which one is actually selected (I might be wrong), to which you'd just use the voteCast method. You can determine which button was selected via the actionListener and the ActionEvent's getSource method. I'd also use a ButtonGroup to manage the buttons, this will ensure that only one button can be selected at a time automatically. Okay, you can also use the VoiceChoice value to determine which state the vote was cast for, maybe either use a Map in the parent class or passing the state to the VoteChoice and providing a getState method from it. – MadProgrammer Oct 12 '15 at 06:43
  • I wouldn't pass the array to the VoteChoice, but instead, pass the actual state it represents – MadProgrammer Oct 12 '15 at 06:44