Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    aanders5 is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Exclamation Simple Action/Listener Help GUI

    Ok, I have been searching around for awhile now for examples on Action/Listeners for a JButton, and JtextField update. All of the examples I have found are too complex for me to follow.
    So, basically I am making a program that has THREE textfields. You enter in a number of votes, and then click the Jbutton CALCULATE. that will trigger a class to check for the winner (have not made that yet). Then the radio box next to the winner will by checked, and the text "The winner is..." will update with the piece's number.
    (see image below for GUI view)

    So, all I am confused on is how to connect the JButton's action with the rest of the project, to update the winner, and the radio box to become checked.
    (if you notice anything else I can do with the source code that would help, feel free to share)

    Thanks for the help,
    -Austin

    Here's my source code:
    Oh, and ignore that string to Int thing, it was just my fail way of trying to get something to work awhile back. :P I'm still new to JAVA.
    Just cleaned the source code up a bit, and added some comments.
    Java Code:
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    /*******************************************************************************************
     * @author Austin Anderson
     * This program will take in the votes of three art pieces, and then calculate the winner.
     *******************************************************************************************/
    public class ArtPrizeEngine {
    
    	/*******************************************************************************************
    	 * Contains the variables used to track the votes for when finding the winner.
    	 *******************************************************************************************/
    	static int vote1 = 0, vote2 = 0, vote3 = 0;
    
    	static String vote1_string = "", vote2_string = "",
    			vote3_string = "", winner = null;
    
    
    	/*******************************************************************************************
    	 * The main method calls the GUI.
    	 *******************************************************************************************/
    	public static void main(String arg[]) {
    
    		ArtPrizeEngineGUI();
    
    	}
    
    
    	/*******************************************************************************************
    	 * The GUI below is the engine of the program, and the GUI itself. It will display the
    	 * voting GUI, count the entered votes, and then calculate the winner by displaying text
    	 * and checking the appropriate radio box.
    	 *******************************************************************************************/
    	public static void ArtPrizeEngineGUI() {
    		/*******************************************************************************************
    		 * The frame and the three main panels used to build the overall GUI, are created here.
    		 *******************************************************************************************/
    		JFrame frame = new JFrame("Art Prize Voter!");
    		JPanel panelmain = new JPanel();
    		JPanel panelmain_votes = new JPanel();
    		JPanel panelmain_button = new JPanel();
    		frame.setPreferredSize(new Dimension(400, 175));
    		/*******************************************************************************************
    		 * GUI for panel1. It creates the JLabel and JPanel for
    		 * displaying the JLabel for piece # 1.
    		 *******************************************************************************************/
    		JPanel panel1 = new JPanel();
    		JLabel jtextlabel1 = new JLabel(
    				"Enter your votes for piece # 1: ");
    		panel1.add(jtextlabel1);
    		/*******************************************************************************************
    		 * GUI for panel2. It creates the JLabel and JPanel for
    		 * displaying the JLabel for piece # 2.
    		 *******************************************************************************************/
    		JPanel panel2 = new JPanel();
    		JLabel jtextlabel2 = new JLabel(
    				"Enter your votes for piece # 2: ");
    		panel2.add(jtextlabel2);
    		/*******************************************************************************************
    		 * GUI for panel3. It creates the JLabel and JPanel for
    		 * displaying the JLabel for piece # 3.
    		 *******************************************************************************************/
    		JPanel panel3 = new JPanel();
    		JLabel jtextlabel3 = new JLabel(
    				"Enter your votes for piece # 3: ");
    		panel3.add(jtextlabel3);
    		/*******************************************************************************************
    		 * GUI for panel4. It creates the JTextfield and JPanel for
    		 * piece 1's voting input.
    		 *******************************************************************************************/
    		JPanel panel4 = new JPanel();
    		JTextField textlabel4_in = new JTextField(5);
    		panel4.add(textlabel4_in);
    		textlabel4_in.setText("" + 0 + "");
    		vote1_string = textlabel4_in.getText();
    		vote1 = Integer.parseInt(vote1_string);
    		/*******************************************************************************************
    		 * GUI for panel5. It creates the JTextfield and JPanel for
    		 * piece 2's voting input.
    		 *******************************************************************************************/
    		JPanel panel5 = new JPanel();
    		JTextField textlabel5_in = new JTextField(5);
    		panel5.add(textlabel5_in);
    		textlabel5_in.setText("" + 0 + "");
    		vote2_string = textlabel5_in.getText();
    		vote2 = Integer.parseInt(vote2_string);
    		/*******************************************************************************************
    		 * GUI for panel6. It creates the JTextfield and JPanel for
    		 * piece 3's voting input.
    		 *******************************************************************************************/
    		JPanel panel6 = new JPanel();
    		JTextField textlabel6_in = new JTextField(5);
    		panel6.add(textlabel6_in);
    		textlabel6_in.setText("" + 0 + "");
    		vote3_string = textlabel6_in.getText();
    		vote3 = Integer.parseInt(vote3_string);
    		/*******************************************************************************************
    		 * GUI for panel7. It creates the radio button field for piece
    		 * 1.
    		 *******************************************************************************************/
    		JPanel panel7 = new JPanel();
    		JRadioButton button1 = new JRadioButton("", false);
    		panel7.add(button1);
    		/*******************************************************************************************
    		 * GUI for panel8. It creates the radio button field for piece
    		 * 2.
    		 *******************************************************************************************/
    		JPanel panel8 = new JPanel();
    		JRadioButton button2 = new JRadioButton("", false);
    		panel8.add(button2);
    		/*******************************************************************************************
    		 * GUI for panel9. It creates the radio button field for piece
    		 * 3.
    		 *******************************************************************************************/
    		JPanel panel9 = new JPanel();
    		JRadioButton button3 = new JRadioButton("", false);
    		panel9.add(button3);
    		/*******************************************************************************************
    		 * The panelmain_votes, holds all of the panels above, creating
    		 * the "top" portion of the GUI. Panels are organized in their
    		 * respective sets for the Jlabel, textfield, and radio button.
    		 * Panels 1, 4, and 7 are the components for piece 1. 
    		 * Panels 2, 5, and 8 are the components for piece 2. 
    		 * Panels 3, 6, and 9 are the components for piece 3.
    		 *******************************************************************************************/
    		panelmain_votes.setLayout(new GridLayout(3, 3));
    		panelmain_votes.add(panel1);
    		panelmain_votes.add(panel4);
    		panelmain_votes.add(panel7);
    		panelmain_votes.add(panel2);
    		panelmain_votes.add(panel5);
    		panelmain_votes.add(panel8);
    		panelmain_votes.add(panel3);
    		panelmain_votes.add(panel6);
    		panelmain_votes.add(panel9);
    		/*******************************************************************************************
    		 * GUI for panelmain_button. It creates the button "Calculate".
    		 *******************************************************************************************/
    		JButton button;
    		JPanel panel_button = new JPanel();
    		button = new JButton("Calculate");
    		panel_button.add(button);
    		// button.addActionListener(this);
    		/*******************************************************************************************
    		 * The JLabel below creates a JLbael that displays who the
    		 * winner is.
    		 *******************************************************************************************/
    		JLabel jtextlabel_winner = new JLabel(
    				"     And the winner is... " + winner);
    		panel_button.add(jtextlabel_winner);
    		/*******************************************************************************************
    		 * The panelmain_button holds the Calculate button as well as
    		 * the winner text and creates the "bottom" portion of the
    		 * overall GUI.
    		 *******************************************************************************************/
    		panelmain_button.setLayout(new GridLayout(1, 2));
    		panelmain_button.add(button);
    		panelmain_button.add(jtextlabel_winner);
    		/*******************************************************************************************
    		 * panelmain adds the "top" section of the GUI (panelmain_votes), with the "bottom" section 
    		 * of the GUI (panelmain_button); to create the overall GUI.
    		 *******************************************************************************************/
    		panelmain.add(panelmain_votes);
    		panelmain.add(panelmain_button);
    		/*******************************************************************************************
    		 * panelmain is added to the frame here, and is then packed,
    		 * and set visible for client use.
    		 *******************************************************************************************/
    		frame.getContentPane().add(panelmain);
    		frame.pack();
    		frame.setVisible(true);
    	}
    
    
    	public void actionPerformed(ActionEvent e) {
    
    		if (vote1 > vote2) {
    			if (vote1 > vote3) {
    				winner = "piece 1!";
    			}
    		}
    		if (vote2 > vote1) {
    			if (vote2 > vote3) {
    				winner = "piece 2!";
    			}
    		}
    		if (vote3 > vote1) {
    			if (vote3 > vote2) {
    				winner = "piece 3!";
    			}
    		}
    		if (vote1 == vote3) {
    			winner = "piece 1 and piece 3 tied!";
    		}
    		if (vote1 == vote2) {
    			winner = "piece 1 and piece 2 tied!";
    		}
    		if (vote2 == vote3) {
    			winner = "piece 2 and piece 3 tied!";
    		}
    		if (vote1 == vote2 && vote1 == vote3) {
    			winner = "piece 1, 2 and 3 tied!";
    		}
    
    	}
    
    }

    Also, I have this image below "Art Prize Voter", that is what the display looks like.
    Is there a simple way to make the space between the JLabel, Jtextfield, and Radio Buttons closer together? As you can see they are spread quite far apart.
    Thanks again!
    Attached Thumbnails Attached Thumbnails Simple Action/Listener Help GUI-screenshot.jpg  
    Last edited by aanders5; 10-13-2010 at 10:14 PM. Reason: Cleaned up the source code a bit

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

    Default

    Your best bet is to go to the tutorials and learn about some other layout managers and use them. Also, show us your attempt at writing an ActionListener. Then we can see what you know and don't know. If you don't have a clue, the Oracle Swing tutorials can help you out with this as well.

    Luck.

  3. #3
    aanders5 is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    well, I am really not too sure.

    I know that I need to do something like
    button b.AddListener

    and then that will go to some class that looks like,
    actionPerformed(ActionEvent e)

    and within there, it will check for the winnner.

    and then I need that code, after its finished doing its job, will check the winning radio box, and setText of variable "winner" to whatever the ActionEvent set "winner" as.


    I just cleaned up the code a bit to make it more readable.
    So...im just kind of lost. I see what's suppose to happen, and once I see how it works, I know it will stick, I'm just lost on the building blocks of what to do.

    -Austin

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

    Default

    Quote Originally Posted by aanders5 View Post
    well, I am really not too sure.

    I know that I need to do something like
    button b.AddListener

    and then that will go to some class that looks like,
    actionPerformed(ActionEvent e)

    and within there, it will check for the winnner.

    and then I need that code, after its finished doing its job, will check the winning radio box, and setText of variable "winner" to whatever the ActionEvent set "winner" as.


    I just cleaned up the code a bit to make it more readable.
    So...im just kind of lost. I see what's suppose to happen, and once I see how it works, I know it will stick, I'm just lost on the building blocks of what to do.

    -Austin
    Again, don't guess. Look at the tutorials on how to create ActionListeners and how to add ActionListeners to your buttons. It's all there spelled out for you with sample code as well.

    You can find the tutorials here:
    How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    How to Use Buttons, Check Boxes, and Radio Buttons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Again, much luck!

  5. #5
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default

    Once you get a pretty and non-functional GUI, leave it alone.
    Do not add any calculations into the GUI object unless they
    pertain to the aesthetics of the GUI, like calculating the
    size of a button, or finding the center of a panel.

    Think of it as a piece of artwork with the potential to
    interact with a user.

    Add functions to the buttons with an ActionListener.
    Within an ActionListener there is a required method
    called actionPerformed(ActionEvent e), inwhich you must
    write code that determines from what GUI component
    an ActionEvent is being created, and what type of
    calculation or process you would like it to perform in
    response to the component generating the ActionEvent.

    A typical starter actionPerformed(ActionEvent e)
    method is:

    Java Code:
      public void actionPerformed(ActionEvent e){
        JMenuItem source = (JMenuItem)e.getSource();
        if(source.getActionCommand().equals(o)){
          System.out.println("O menuItem selected");
        }
      }
    Obviously designed to add fuctionality to a menuItem.

    Some people on this forum have strong feelings on how to
    add an actionListener to a component. Your code leans
    toward a certain solution that is absolutely normal and
    practical, but I have seen critisized on this forum.

    I personally don't loose sleep over this none controversial
    subject, and feel like an idiot just typing these last
    three sentences.

    After you design your actionListener, add it to the
    components you would like to see it work with.
    it's usually done like this:

    myButton.addActionListener(new MenuActionListener(this));


    I have left a lot of details out.
    You will have to look at a tutorial
    to get a better feel for how this works.

    also..

    In your code, you shouldn't have so many static variables.
    S in column 1 marks places where they are not needed.
    X in column 1 marks calculations that should be removed
    and probably placed in the actionListener.

    AL in colum 1 marks the function that should go in the
    actionListener.

    or..

    change
    Java Code:
    public class ArtPrizeEngine {
    to

    Java Code:
    public class ArtPrizeEngine implements ActionListener{
    and it will be OK.. ..almost.
    you'll see in the tutorial.


    Java Code:
    public class ArtPrizeEngine {
    
      /*******************************************************************************************
       * Contains the variables used to track the votes for when finding the winner.
       *******************************************************************************************/
    S  static int vote1 = 0, vote2 = 0, vote3 = 0;
    
    S  static String vote1_string = "", vote2_string = "",
          vote3_string = "", winner = null;
    
    
      /*******************************************************************************************
       * The main method calls the GUI.
       *******************************************************************************************/
       public static void main(String arg[]) {
    
        ArtPrizeEngineGUI();
    
      }
    
    
      /*******************************************************************************************
       * The GUI below is the engine of the program, and the GUI itself. It will display the
       * voting GUI, count the entered votes, and then calculate the winner by displaying text
       * and checking the appropriate radio box.
       *******************************************************************************************/
    S  public static void ArtPrizeEngineGUI() {
        /*******************************************************************************************
         * The frame and the three main panels used to build the overall GUI, are created here.
         *******************************************************************************************/
        JFrame frame = new JFrame("Art Prize Voter!");
        JPanel panelmain = new JPanel();
        JPanel panelmain_votes = new JPanel();
        JPanel panelmain_button = new JPanel();
        frame.setPreferredSize(new Dimension(400, 175));
        /*******************************************************************************************
         * GUI for panel1. It creates the JLabel and JPanel for
         * displaying the JLabel for piece # 1.
         *******************************************************************************************/
        JPanel panel1 = new JPanel();
        JLabel jtextlabel1 = new JLabel(
            "Enter your votes for piece # 1: ");
        panel1.add(jtextlabel1);
        /*******************************************************************************************
         * GUI for panel2. It creates the JLabel and JPanel for
         * displaying the JLabel for piece # 2.
         *******************************************************************************************/
        JPanel panel2 = new JPanel();
        JLabel jtextlabel2 = new JLabel(
            "Enter your votes for piece # 2: ");
        panel2.add(jtextlabel2);
        /*******************************************************************************************
         * GUI for panel3. It creates the JLabel and JPanel for
         * displaying the JLabel for piece # 3.
         *******************************************************************************************/
        JPanel panel3 = new JPanel();
        JLabel jtextlabel3 = new JLabel(
            "Enter your votes for piece # 3: ");
        panel3.add(jtextlabel3);
        /*******************************************************************************************
         * GUI for panel4. It creates the JTextfield and JPanel for
         * piece 1's voting input.
         *******************************************************************************************/
        JPanel panel4 = new JPanel();
        JTextField textlabel4_in = new JTextField(5);
        panel4.add(textlabel4_in);
        textlabel4_in.setText("" + 0 + "");
        vote1_string = textlabel4_in.getText();
        vote1 = Integer.parseInt(vote1_string);
        /*******************************************************************************************
         * GUI for panel5. It creates the JTextfield and JPanel for
         * piece 2's voting input.
         *******************************************************************************************/
        JPanel panel5 = new JPanel();
        JTextField textlabel5_in = new JTextField(5);
        panel5.add(textlabel5_in);
        textlabel5_in.setText("" + 0 + "");
    x    vote2_string = textlabel5_in.getText();
    x    vote2 = Integer.parseInt(vote2_string);
        /*******************************************************************************************
         * GUI for panel6. It creates the JTextfield and JPanel for
         * piece 3's voting input.
         *******************************************************************************************/
        JPanel panel6 = new JPanel();
        JTextField textlabel6_in = new JTextField(5);
        panel6.add(textlabel6_in);
        textlabel6_in.setText("" + 0 + "");
    x    vote3_string = textlabel6_in.getText();
    x    vote3 = Integer.parseInt(vote3_string);
        /*******************************************************************************************
         * GUI for panel7. It creates the radio button field for piece
         * 1.
         *******************************************************************************************/
        JPanel panel7 = new JPanel();
        JRadioButton button1 = new JRadioButton("", false);
        panel7.add(button1);
        /*******************************************************************************************
         * GUI for panel8. It creates the radio button field for piece
         * 2.
         *******************************************************************************************/
        JPanel panel8 = new JPanel();
        JRadioButton button2 = new JRadioButton("", false);
        panel8.add(button2);
        /*******************************************************************************************
         * GUI for panel9. It creates the radio button field for piece
         * 3.
         *******************************************************************************************/
        JPanel panel9 = new JPanel();
        JRadioButton button3 = new JRadioButton("", false);
        panel9.add(button3);
        /*******************************************************************************************
         * The panelmain_votes, holds all of the panels above, creating
         * the "top" portion of the GUI. Panels are organized in their
         * respective sets for the Jlabel, textfield, and radio button.
         * Panels 1, 4, and 7 are the components for piece 1. 
         * Panels 2, 5, and 8 are the components for piece 2. 
         * Panels 3, 6, and 9 are the components for piece 3.
         *******************************************************************************************/
        panelmain_votes.setLayout(new GridLayout(3, 3));
        panelmain_votes.add(panel1);
        panelmain_votes.add(panel4);
        panelmain_votes.add(panel7);
        panelmain_votes.add(panel2);
        panelmain_votes.add(panel5);
        panelmain_votes.add(panel8);
        panelmain_votes.add(panel3);
        panelmain_votes.add(panel6);
        panelmain_votes.add(panel9);
        /*******************************************************************************************
         * GUI for panelmain_button. It creates the button "Calculate".
         *******************************************************************************************/
        JButton button;
        JPanel panel_button = new JPanel();
        button = new JButton("Calculate");
        panel_button.add(button);
        // button.addActionListener(this);
        /*******************************************************************************************
         * The JLabel below creates a JLbael that displays who the
         * winner is.
         *******************************************************************************************/
        JLabel jtextlabel_winner = new JLabel(
            "     And the winner is... " + winner);
        panel_button.add(jtextlabel_winner);
        /*******************************************************************************************
         * The panelmain_button holds the Calculate button as well as
         * the winner text and creates the "bottom" portion of the
         * overall GUI.
         *******************************************************************************************/
        panelmain_button.setLayout(new GridLayout(1, 2));
        panelmain_button.add(button);
        panelmain_button.add(jtextlabel_winner);
        /*******************************************************************************************
         * panelmain adds the "top" section of the GUI (panelmain_votes), with the "bottom" section 
         * of the GUI (panelmain_button); to create the overall GUI.
         *******************************************************************************************/
        panelmain.add(panelmain_votes);
        panelmain.add(panelmain_button);
        /*******************************************************************************************
         * panelmain is added to the frame here, and is then packed,
         * and set visible for client use.
         *******************************************************************************************/
        frame.getContentPane().add(panelmain);
        frame.pack();
        frame.setVisible(true);
      }
    
    
    AL  public void actionPerformed(ActionEvent e) {
    AL
    AL    if (vote1 > vote2) {
    AL      if (vote1 > vote3) {
    AL        winner = "piece 1!";
    AL      }
    AL    }
    AL    if (vote2 > vote1) {
    AL      if (vote2 > vote3) {
    AL        winner = "piece 2!";
    AL      }
    AL    }
    AL    if (vote3 > vote1) {
    AL      if (vote3 > vote2) {
    AL        winner = "piece 3!";
    AL      }
    AL    }
    AL    if (vote1 == vote3) {
    AL      winner = "piece 1 and piece 3 tied!";
    AL    }
    AL    if (vote1 == vote2) {
    AL      winner = "piece 1 and piece 2 tied!";
    AL    }
    AL    if (vote2 == vote3) {
    AL      winner = "piece 2 and piece 3 tied!";
    AL    }
    AL    if (vote1 == vote2 && vote1 == vote3) {
    AL      winner = "piece 1, 2 and 3 tied!";
    AL    }
    AL
    AL  }
    
    }
    Last edited by paul pasciak; 10-14-2010 at 12:09 AM.

  6. #6
    aanders5 is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    Ok I will look into those links.
    The sites I had been generating earlier though were far too complex for me to see how the action/listener was in use.

    I'll get back to this post by tomorrow, so thanks thus far!
    -Austin

  7. #7
    aanders5 is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    Thanks for that info Paul, I had also asked one my mates what to do and he asked where my GUI was, and I was like "Its all in this class" and was like "You mixed your engine and GUI?!> ...I'm not gonna deal with this then" (jokingly). :P

    I will look at the tutorials for the action listener stuff, but how would I go about separating the GUI from the engine?

  8. #8
    aanders5 is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    Another problem, how do I set the radiobox to TRUE, for when the winner is calculated.

    like, some sort of setRadiobutton1 = ("", true); feature
    Thanks

  9. #9
    aanders5 is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    Another quick question too, I got this to update the label for "The winner is..."

    jtextlabel_winner.setLabel(" And the winner is... " + winner);
    And the .setLabel isi giving me an error, says the .setLabel is undefined for the string. :/

  10. #10
    aanders5 is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    Ok, so now the program wont even run. *facepalm*. :P

    changed some stuff around, um, and the questions above are still unfixed, this is my update/sloppy code now, I moved some stuff around.

    -Variables moved, Tut's said to make em universal so I moved a lot of them up by the main.
    -Added some stuff into the actionPreformed section, like a way to change the "winner" label, I also moved the INTparsing stuff into there as well (if thats even needed).
    -Added button.addActionListener into the button part of my GUI
    -removed static's.
    -Main wont run now though

    So, im sorry I'm throwing a lot at you, but I am learning, rather than just doing, which is making really happy right now.

    here's the code, sorry its sloppy, and for the things I moved about, I just commented them, in case i need to go back to an earlier state.

    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    /*******************************************************************************************
     * @author Austin Anderson
     * This program will take in the votes of three art pieces, and then calculate the winner.
     *******************************************************************************************/
    public class ArtPrizeEngine implements ActionListener {
    
    	/*******************************************************************************************
    	 * Contains the variables used to track the votes for when finding the winner.
    	 *******************************************************************************************/
    	int vote1 = 0, vote2 = 0, vote3 = 0;
    
    	String vote1_string = "", vote2_string = "",
    			vote3_string = "", winner = null;
    	
    	JLabel jtextlabel_winner = new JLabel(
    			"     And the winner is... " + winner);
    	JRadioButton button1 = new JRadioButton("", false);
    	JRadioButton button2 = new JRadioButton("", false);
    	JRadioButton button3 = new JRadioButton("", false);
    	JTextField textlabel4_in = new JTextField(5);
    	JTextField textlabel5_in = new JTextField(5);
    	JTextField textlabel6_in = new JTextField(5);
    
    	/*******************************************************************************************
    	 * The main method calls the GUI.
    	 *******************************************************************************************/
    	public void main(String arg[]) {
    		ArtPrizeEngineGUI();
    	}
    
    	
    
    	/*******************************************************************************************
    	 * The GUI below is the engine of the program, and the GUI itself. It will display the
    	 * voting GUI, count the entered votes, and then calculate the winner by displaying text
    	 * and checking the appropriate radio box.
    	 *******************************************************************************************/
    	public void ArtPrizeEngineGUI() {
    		/*******************************************************************************************
    		 * The frame and the three main panels used to build the overall GUI, are created here.
    		 *******************************************************************************************/
    		JFrame frame = new JFrame("Art Prize Voter!");
    		JPanel panelmain = new JPanel();
    		JPanel panelmain_votes = new JPanel();
    		JPanel panelmain_button = new JPanel();
    		frame.setPreferredSize(new Dimension(400, 175));
    		/*******************************************************************************************
    		 * GUI for panel1. It creates the JLabel and JPanel for
    		 * displaying the JLabel for piece # 1.
    		 *******************************************************************************************/
    		JPanel panel1 = new JPanel();
    		JLabel jtextlabel1 = new JLabel(
    				"Enter your votes for piece # 1: ");
    		panel1.add(jtextlabel1);
    		/*******************************************************************************************
    		 * GUI for panel2. It creates the JLabel and JPanel for
    		 * displaying the JLabel for piece # 2.
    		 *******************************************************************************************/
    		JPanel panel2 = new JPanel();
    		JLabel jtextlabel2 = new JLabel(
    				"Enter your votes for piece # 2: ");
    		panel2.add(jtextlabel2);
    		/*******************************************************************************************
    		 * GUI for panel3. It creates the JLabel and JPanel for
    		 * displaying the JLabel for piece # 3.
    		 *******************************************************************************************/
    		JPanel panel3 = new JPanel();
    		JLabel jtextlabel3 = new JLabel(
    				"Enter your votes for piece # 3: ");
    		panel3.add(jtextlabel3);
    		/*******************************************************************************************
    		 * GUI for panel4. It creates the JTextfield and JPanel for
    		 * piece 1's voting input.
    		 *******************************************************************************************/
    		JPanel panel4 = new JPanel();
    		//JTextField textlabel4_in = new JTextField(5);
    		panel4.add(textlabel4_in);
    		textlabel4_in.setText("" + 0 + "");
    		//vote1_string = textlabel4_in.getText();
    		//vote1 = Integer.parseInt(vote1_string);
    		/*******************************************************************************************
    		 * GUI for panel5. It creates the JTextfield and JPanel for
    		 * piece 2's voting input.
    		 *******************************************************************************************/
    		JPanel panel5 = new JPanel();
    		//JTextField textlabel5_in = new JTextField(5);
    		panel5.add(textlabel5_in);
    		textlabel5_in.setText("" + 0 + "");
    		//vote2_string = textlabel5_in.getText();
    		//vote2 = Integer.parseInt(vote2_string);
    		/*******************************************************************************************
    		 * GUI for panel6. It creates the JTextfield and JPanel for
    		 * piece 3's voting input.
    		 *******************************************************************************************/
    		JPanel panel6 = new JPanel();
    		//JTextField textlabel6_in = new JTextField(5);
    		panel6.add(textlabel6_in);
    		textlabel6_in.setText("" + 0 + "");
    		//vote3_string = textlabel6_in.getText();
    		//vote3 = Integer.parseInt(vote3_string);
    		/*******************************************************************************************
    		 * GUI for panel7. It creates the radio button field for piece
    		 * 1.
    		 *******************************************************************************************/
    		JPanel panel7 = new JPanel();
    		// JRadioButton button1 = new JRadioButton("", false);
    		panel7.add(button1);
    		/*******************************************************************************************
    		 * GUI for panel8. It creates the radio button field for piece
    		 * 2.
    		 *******************************************************************************************/
    		JPanel panel8 = new JPanel();
    		// JRadioButton button2 = new JRadioButton("", false);
    		panel8.add(button2);
    		/*******************************************************************************************
    		 * GUI for panel9. It creates the radio button field for piece
    		 * 3.
    		 *******************************************************************************************/
    		JPanel panel9 = new JPanel();
    		// JRadioButton button3 = new JRadioButton("", false);
    		panel9.add(button3);
    		/*******************************************************************************************
    		 * The panelmain_votes, holds all of the panels above, creating
    		 * the "top" portion of the GUI. Panels are organized in their
    		 * respective sets for the Jlabel, textfield, and radio button.
    		 * Panels 1, 4, and 7 are the components for piece 1. 
    		 * Panels 2, 5, and 8 are the components for piece 2. 
    		 * Panels 3, 6, and 9 are the components for piece 3.
    		 *******************************************************************************************/
    		panelmain_votes.setLayout(new GridLayout(3, 3));
    		panelmain_votes.add(panel1);
    		panelmain_votes.add(panel4);
    		panelmain_votes.add(panel7);
    		panelmain_votes.add(panel2);
    		panelmain_votes.add(panel5);
    		panelmain_votes.add(panel8);
    		panelmain_votes.add(panel3);
    		panelmain_votes.add(panel6);
    		panelmain_votes.add(panel9);
    		/*******************************************************************************************
    		 * GUI for panelmain_button. It creates the button "Calculate".
    		 *******************************************************************************************/
    		JButton button;
    		JPanel panel_button = new JPanel();
    		button = new JButton("Calculate");
    		panel_button.add(button);
    		button.addActionListener(this);
    		/*******************************************************************************************
    		 * The JLabel below creates a JLbael that displays who the
    		 * winner is.
    		 *******************************************************************************************/
    		
    		panel_button.add(jtextlabel_winner);
    		/*******************************************************************************************
    		 * The panelmain_button holds the Calculate button as well as
    		 * the winner text and creates the "bottom" portion of the
    		 * overall GUI.
    		 *******************************************************************************************/
    		panelmain_button.setLayout(new GridLayout(1, 2));
    		panelmain_button.add(button);
    		panelmain_button.add(jtextlabel_winner);
    		/*******************************************************************************************
    		 * panelmain adds the "top" section of the GUI (panelmain_votes), with the "bottom" section 
    		 * of the GUI (panelmain_button); to create the overall GUI.
    		 *******************************************************************************************/
    		panelmain.add(panelmain_votes);
    		panelmain.add(panelmain_button);
    		/*******************************************************************************************
    		 * panelmain is added to the frame here, and is then packed,
    		 * and set visible for client use.
    		 *******************************************************************************************/
    		frame.getContentPane().add(panelmain);
    		frame.pack();
    		frame.setVisible(true);
    	
    	}
    	public void actionPerformed(ActionEvent e) {
    
    		vote1_string = textlabel4_in.getText();
    		vote1 = Integer.parseInt(vote1_string);
    		vote2_string = textlabel5_in.getText();
    		vote2 = Integer.parseInt(vote2_string);
    		vote3_string = textlabel6_in.getText();
    		vote3 = Integer.parseInt(vote3_string);
    		
    		if (vote1 > vote2) {
    			if (vote1 > vote3) {
    				winner = "piece 1!";
    			}
    		// button1 = ("", true);
    		}
    		if (vote2 > vote1) {
    			if (vote2 > vote3) {
    				winner = "piece 2!";
    			}
    		}
    		if (vote3 > vote1) {
    			if (vote3 > vote2) {
    				winner = "piece 3!";
    			}
    		}
    		if (vote1 == vote3) {
    			winner = "piece 1 and piece 3 tied!";
    		}
    		if (vote1 == vote2) {
    			winner = "piece 1 and piece 2 tied!";
    		}
    		if (vote2 == vote3) {
    			winner = "piece 2 and piece 3 tied!";
    		}
    		if (vote1 == vote2 && vote1 == vote3) {
    			winner = "piece 1, 2 and 3 tied!";
    		}
    		
    		jtextlabel_winner.equals("     And the winner is... " + winner);
    
    
    	}
    
    }
    /**
     * public void ArtPrizeGUI_end() {
     * 
     * JFrame frame = new JFrame(); JPanel panelmain = new JPanel();
     * panelmain.setBackground(Color.black); JPanel panel = new JPanel();
     * panel.setPreferredSize(new Dimension(200, 225));
     * 
     * JTextField textlabel1_out = new JTextField(16);
     * panel.add(textlabel1_out); textlabel1_out.setText(vote1);
     * 
     * panel.setBackground(Color.white);
     * 
     * frame.getContentPane().add(panelmain); frame.pack();
     * frame.setVisible(true); }
     */
    TY!!

    P.S. I'm using Eclipse for my environment, so maybe that will help in explaining things for me.

  11. #11
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default

    Here's what I did with your code.

    I made the ArtPrizeEngine class implement ActionListener.
    This means, among other things this class might be, it
    is also an ActionListener.

    As an ActionListener, it must have an
    "actionPerformed(ActionEvent e)" method.

    Usually the first thing that is done within
    an actionPerformed method is an analysis of
    the actionEvent it has recieved. By examining
    the actionEvent, many things can be discovered
    about how the actionEvent was created.

    Depending on its origin, the programmer will
    add code to produce the desired function for
    that actionEvent (calculations, etc.).

    I have simply given some components println
    statements that expose the guts of the
    actionEvent so you can see all the
    possibilities it provides.

    I have not completed your code, It does
    compile and it runs. Look carefully at how
    some things are accomplished.
    Consult the API for methods that access
    all that stuff in the actionEvent.


    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    /*******************************************************************************************
     * @author Austin Anderson
     * This program will take in the votes of three art pieces, and then calculate the winner.
     *******************************************************************************************/
    public class ArtPrizeEngine implements ActionListener{
    
      /*******************************************************************************************
       * Contains the variables used to track the votes for when finding the winner.
       *******************************************************************************************/
      int vote1 = 0, vote2 = 0, vote3 = 0;
    
      String vote1_string = "", vote2_string = "",
          vote3_string = "", winner = null;
    
      /*******************************************************************************************
       * The main method calls the GUI.
       *******************************************************************************************/
      public static void main(String arg[]) {
    
        ArtPrizeEngine t = new ArtPrizeEngine();
        t.ArtPrizeEngineGUI();
      }
    
    
      /*******************************************************************************************
       * The GUI below is the engine of the program, and the GUI itself. It will display the
       * voting GUI, count the entered votes, and then calculate the winner by displaying text
       * and checking the appropriate radio box.
       *******************************************************************************************/
      public void ArtPrizeEngineGUI() {
        /*******************************************************************************************
         * The frame and the three main panels used to build the overall GUI, are created here.
         *******************************************************************************************/
        JFrame frame = new JFrame("Art Prize Voter!");
        JPanel panelmain = new JPanel();
        JPanel panelmain_votes = new JPanel();
        JPanel panelmain_button = new JPanel();
        frame.setPreferredSize(new Dimension(400, 175));
        /*******************************************************************************************
         * GUI for panel1. It creates the JLabel and JPanel for
         * displaying the JLabel for piece # 1.
         *******************************************************************************************/
        JPanel panel1 = new JPanel();
        JLabel jtextlabel1 = new JLabel(
            "Enter your votes for piece # 1: ");
        panel1.add(jtextlabel1);
        /*******************************************************************************************
         * GUI for panel2. It creates the JLabel and JPanel for
         * displaying the JLabel for piece # 2.
         *******************************************************************************************/
        JPanel panel2 = new JPanel();
        JLabel jtextlabel2 = new JLabel(
            "Enter your votes for piece # 2: ");
        panel2.add(jtextlabel2);
        /*******************************************************************************************
         * GUI for panel3. It creates the JLabel and JPanel for
         * displaying the JLabel for piece # 3.
         *******************************************************************************************/
        JPanel panel3 = new JPanel();
        JLabel jtextlabel3 = new JLabel(
            "Enter your votes for piece # 3: ");
        panel3.add(jtextlabel3);
        /*******************************************************************************************
         * GUI for panel4. It creates the JTextfield and JPanel for
         * piece 1's voting input.
         *******************************************************************************************/
        JPanel panel4 = new JPanel();
        JTextField textlabel4_in = new JTextField(5);
        panel4.add(textlabel4_in);
        textlabel4_in.setText("" + 0 + "");
        vote1_string = textlabel4_in.getText();
        vote1 = Integer.parseInt(vote1_string);
        /*******************************************************************************************
         * GUI for panel5. It creates the JTextfield and JPanel for
         * piece 2's voting input.
         *******************************************************************************************/
        JPanel panel5 = new JPanel();
        JTextField textlabel5_in = new JTextField(5);
        panel5.add(textlabel5_in);
        textlabel5_in.setText("" + 0 + "");
        vote2_string = textlabel5_in.getText();
        vote2 = Integer.parseInt(vote2_string);
        /*******************************************************************************************
         * GUI for panel6. It creates the JTextfield and JPanel for
         * piece 3's voting input.
         *******************************************************************************************/
        JPanel panel6 = new JPanel();
        JTextField textlabel6_in = new JTextField(5);
        panel6.add(textlabel6_in);
        textlabel6_in.setText("" + 0 + "");
        vote3_string = textlabel6_in.getText();
        vote3 = Integer.parseInt(vote3_string);
        /*******************************************************************************************
         * GUI for panel7. It creates the radio button field for piece
         * 1.
         *******************************************************************************************/
        JPanel panel7 = new JPanel();
        JRadioButton button1 = new JRadioButton("", false);
        panel7.add(button1);
                    button1.setActionCommand("go");
                    button1.addActionListener(this);
        /*******************************************************************************************
         * GUI for panel8. It creates the radio button field for piece
         * 2.
         *******************************************************************************************/
        JPanel panel8 = new JPanel();
        JRadioButton button2 = new JRadioButton("", false);
        panel8.add(button2);
                    button2.addActionListener(this);
        /*******************************************************************************************
         * GUI for panel9. It creates the radio button field for piece
         * 3.
         *******************************************************************************************/
        JPanel panel9 = new JPanel();
        JRadioButton button3 = new JRadioButton("", false);
        panel9.add(button3);
        /*******************************************************************************************
         * The panelmain_votes, holds all of the panels above, creating
         * the "top" portion of the GUI. Panels are organized in their
         * respective sets for the Jlabel, textfield, and radio button.
         * Panels 1, 4, and 7 are the components for piece 1. 
         * Panels 2, 5, and 8 are the components for piece 2. 
         * Panels 3, 6, and 9 are the components for piece 3.
         *******************************************************************************************/
        panelmain_votes.setLayout(new GridLayout(3, 3));
        panelmain_votes.add(panel1);
        panelmain_votes.add(panel4);
        panelmain_votes.add(panel7);
        panelmain_votes.add(panel2);
        panelmain_votes.add(panel5);
        panelmain_votes.add(panel8);
        panelmain_votes.add(panel3);
        panelmain_votes.add(panel6);
        panelmain_votes.add(panel9);
        /*******************************************************************************************
         * GUI for panelmain_button. It creates the button "Calculate".
         *******************************************************************************************/
        JButton button;
        JPanel panel_button = new JPanel();
        button = new JButton("Calculate");
        panel_button.add(button);
        button.addActionListener(this);
        /*******************************************************************************************
         * The JLabel below creates a JLbael that displays who the
         * winner is.
         *******************************************************************************************/
        JLabel jtextlabel_winner = new JLabel(
            "     And the winner is... " + winner);
        panel_button.add(jtextlabel_winner);
        /*******************************************************************************************
         * The panelmain_button holds the Calculate button as well as
         * the winner text and creates the "bottom" portion of the
         * overall GUI.
         *******************************************************************************************/
        panelmain_button.setLayout(new GridLayout(1, 2));
        panelmain_button.add(button);
        panelmain_button.add(jtextlabel_winner);
        /*******************************************************************************************
         * panelmain adds the "top" section of the GUI (panelmain_votes), with the "bottom" section 
         * of the GUI (panelmain_button); to create the overall GUI.
         *******************************************************************************************/
        panelmain.add(panelmain_votes);
        panelmain.add(panelmain_button);
        /*******************************************************************************************
         * panelmain is added to the frame here, and is then packed,
         * and set visible for client use.
         *******************************************************************************************/
        frame.getContentPane().add(panelmain);
        frame.pack();
        frame.setVisible(true);
      }
    
    
    
      public void actionPerformed(ActionEvent e){
        String actionCommand = e.getActionCommand();
    
        if (e.getSource() instanceof JButton){
          if ("Calculate" .equals(actionCommand)){
            System.out.println("Calculate pressed");
            System.out.println(e + "\n");
          }
        }
        else if (e.getSource() instanceof JRadioButton){
          if ("" .equals(actionCommand)){
            System.out.println( e );
          }
          if ("go" .equals(actionCommand)){
            System.out.println( "The first button was pressed" );
          }
        }
      }
    }

  12. #12
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default My static void main(String[] args) error

    I am looking over your questions
    and I will make more suggestions
    on how to bring order to your code.

    In my first post, I accidentally included
    "static void main(String[] args)"
    as one of the lines you should remove
    "static" from.

    This was a mistake.
    The "main(String[] args)" method is
    the 'launching point' for your code, and
    it is ALWAYS static.

  13. #13
    aanders5 is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    if i turn it to static,
    public static void main(String arg[]) {
    ArtPrizeEngineGUI();
    }

    then the ArtPrizeEngineGUI(); gives an error, and whats the method itself to become static too....sooo?

  14. #14
    aanders5 is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    Ok, so I am looking over the code you posted. Gotta few Q's.

    1. Before you had marked to remove the three codes that converted my strings into ints for calculating,
    Java Code:
     vote#_string = textlabel#_in.getText();
     vote# = Integer.parseInt(vote#_string);
    But now you have them back in the code un-commented, were they needed now?

    2. How come "button1" has two lines of action listeners,
    Java Code:
    button1.setActionCommand("go");
    button1.addActionListener(this);
    but, the other two radio buttons only have this one line?
    (This is not the calculate button that runs the program, if that's what your doing)
    Java Code:
    button2.addActionListener(this);
    3. The whole action area,
    Java Code:
    public void actionPerformed(ActionEvent e)
    Um...whats going with that, and where do I insert the if statements to check for the highest number?
    Also, where/how do I update my JLabel so that it prints off
    Java Code:
    jtextlabel_winner.equals("     And the winner is... " + winner);
    Speaking of which, says it need a universal variable for "jtextlabel_winner", so can i put that back up to the top as a universal variable?

    4. I assume that within the action performed area, I will also put the code to update the checkbox(s) to become true, for the winner.
    What coding do I need for that? I searched around, and I am not finding anything for the radio boxes to get updated by code.



    So far, THANKS! I finally realize how/why the instantiation I used, failed. I kind of see how the action/listener stuff is working too. I am just confused on "this" and how the action listener is linked to the actionpreformed (unless that's just something JAVA does on it its own).

    But thank you so far.



    Again, the user enters in some amount of votes in the text fields.
    You hit calculate
    The result is, the highest number will auto check the corresponding radio box (or boxes, if its a tie), then update winner (set to null at the moment) to either piece 1, 2, or 3 (or multiple if tied).
    Last edited by aanders5; 10-15-2010 at 09:34 PM.

  15. #15
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default ActionListener issues

    1. Before you had marked to remove the three codes
    that converted my strings into ints for calculating,

    Code:
    vote#_string = textlabel#_in.getText();
    vote# = Integer.parseInt(vote#_string);

    But now you have them back in the code un-commented,
    were they needed now?

    I looked at this code more closely.
    It seems to be in the catagory of
    GUI initalization, not data
    calculation as I first thought.

    Bottom line, it seems non-essentual
    to the primary issue; working with
    actionListeners.

    2. How much "button1" has two lines of acrtion listeners,

    Code:
    button1.setActionCommand("go");
    button1.addActionListener(this);

    but, the other two radio buttons only have this one line?

    Code:
    button2.addActionListener(this);

    I added actionListeners to SOME
    buttons in your code so you would
    see what it takes to make these
    puppies work.

    button1.setActionCommand("go");

    sets an internal String value in
    these components which we can use
    in the actionPerformed(ActionEvent e)
    method to determine which component
    created the actionEvent.

    If you do not set this value, by
    default, it will be assigned the
    'Label' value of the component.
    Your "Calculate button" has the
    string "Calculate" as this value.
    Check it out. It's tested for in
    the actionPerformed(ActionEvent e)
    method.

    "go" was assigned to the command
    string of button1, so guess what
    string we should test for to
    determine if button1 has been
    pressed?

    staticstaticstaticstaticstaticstaticstaticstatic

    So much for answers specific to the
    actionListener.
    I will modify your code a few more
    times, within the context of this
    subject to help clarify it further.

    As I reviewed your code, I realized
    There were at least three BIG issues
    you needed help with. I know I can
    cover only one at a time.

    "static" is a subject you must
    investigate. Do not try to associate
    "static" with the main(String[] args)
    method.
    The purpose of this method is to
    specialized to divine universal
    knowledge about "static" by studying
    it.

    On the other hand, "static" has
    deep universal purpose in java, and
    you do not start plugging it into
    lines of code because the compiler
    starts crying about it. Programmers
    know you don't know when they see
    values labeled as "static" that
    shouldn't be.

    "static" is a topic you have to open
    the book up and study.

    staticstaticstaticstaticstaticstaticstaticstatic

    3. The whole action area,

    Code:
    public void actionPerformed(ActionEvent e)

    Um...whats going with that, and where do I insert the if
    statements to check for the highest number?
    Also, where/how do I update my JLabel so that it prints off

    Code:
    jtextlabel_winner.equals(" And the winner is... " + winner);

    Speaking of which, says I need a universal variable for
    "jtextlabel_winner"

    4. I assume that within the action performed area, I will
    also put the code to update the checkbox to become true,
    for the winner.
    What coding do I need for that?
    I searched around, and I am not finding
    anything for the radio boxes to get updated by code.

    This is the area where you begin
    your calculations, and have them
    call other methods and data from
    wherever in your project, as you
    need them.

    I'm not commenting on this section
    because it's your algorithm. I'm
    not done with your actionListener
    question(s).


    So far, THANKS! I finally realize how/why the instantiation
    I used, failed.
    I kind of see how the action/listener stuff is working too.
    I am just confused on "this" and how the action listener is
    linked to the actionpreformed (unless that's just something
    JAVA does on it its own).

    This is the third big issue you
    will have to understand about java.
    Java is event driven.
    You do not linearly progress through
    the code. An event occurs, and
    somewhere in your code, there is a
    method that responds. That's why
    there are objects called "eventListeners"
    in Java.

  16. #16
    aanders5 is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    Ok, so what about the radiobuttons then?
    I have looked around for that, but I don't see what I have to put into the action area that will change/update the status of checkbox from false to true for the inner.

    I did see something called an ItemListener though when I was searching around, does that have to deal with this issue?

  17. #17
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default

    Ok, so what about the radiobuttons then?
    I have looked around for that, but I don't
    see what I have to put into the action area
    that will change/update the status of
    checkbox from false to true for the inner.

    The checkboxes are treated the same way the
    buttons are in the actionListener.

    ==========

    In the following code, the ArtPrizeEngine class
    no longer serves as an actionListener. Now it
    can be considered as a GUI with the public
    variables:

    int vote1, vote2, vote3
    String vote1_string, vote2_string, vote3_string, winner

    The actionListener is now a seperate class named
    MyActionListener.

    Notice "implements ActionListener" no longer
    follows "ArtPrizeEngine" in its class name,
    and "implements ActionListener" is added
    to the "MyActionListener's" name.

    This code will compile and it will work.

    A problem it has is that it cannot access variables
    in the ArtPrizeEngine class.

    This is fixed in the next example.


    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    /*******************************************************************************************
     * @author Austin Anderson
     * This program will take in the votes of three art pieces, and then calculate the winner.
     *******************************************************************************************/
    public class ArtPrizeEngine{
    
      /*******************************************************************************************
       * Contains the variables used to track the votes for when finding the winner.
       *******************************************************************************************/
      int vote1 = 0, vote2 = 0, vote3 = 0;
    
      String vote1_string = "", vote2_string = "", vote3_string = "", winner = null;
    
      /*******************************************************************************************
       * The main method calls the GUI.
       *******************************************************************************************/
      public static void main(String arg[]) {
        ArtPrizeEngine t = new ArtPrizeEngine();
        t.ArtPrizeEngineGUI();
      }
    
      /*******************************************************************************************
       * The GUI below is the engine of the program, and the GUI itself. It will display the
       * voting GUI, count the entered votes, and then calculate the winner by displaying text
       * and checking the appropriate radio box.
       *******************************************************************************************/
      public void ArtPrizeEngineGUI() {
        /*******************************************************************************************
         * The frame and the three main panels used to build the overall GUI, are created here.
         *******************************************************************************************/
        JFrame frame = new JFrame("Art Prize Voter!");
        JPanel panelmain = new JPanel();
        JPanel panelmain_votes = new JPanel();
        JPanel panelmain_button = new JPanel();
        frame.setPreferredSize(new Dimension(400, 175));
        /*******************************************************************************************
         * GUI for panel1. It creates the JLabel and JPanel for
         * displaying the JLabel for piece # 1.
         *******************************************************************************************/
        JPanel panel1 = new JPanel();
        JLabel jtextlabel1 = new JLabel("Enter your votes for piece # 1: ");
        panel1.add(jtextlabel1);
        /*******************************************************************************************
         * GUI for panel2. It creates the JLabel and JPanel for
         * displaying the JLabel for piece # 2.
         *******************************************************************************************/
        JPanel panel2 = new JPanel();
        JLabel jtextlabel2 = new JLabel("Enter your votes for piece # 2: ");
        panel2.add(jtextlabel2);
        /*******************************************************************************************
         * GUI for panel3. It creates the JLabel and JPanel for
         * displaying the JLabel for piece # 3.
         *******************************************************************************************/
        JPanel panel3 = new JPanel();
        JLabel jtextlabel3 = new JLabel("Enter your votes for piece # 3: ");
        panel3.add(jtextlabel3);
        /*******************************************************************************************
         * GUI for panel4. It creates the JTextfield and JPanel for
         * piece 1's voting input.
         *******************************************************************************************/
        JPanel panel4 = new JPanel();
        JTextField textlabel4_in = new JTextField(5);
        panel4.add(textlabel4_in);
        textlabel4_in.setText("" + 0 + "");
        vote1_string = textlabel4_in.getText();
        vote1 = Integer.parseInt(vote1_string);
        /*******************************************************************************************
         * GUI for panel5. It creates the JTextfield and JPanel for
         * piece 2's voting input.
         *******************************************************************************************/
        JPanel panel5 = new JPanel();
        JTextField textlabel5_in = new JTextField(5);
        panel5.add(textlabel5_in);
        textlabel5_in.setText("" + 0 + "");
        vote2_string = textlabel5_in.getText();
        vote2 = Integer.parseInt(vote2_string);
        /*******************************************************************************************
         * GUI for panel6. It creates the JTextfield and JPanel for
         * piece 3's voting input.
         *******************************************************************************************/
        JPanel panel6 = new JPanel();
        JTextField textlabel6_in = new JTextField(5);
        panel6.add(textlabel6_in);
        textlabel6_in.setText("" + 0 + "");
        vote3_string = textlabel6_in.getText();
        vote3 = Integer.parseInt(vote3_string);
        /*******************************************************************************************
         * GUI for panel7. It creates the radio button field for piece
         * 1.
         *******************************************************************************************/
        JPanel panel7 = new JPanel();
        JRadioButton button1 = new JRadioButton("", false);
        panel7.add(button1);
        button1.setActionCommand("Moe");
        button1.addActionListener(new MyActionListener());
        /*******************************************************************************************
         * GUI for panel8. It creates the radio button field for piece
         * 2.
         *******************************************************************************************/
        JPanel panel8 = new JPanel();
        JRadioButton button2 = new JRadioButton("", false);
        panel8.add(button2);
        button2.setActionCommand("Jerry");
        button2.addActionListener(new MyActionListener());
        /*******************************************************************************************
         * GUI for panel9. It creates the radio button field for piece
         * 3.
         *******************************************************************************************/
        JPanel panel9 = new JPanel();
        JRadioButton button3 = new JRadioButton("", false);
        button3.setActionCommand("Curly");
        button3.addActionListener(new MyActionListener());
        panel9.add(button3);
        /*******************************************************************************************
         * The panelmain_votes, holds all of the panels above, creating
         * the "top" portion of the GUI. Panels are organized in their
         * respective sets for the Jlabel, textfield, and radio button.
         * Panels 1, 4, and 7 are the components for piece 1. 
         * Panels 2, 5, and 8 are the components for piece 2. 
         * Panels 3, 6, and 9 are the components for piece 3.
         *******************************************************************************************/
        panelmain_votes.setLayout(new GridLayout(3, 3));
        panelmain_votes.add(panel1);
        panelmain_votes.add(panel4);
        panelmain_votes.add(panel7);
        panelmain_votes.add(panel2);
        panelmain_votes.add(panel5);
        panelmain_votes.add(panel8);
        panelmain_votes.add(panel3);
        panelmain_votes.add(panel6);
        panelmain_votes.add(panel9);
        /*******************************************************************************************
         * GUI for panelmain_button. It creates the button "Calculate".
         *******************************************************************************************/
        JButton button;
        JPanel panel_button = new JPanel();
        button = new JButton("Calculate");
        panel_button.add(button);
        button.addActionListener(new MyActionListener());
        /*******************************************************************************************
         * The JLabel below creates a JLbael that displays who the
         * winner is.
         *******************************************************************************************/
        JLabel jtextlabel_winner = new JLabel("     And the winner is... " + winner);
        panel_button.add(jtextlabel_winner);
        /*******************************************************************************************
         * The panelmain_button holds the Calculate button as well as
         * the winner text and creates the "bottom" portion of the
         * overall GUI.
         *******************************************************************************************/
        panelmain_button.setLayout(new GridLayout(1, 2));
        panelmain_button.add(button);
        panelmain_button.add(jtextlabel_winner);
        /*******************************************************************************************
         * panelmain adds the "top" section of the GUI (panelmain_votes), with the "bottom" section 
         * of the GUI (panelmain_button); to create the overall GUI.
         *******************************************************************************************/
        panelmain.add(panelmain_votes);
        panelmain.add(panelmain_button);
        /*******************************************************************************************
         * panelmain is added to the frame here, and is then packed,
         * and set visible for client use.
         *******************************************************************************************/
        frame.getContentPane().add(panelmain);
        frame.pack();
        frame.setVisible(true);
      }
    }
    
    
    class MyActionListener implements ActionListener{
      public void actionPerformed(ActionEvent e){
        String actionCommand = e.getActionCommand();
    
        if (e.getSource() instanceof JButton){
          if ("Calculate" .equals(actionCommand)){
            System.out.println("Calculate pressed");
          }
        }
        else if (e.getSource() instanceof JRadioButton){
          if ("Moe" .equals(actionCommand)){
            System.out.println( "The 1st button was pressed" );
          }
          else if ("Larry" .equals(actionCommand)){
            System.out.println( "The 2nd button was pressed" );
          }
          else if ("Curly" .equals(actionCommand)){
            System.out.println( "The 3rd button was pressed" );
          }
        }
      }
    }

    An important difference in the
    following (and final) MyActionListener
    class, listed below, is the way the
    actionListener is added to each
    button in the ArtPrizeEngine.

    To conserve space, I'm not printing
    the ArtPrizeEngine code again. Just
    replace the old with the new shown
    below.

    the old way:

    Java Code:
        button1.addActionListener(new MyActionListener());
    the new way

    Java Code:
        button1.addActionListener(new MyActionListener(this));
    "(this)" in the addActionListener method above
    gives the MyActionListener a referrence to the
    ArtPrizeEngine object. Through this reference the code
    in the MyActionListener can access the variables in
    the ArtPrizeEngine object (this makes them glow-ball!).

    Compile and run the code. Play with the buttons! You'll
    see that it's true!

    Details:
    If you examine the code in the MyActionListener, you will
    see the variables in the ArtPrizeEngine object are
    referenced by the MyActionListener as:

    src.vote1, src.vote2, and src.vote3




    Java Code:
    class MyActionListener implements ActionListener{
    
      ArtPrizeEngine src;
      MyActionListener(){};                        // Constructors!!
      MyActionListener(ArtPrizeEngine source){     // another new
        src = source;                              // topic!!
      }
    
      public void actionPerformed(ActionEvent e){
        String actionCommand = e.getActionCommand();
    
        if (e.getSource() instanceof JButton){
          if ("Calculate" .equals(actionCommand)){
            System.out.println("Calculate pressed");
            int temp = src.vote1 + src.vote2 + src.vote3;
            System.out.println("Sum: " + temp);
          }
        }
        else if (e.getSource() instanceof JRadioButton){
          if ("Moe" .equals(actionCommand)){
            System.out.println( "The 1st button was pressed" );
            src.vote1++;
          }
          else if ("Larry" .equals(actionCommand)){
            System.out.println( "The 2nd button was pressed" );
            src.vote2++;
          }
          else if ("Curly" .equals(actionCommand)){
            System.out.println( "The 3rd button was pressed" );
            src.vote3--;
          }
        }
      }
    }

  18. #18
    aanders5 is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    So, I added the code above into a new project to see how it works...and nothing happens.
    Is hitting Calculate with the above code, suppose to do something?

  19. #19
    aanders5 is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    wait...when i press the buttons its printing stuff out.... I am not sure if you know what I am trying to do...

    The radio buttons serve absolutely no purpose at all in this project, except to be marked true when the winner is found.

    So if piece one has 30 votes, piece two has 10 votes, and piece three has 50 votes. The "..null" text becomes "piece three has won!", and the radio box next to piece three is marked true.

    The user is not at any time actually pushing the radio buttons, it is just solely there for us to learn how they work, and for the engine to auto mark the winner.

  20. #20
    aanders5 is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    I also was checking something, and so I ran the debugger, and the program never went into the actionpreformed class to check my if statements, it just stopped and displayed the GUI after going through ArtPrizeGUI class.

Page 1 of 2 12 LastLast

Similar Threads

  1. Action Listener
    By greatmajestics in forum AWT / Swing
    Replies: 8
    Last Post: 03-25-2010, 06:39 PM
  2. Accessing objects from within Action listener
    By cog in forum New To Java
    Replies: 4
    Last Post: 12-24-2009, 09:17 PM
  3. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 09:51 AM
  4. action listener on jcombobox
    By chkm8 in forum New To Java
    Replies: 2
    Last Post: 02-05-2009, 11:14 AM
  5. [SOLVED] action listener and Jtextfield
    By tOpach in forum AWT / Swing
    Replies: 4
    Last Post: 12-16-2008, 02:02 PM

Posting Permissions

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