Results 1 to 12 of 12
  1. #1
    acat is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default about radiobutton

    Hi guys!

    there are 2 class.
    the first class is product name
    Java Code:
    public class Product _Name
    {
    	//class data
    	private String Name;
    	private double Price;
    	
    	public Product _Name()
    	{
    		Name = "";
    		Price = 0.0;
    	}
    
    	public Product _Name(String In_Name, double In_Price, )
    	{
    		Name = In_Name;
    		Price = In_Price;
    	}
    
    	public Product _Name(String In_Name)
    	{
    		Name = In_Name;
    	}
    
    	public String Get_Name()
    	{
    		return Name;
    	}
    
    	public double Get_Price()
    	{
    		return Price;
    	}
    }

    the main class

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    import java.text.*;
    import javax.swing.JOptionPane;
    
    public class Product extends JFrame
    {
         JRadioButton[] Product_Name = new JRadioButton[9];
    
         JLabel outName = new JLabel("");
         JLabel outPrice = new JLabel("");
         JLabel outTotalPrice = new JLabel ("");
    
         JRadioButton NameRadioButton [];
    
        private class Product_Name_RadioButton_Listener implements ActionListener
       {
            public void actionPerformed (ActionEvent e)
           {
                for (int i=0; i < NameRadioButton.length; i ++)
               {
                      if (e.getSource () == NameRadioButton [i])
                     {
                           outName.setText("" + Product_Name[i].Get_Name());
                           outPrice.setText("" +  Product_Name[i].Get_Price());
    
                      }
                }
           }
       }
              public Product()
             {
    		//create the radiobutton
    		Product_Name[0] = new JRadioButton("Product 1");
    		Product_Name[1] = new JRadioButton("Product 2");
    		Product_Name[2] = new JRadioButton("Product 3");
    		Product_Name[3] = new JRadioButton("Product 4");
    		Product_Name[4] = new JRadioButton("Product 5");
    		Product_Name[5] = new JRadioButton("Product 6");
    		Product_Name[6] = new JRadioButton("Product 7");
                             Product_Name[7] = new JRadioButton("Product 8");
                             Product_Name[8] = new JRadioButton("Product 9");
                            ButtonGroup group = new ButtonGroup();
                           setLayout (new FlowLayout ());
    
                       NameRadioButton = new JRadioButton [Product_Name.length];
    
                     for (int i = 0; i < Product_Name.length; i++ )
                    {
                          group.add(Product_Name[i]);
                           NameRadioButton [i] = new JRadioButton (Product_Name[i].Get_Name ());
                          NameRadioButton[i].addActionListener (new Product_Name_RadioButton_Listener ());
                   }
           }
    
        public static void main(String[] args)
        {
          JFrame frame = new Product();
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setTitle("Product");
        }
    }

    this is my class. the first class is right, but there are some problem in the main class.
    my question is why it alway cannot find the "Get_Name" and the "Get_Price" I already declared it in the first class. And the Listener is doesnt work, I mean when I choose a product, the lable does not "setText" !
    where am I wrong?

    Someone can help me?
    Last edited by acat; 04-13-2011 at 02:40 AM.

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

    Default

    Well for one, your class name has a space in it, and spaces aren't allowed. Is this a typo?

    Edit: your code is furschmuttered. You've got an array of JRadioButton variable that has the same name as one of your classes -- don't do this. You're trying to call Product_Name methods on JRadioButton objects -- don't do this either.

    JRadioButton may not even be the best fit here but rather a JComboBox. The model can hold Product_Name objects making easier to retrieve these guys.
    Last edited by Fubarable; 04-13-2011 at 02:09 AM.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    Product_Name is an array of JRadioButtons. The Get_Name method is in the Product_Name class and not the JRadioButton class.

  4. #4
    acat is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    how should i change it, make the Get_Name method get into the JRadioButton class?

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    How do suggest doing that? It is a class in the Java API which you can't change, or if you can is probably illegal. If you want to call the Get_Name method then you should call it on a Product_Name instance and not a JRadioButton instance.

  6. #6
    acat is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    I mean how to fix it, make it be found !!!
    I just confuse that how should I do when I click the radiobutton, and then it will response. If I just ues the main class, it will work?

    Java Code:
    private class Product_Name_RadioButton_Listener implements ActionListener
       {
            public void actionPerformed (ActionEvent e)
           {
                for (int i=0; i < NameRadioButton.length; i ++)
               {
                      if (NameRadioButton [i].isSelected())
                           outName.setText("" + Product_Name[i]);
                           outPrice.setText("" +  Product_Name[i]);
    
                 }
           }
    }

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

    Default

    Why are you ignoring my reply? You still have your array named the same as the other class, and haven't addressed this issue. You have not responded regarding using a JComboBox in place of JRadioButtons.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    I have no idea what you are attempting to do so it makes it hard to help. One thing that leads to confusion is that your class and array Of JRadioButtons have the same name: Product_Name.

    Repeating what I said earlier: If you want to call the Get_Name method then you should call it on a Product_Name instance and not a JRadioButton instance.
    Java Code:
    Product_Name prod = new Porduct_Name();
    System.out.println(prod.Get_Name());
    That will work. Where in your code do you create Product_Name objects?

  9. #9
    acat is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Im sorry, actually, Im not .
    Im trying using the JComboBox instead of the RadioButton.
    I donot think it is the spell mistake. And I m also change the name of the class name. but I am still trying to stop calling the Product_Name methods on JRadioButton objects, It is not going very well !!!
    I still think there are something wrong with the Listener, you know. I tried use the JButton, it works.
    However, when I use the JRadioButton, so many things different.

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

    Default

    Quote Originally Posted by acat View Post
    but I am still trying to stop calling the Product_Name methods on JRadioButton objects, It is not going very well !!!
    I'm not sure how many times we can tell you, but please believe us, this will not work so stop trying.

    If you need to associate the object with a radio button then use a HashMap<JRadioButton, Product_Name> or HashMap<String, Product_Name> so you can use either the JRadioButton or its text to get your object.

    But if you use a JComboBox, you can simply use an array of Product_Name as the nucleus of your combo box's model (a DefaultComboBoxModel object I think), and then extract your object by the getSelectedItem() method. The tutorials will explain how. Just make sure that your product class has a decent toString method that return's the product's name.

  11. #11
    acat is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    I am so believe you.
    but you know I am a very fresh leaner, so sometimes I have no idea what are you trying to tell me, that I just try any way to make it right. when I write java code, I always need some examples to show me which way is correct, if there is no example, I can do nothing. I just confirmed that this task require me use the radiobutton. I wil try to use the toString method !!

  12. #12
    acat is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    I do not think I have already created Product_Name objects
    you mean in the main class, I should create Product_Name objects?

Similar Threads

  1. Help with radiobutton!
    By goffy in forum New To Java
    Replies: 1
    Last Post: 05-09-2010, 04:14 PM
  2. uses of radiobutton...
    By mlibot in forum New To Java
    Replies: 22
    Last Post: 09-17-2009, 08:33 AM
  3. freeze radioButton without disable them
    By itaipee in forum AWT / Swing
    Replies: 5
    Last Post: 05-25-2009, 09:29 AM
  4. RadioButton
    By tiger100plus in forum New To Java
    Replies: 1
    Last Post: 01-05-2009, 08:04 PM
  5. radiobutton selection
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-17-2008, 02:58 AM

Posting Permissions

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