Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Radio Button Help Please!

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    37
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Radio Button Help Please!

    I am writing a program that prompts a user to enter the name of an item, the price of the item, and the percentage discount on that item. The program then calculates the sale price and spits that out in a display box.

    The program is also supposed to contain a menu that lists "departments" that the user can choose from. The menu needs to be either in JComboBox format or using radio buttons. I failed miserably at getting the JComboBox option to work, so I did it using radio buttons.

    When I run the program in NetBeans it works like a champ. The only issue is, NetBeans IDE is indicating a warning on each of the 5 radio button lines. The warning message is "Local variable hides a field". Using the Alt-Enter help feature, a menun pops up that says "Rename the local variable" and there are 3 sub-menu options where I can either Disable "Local variable hides a field" hint, Configure "Local variable hides a field" hint, or Suppress Warning - LocalVariableHidesMemberVariable.

    i renamed the variables, and all that accomplished was renaming the variables. The warning messages still exist. I don't want to hide or suppress the warnings because that doesn't fix the underlying problem. Eventually, I want the radio buttons to actually work and not just appear on the menu.

    So my question is, why am I getting this warning message, and how do I fix it?!

    Please...If you respond, keep it WAY simple. I am not a Java developer! I am building this thing based on what I am reading in a book and finding online. So I am very new to this thing. Any other suggestions you may have to make thing thing more accurate/efficient/professional would be greatly appreciated.

    Here is the code:

    package retail.price.calculator;
     
    import java.awt.BorderLayout;
    import java.awt.event.*;
    import javax.swing.*;
     
       /**
        * Author: Rick Sebastian
        */	 
    	public class RetailPriceCalculator extends JFrame
    	{
                private JPanel panel;
                private JPanel selectedDeptPanel; //To hold department info
                private JPanel deptList; // A list of departments
                private JTextField selectedDept; // The selected department
                private JLabel label; // To display a message
                private JRadioButton meats, dairy, bakery, produce, pharmacy;
                private JLabel messageLabelA;
                private JTextField productNameTextField; 
    	    private JLabel messageLabelB;
                private JTextField originalPriceTextField;
                private JLabel messageLabelC;
                private JTextField discountPercentageTextField;
    	    private JButton calcButton;
                public RetailPriceCalculator()
     
     
                    {
                    setTitle("Retail Price Calculator"); // Display name for frame
                    setLayout (new BorderLayout());
                    setSize(950,200); // Sets display box size to 400x400 pixels
    	        setLocationRelativeTo(null); // Centers frame on the screen
                    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Ensures program terminates on exit
    	        buildPanel(); // Builds the main panel
    	        add(panel, BorderLayout.CENTER);   // Centers items in the panel
                    setVisible(true); // makes it visible
     
                    JPanel p = new JPanel(); // Creates the panel for the radio buttons
                    JRadioButtonMenuItem meats = new JRadioButtonMenuItem();
                    meats.setText("Meats");
                    JRadioButtonMenuItem dairy = new JRadioButtonMenuItem();
                    dairy.setText("Dairy");
                    JRadioButtonMenuItem bakery = new JRadioButtonMenuItem();
                    bakery.setText("Bakery");
                    JRadioButtonMenuItem produce = new JRadioButtonMenuItem();
                    produce.setText("Produce");
                    JRadioButtonMenuItem pharmacy = new JRadioButtonMenuItem();
                    pharmacy.setText("Pharmacy");
     
                    ButtonGroup group = new ButtonGroup();
                    group.add(meats);
                    group.add(dairy);
                    group.add(bakery);
                    group.add(produce);
                    group.add(pharmacy);
     
                    panel.add(meats);
                    panel.add(dairy);
                    panel.add(bakery);
                    panel.add(produce);
                    panel.add(pharmacy);
     
    	    }
     
    	    private void buildPanel()
    	    {
                    messageLabelA = new JLabel("Enter the product name");
    	        productNameTextField = new JTextField(10);
    	        messageLabelB = new JLabel("Enter the original price");
    	        originalPriceTextField = new JTextField(10);
                    messageLabelC = new JLabel("Enter the percent to discount");
    	        discountPercentageTextField = new JTextField(10);
    	        calcButton = new JButton ("Calculate");
    	        calcButton.addActionListener(new CalcButtonListener());
    	        panel = new JPanel();
                    panel.add(messageLabelA);
                    panel.add(productNameTextField);
    	        panel.add(messageLabelB);
                    panel.add(originalPriceTextField);
    	        panel.add(messageLabelC);
                    panel.add(discountPercentageTextField);
    	        panel.add(calcButton);
     
                    JTextArea textInput = new JTextArea(20, 40);
                    JScrollPane scrollPane = new JScrollPane(textInput);
     
    	    }
     
    	    private class CalcButtonListener implements ActionListener
    	    {
     
    	        public void actionPerformed(ActionEvent e)
    	        {
    	            String productNameInput;
                        String originalPriceInput;
    	            String discountPercentageInput;
    	            double originalPrice;
    	            double discountPercentage;
    	            double salePrice;
     
    	            productNameInput = productNameTextField.getText();
                        originalPriceInput = originalPriceTextField.getText();
    	            originalPrice = Double.parseDouble(originalPriceInput);
    	            discountPercentageInput = discountPercentageTextField.getText();
    	            discountPercentage = Double.parseDouble(discountPercentageInput);
     
    	            salePrice = originalPrice - ((discountPercentage * originalPrice) * .01);
    	            JOptionPane.showMessageDialog(null, 
                                "The Sale Price of your " + productNameInput + " is $ "+ salePrice);
    	        }
    	    }
     
    	    public static void main(String[] args)
    	    {
                   new RetailPriceCalculator();
    	    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,349
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

    Default Re: Radio Button Help Please!

    The warning message is "Local variable hides a field
    What is the name of the local variable and the name of the field that is hidden?
    Do you have variables with the same name defined in two places: one local to a method and one in the class?
    The local variable will "hide" the class variable.

    The solution is to only define the variable in the class and not in the method so it can be used in all the methods of the class.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Rick Sebastian (March 3rd, 2013)

  4. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Radio Button Help Please!

    You are re-declaring class fields (the JRadioButton variables) in the constructor. Stop doing that. Instead declare them in the class, and initialize them in the constructor or the class, but don't re-declare them in the constructor. Doing this will leave the class fields null, and you don't want that.

  5. The Following User Says Thank You to curmudgeon For This Useful Post:

    Rick Sebastian (March 3rd, 2013)

  6. #4
    Member
    Join Date
    Jan 2013
    Posts
    37
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Radio Button Help Please!

    Norm, curmudgeon - Guys, thanks so much. The error was obvious once you pointed it out.

    Appreciate the help!!

  7. #5
    Member
    Join Date
    Jan 2013
    Posts
    37
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Radio Button Help Please!

    One more question if I may...

    When the calculate button is clicked, the output window needs to reflect 4 items:

    1. The item name
    2. The department it came from (the radio button selection)
    3. The original price
    4. The sale price

    I have everything but item 2 (the radio button selection) included so far.

    Can you tell me what I need to do to get the radio button selection included in the output please?

    Thanks again for all your help.

    Here's the latest code again just in case:

    package retail.price.calculator;
     
    import java.awt.BorderLayout;
    import java.awt.event.*;
    import javax.swing.*;
     
       /**
        * Author: Rick Sebastian
        */	 
    	public class RetailPriceCalculator extends JFrame
    	{
                private JPanel panel;
                private JPanel selectedDeptPanel; //To hold department info
                private JPanel deptList; // A list of departments
                private JTextField selectedDept; // The selected department
                private JLabel label; // To display a message
                private JLabel messageLabelA;
                private JTextField productNameTextField; 
    	    private JLabel messageLabelB;
                private JTextField originalPriceTextField;
                private JLabel messageLabelC;
                private JTextField discountPercentageTextField;
    	    private JButton calcButton;
                public RetailPriceCalculator()
     
     
                    {
                    setTitle("Retail Price Calculator"); // Display name for frame
                    setLayout (new BorderLayout());
                    setSize(375,400); // Sets display box size to 400x400 pixels
    	        setLocationRelativeTo(null); // Centers frame on the screen
                    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Ensures program terminates on exit
    	        buildPanel(); // Builds the main panel
    	        add(panel, BorderLayout.CENTER);   // Centers items in the panel
                    setVisible(true); // makes it visible
     
                    JRadioButtonMenuItem meats = new JRadioButtonMenuItem("Meats", true); //Defines the meat radio button and sets it to default
                    JRadioButtonMenuItem dairy = new JRadioButtonMenuItem("Dairy"); //Defines the dairy radio button
                    JRadioButtonMenuItem bakery = new JRadioButtonMenuItem("Bakery"); //Defines the bakery radio button
                    JRadioButtonMenuItem produce = new JRadioButtonMenuItem("Produce"); //Defines the produce radio button
                    JRadioButtonMenuItem pharmacy = new JRadioButtonMenuItem("Pharmacy"); //Defines the pharmacy radio button
     
                    ButtonGroup group = new ButtonGroup(); //Defines a button group to group buttons together
                    group.add(meats); // Adds the meats button to the group
                    group.add(dairy); // Adds the dairy button to the group
                    group.add(bakery); // Adds the bakery button to the group
                    group.add(produce); // Adds the produce button to the group
                    group.add(pharmacy); // Adds the pharmacy button to the group
     
                    panel.add(meats); // Adds the meats button to the outwindow
                    panel.add(dairy); // Adds the dairy button to the output window
                    panel.add(bakery); // Adds the bakery button to the output window
                    panel.add(produce); // Adds the produce button to the output window
                    panel.add(pharmacy); // Adds the pharmacy button to the output window
     
     
    	    }
     
    	    private void buildPanel()
    	    {
                    JTextArea textInput = new JTextArea("Your Order Summary:",10, 30);
     
                    messageLabelA = new JLabel("Enter the product name");
    	        productNameTextField = new JTextField(10);
    	        messageLabelB = new JLabel("Enter the original price");
    	        originalPriceTextField = new JTextField(10);
                    messageLabelC = new JLabel("Enter the percent to discount");
    	        discountPercentageTextField = new JTextField(10);
    	        calcButton = new JButton ("Calculate");
    	        calcButton.addActionListener(new CalcButtonListener());
    	        panel = new JPanel();
                    panel.add(messageLabelA);
                    panel.add(productNameTextField);
    	        panel.add(messageLabelB);
                    panel.add(originalPriceTextField);
    	        panel.add(messageLabelC);
                    panel.add(discountPercentageTextField);
    	        panel.add(calcButton);
                    panel.add(textInput);               
     
     
    	    }
     
    	    private class CalcButtonListener implements ActionListener
    	    {
     
                    @Override
    	        public void actionPerformed(ActionEvent e)
    	        {
    	            String productNameInput;
                        String originalPriceInput;
    	            String discountPercentageInput;
    	            double originalPrice;
    	            double discountPercentage;
    	            double salePrice;
                        productNameInput = productNameTextField.getText();
                        originalPriceInput = originalPriceTextField.getText();
    	            originalPrice = Double.parseDouble(originalPriceInput);
    	            discountPercentageInput = discountPercentageTextField.getText();
    	            discountPercentage = Double.parseDouble(discountPercentageInput);
     
    	            salePrice = originalPrice - ((discountPercentage * originalPrice) * .01);
    	            JOptionPane.showMessageDialog(null, 
                                "The Sale Price of your " + productNameInput + " with an original price of " 
                                + originalPriceInput + " is now only $ " + salePrice + "!!");
    	        }
    	    }
     
    	    public static void main(String[] args)
    	    {
                    RetailPriceCalculator retailPriceCalculator = new RetailPriceCalculator();
    	    }
    }

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,349
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

    Default Re: Radio Button Help Please!

    Look at the ButtonGroup class. It probably has a way to get a reference to the selected radio button.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #7
    Member
    Join Date
    Jan 2013
    Posts
    37
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Radio Button Help Please!

    I used the JRadioButtonMenuItem name in the output string *assuming* it would identify the selected JRadioButtonMenuItem... That didn't work.

    So unless I'm just not formatting the expression properly, I really don't know what else to include.

    Here's what I had originally...

    "The Sale Price of your " + productNameInput + " from the " + JRadioButtonMenuItem + " with an original price of " + originalPriceInput + " is $ "+ salePrice);

  10. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Radio Button Help Please!

    Again why don't you do what Norm suggested -- use your ButtonGroup object, group?

  11. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,349
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

    Default Re: Radio Button Help Please!

    I can't find a way to get to the JRadioButton using the ButtonGroup. Is there a path through the classes and methods to get it?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #10
    Member
    Join Date
    Jan 2013
    Posts
    37
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Radio Button Help Please!

    I tried adding the word "group" to the statement too and it didn't recognize it in the expression.

    I simply do not understand the syntax of the expression I need.

    Would it be + group.add(meats) or group.add(dairy) or etc... + ??

    I have no idea... I have tried every variation I can think of.

  13. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,349
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

    Default Re: Radio Button Help Please!

    What you want is the JRadioButton that is selected. The ButtonGroup the JRadioButton is in can be used to get data from the JRadioButton that is selected.

    it didn't recognize it
    Is the variable defined in the correct scope so other methods can see it? See post#2
    If you don't understand my answer, don't ignore it, ask a question.

  14. #12
    Member
    Join Date
    Jan 2013
    Posts
    37
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Radio Button Help Please!

    The ButtonGroup the JRadiobutton is in is called "group". But if I plug the word "group" into the output string it doesn't recognize it.

    Do I have to declare "group" as a variable maybe first??

    I am so confused....

  15. #13
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Radio Button Help Please!

    Again is the group variable in the correct scope? that was the issue of your first question. Where is the group variable declared? In the class or in a constructor or method?

  16. #14
    Member
    Join Date
    Jan 2013
    Posts
    37
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Radio Button Help Please!

    I removed the line - private JRadioButton meats, dairy, bakery, produce, pharmacy; from the original code as you indicated I should and I left the variables in the button statements as follows:

                    JRadioButtonMenuItem meats = new JRadioButtonMenuItem("Meats", true); //Defines the meat radio button and sets it to default
                    JRadioButtonMenuItem dairy = new JRadioButtonMenuItem("Dairy"); //Defines the dairy radio button
                    JRadioButtonMenuItem bakery = new JRadioButtonMenuItem("Bakery"); //Defines the bakery radio button
                    JRadioButtonMenuItem produce = new JRadioButtonMenuItem("Produce"); //Defines the produce radio button
                    JRadioButtonMenuItem pharmacy = new JRadioButtonMenuItem("Pharmacy"); //Defines the pharmacy radio button
     
                    ButtonGroup group = new ButtonGroup(); //Defines a button group to group buttons together
                    group.add(meats); // Adds the meats button to the group
                    group.add(dairy); // Adds the dairy button to the group
                    group.add(bakery); // Adds the bakery button to the group
                    group.add(produce); // Adds the produce button to the group
                    group.add(pharmacy); // Adds the pharmacy button to the group
     
                    panel.add(meats); // Adds the meats button to the outwindow
                    panel.add(dairy); // Adds the dairy button to the output window
                    panel.add(bakery); // Adds the bakery button to the output window
                    panel.add(produce); // Adds the produce button to the output window
                    panel.add(pharmacy); // Adds the pharmacy button to the output window
     
     
    	    }

    Do I need to take the variables back out of the button statements and re-insert the private JRadioButton meats, dairy, bakery, produce, pharmacy; statement in order to get the variable to print out with the output??

  17. #15
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Radio Button Help Please!

    Quote Originally Posted by Rick Sebastian View Post
    I removed the line - private JRadioButton meats, dairy, bakery, produce, pharmacy; from the original code as you indicated I should and I left the variables in the button statements as follows:

                    JRadioButtonMenuItem meats = new JRadioButtonMenuItem("Meats", true); //Defines the meat radio button and sets it to default
                    JRadioButtonMenuItem dairy = new JRadioButtonMenuItem("Dairy"); //Defines the dairy radio button
                    JRadioButtonMenuItem bakery = new JRadioButtonMenuItem("Bakery"); //Defines the bakery radio button
                    JRadioButtonMenuItem produce = new JRadioButtonMenuItem("Produce"); //Defines the produce radio button
                    JRadioButtonMenuItem pharmacy = new JRadioButtonMenuItem("Pharmacy"); //Defines the pharmacy radio button
     
                    ButtonGroup group = new ButtonGroup(); //Defines a button group to group buttons together
                    group.add(meats); // Adds the meats button to the group
                    group.add(dairy); // Adds the dairy button to the group
                    group.add(bakery); // Adds the bakery button to the group
                    group.add(produce); // Adds the produce button to the group
                    group.add(pharmacy); // Adds the pharmacy button to the group
     
                    panel.add(meats); // Adds the meats button to the outwindow
                    panel.add(dairy); // Adds the dairy button to the output window
                    panel.add(bakery); // Adds the bakery button to the output window
                    panel.add(produce); // Adds the produce button to the output window
                    panel.add(pharmacy); // Adds the pharmacy button to the output window
     
     
    	    }
    I don't recall anyone telling you to remove the variables from the class, and in fact you should leave them in the class. What we recommended is that you not re-declare them in your constructor. That's a big difference. You need the class JRadioButton variables and a class ButtonGroup variable so that the variable and its referenced object will be visible to all methods of the class.

    Do I need to take the variables back out of the button statements and re-insert the private JRadioButton meats, dairy, bakery, produce, pharmacy; statement in order to get the variable to print out with the output??
    You need to add them back to the class, but don't re-declare them. Again do this:

    public class MyClass {
     
      private JRadioButton myRadioButton; // this is OK
     
      // constructor
      public MyClass() {
        // the code below is *not* OK since it re-declares the variable
        // JRadioButton myRadioButton = new JRadioButton("Radio Button"); 
     
        // the code below *is* OK since it does not re-declare the variable
        myRadioButton = new JRadioButton("Radio Button"); 
     
       // .....

  18. #16
    Member
    Join Date
    Jan 2013
    Posts
    37
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Radio Button Help Please!

    Okay... I renamed all of the button statements to remove the variable:

      myRadioButton1 = new JRadioButton("Meats", true); //Defines the meat radio button and sets it to default
                    myRadioButton2 = new JRadioButton("Dairy"); //Defines the dairy radio button
                    myRadioButton3 = new JRadioButton("Bakery"); //Defines the bakery radio button
                    myRadioButton4 = new JRadioButton("Produce"); //Defines the produce radio button
                    myRadioButton5 = new JRadioButton("Pharmacy"); //Defines the pharmacy radio button
     
                    ButtonGroup group = new ButtonGroup(); //Defines a button group to group buttons together
                    group.add(myRadioButton1); // Adds the meats button to the group
                    group.add(myRadioButton2); // Adds the dairy button to the group
                    group.add(myRadioButton3); // Adds the bakery button to the group
                    group.add(myRadioButton4); // Adds the produce button to the group
                    group.add(myRadioButton5); // Adds the pharmacy button to the group
     
                    panel.add(myRadioButton1); // Adds the meats button to the outwindow
                    panel.add(myRadioButton2); // Adds the dairy button to the output window
                    panel.add(myRadioButton3); // Adds the bakery button to the output window
                    panel.add(myRadioButton4); // Adds the produce button to the output window
                    panel.add(myRadioButton5); // Adds the pharmacy button to the output window

    I added the following statements to the declaration:

    public class RetailPriceCalculator extends JFrame
    	{
                private JRadioButton myRadioButton1, myRadioButton2, myRadioButton3, myRadioButton4, myRadioButton5;
                private ButtonGroup group;

    By adding the ButtonGroup group; declaration, I am now getting the "Local variable hides a field" warning message.

    Which probably explains why the statement:

    JOptionPane.showMessageDialog(null, 
                                "The Sale Price of your " + productNameInput + " from the " + group + "Department with an original price of " 
                                + originalPriceInput + " is now only $ " + salePrice + "!!");

    still does not produce the desired results....

    Not sure where else to go from here....

  19. #17
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Radio Button Help Please!

    You are re-declaring the group variable in the constructor. Haven't we discussed this very issue several times in this thread already?

  20. #18
    Member
    Join Date
    Jan 2013
    Posts
    37
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Radio Button Help Please!

    If this is the constructor:

                    myRadioButton1 = new JRadioButton("Meats", true); //Defines the meat radio button and sets it to default
                    myRadioButton2 = new JRadioButton("Dairy"); //Defines the dairy radio button
                    myRadioButton3 = new JRadioButton("Bakery"); //Defines the bakery radio button
                    myRadioButton4 = new JRadioButton("Produce"); //Defines the produce radio button
                    myRadioButton5 = new JRadioButton("Pharmacy"); //Defines the pharmacy radio button

    then I suppose I don't understand what a variable is.

    My understanding is that this is the constructor for the radio buttons. But the variable is the name "myRadioButton%" and the names "Meats, "Dairy", etc. inside the statements are only display text, not variables.

  21. #19
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Radio Button Help Please!

    Quote Originally Posted by Rick Sebastian View Post
    If this is the constructor:

                    myRadioButton1 = new JRadioButton("Meats", true); //Defines the meat radio button and sets it to default
                    myRadioButton2 = new JRadioButton("Dairy"); //Defines the dairy radio button
                    myRadioButton3 = new JRadioButton("Bakery"); //Defines the bakery radio button
                    myRadioButton4 = new JRadioButton("Produce"); //Defines the produce radio button
                    myRadioButton5 = new JRadioButton("Pharmacy"); //Defines the pharmacy radio button
    No that isn't a constructor but rather is code that is inside of your constructor.

    then I suppose I don't understand what a variable is.

    My understanding is that this is the constructor for the radio buttons. But the variable is the name "myRadioButton%" and the names "Meats, "Dairy", etc. inside the statements are only display text, not variables.
    I don't understand what you're stating.

    An example of a variable is your myRadioButton1 and all the myRadioButtonX's above as well as your group variable. The key issue is one of scope, and this will depend on where you *declare* your variable. If you declare it in the class, it will be visible throughout the class. If you declare it inside of a method or a constructor, then the variable is only visible inside of that method or constructor -- its scope is limited to that method or constructor (or other block of code).

    To again use an example:

    public class MyClass {
     
      private JRadioButton radioButtonInClass; // declared in class
      private JRadioButton radioButtonThatWillBeShadowed; // also declared in class
     
      // constructor
      public MyClass() {
        // the code below initializes the variable but does not re-declare the variable
        radioButtonInClass = new JRadioButton("Radio Button in class"); 
     
        // the variable below re-declares the variable and so the class variable will remain *null*
        // the variable below will thus be visible only in the constructor and will "shadow" the class
        // field of the same name
        JRadioButton radioButtonThatWillBeShadowed = new JRadioButton("Radio Button shadowed"); 
     
        // the variable below is local to the constructor and is visible in only the constructor.
        JRadioButton localRadioButton = new JRadioButton("Local radio button");
     
       // .....

Similar Threads

  1. add a radio button in RichTextArea
    By jim17 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2012, 07:33 AM
  2. Quick beginner radio button question
    By smarmy_cheauffeur in forum AWT / Java Swing
    Replies: 2
    Last Post: October 17th, 2011, 08:20 AM
  3. need help Coding RADIO BUTTON for REMEMBER ME
    By timosoft in forum Java Theory & Questions
    Replies: 3
    Last Post: February 4th, 2011, 04:19 PM
  4. Adding Radio Button Values
    By bazazu in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 13th, 2010, 09:41 AM
  5. Action from Radio Button
    By halfwaygone in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 25th, 2010, 10:52 AM