Results 1 to 7 of 7
  1. #1
    hypernova is offline Member
    Join Date
    Sep 2011
    Location
    United Kingdom
    Posts
    6
    Rep Power
    0

    Exclamation Problem with JRadioButton and ButtonGroup - Identifier Expected error?

    Hi guys, I'm new to Java and this is probably a really silly question, or at least is has a simple answer because I don't understand something, most probably...

    Here is the contents of the file "OrderFrame":

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class OrderFrame extends JFrame{
    
    	private JPanel BaseSelectPanel, SizeSelectPanel, ExtraSelectPanel,
    		ToppingSelectPanel, NumberSelectPanel, PizzaPricePanel,
    		DeliverySelectPanel, OrderSummaryPanel;
    
    	private class BasePanel extends JPanel{
    		private JPanel BaseSelectPanel = new JPanel();
    		private JLabel BaseSelectLabel = new JLabel("Base type:");
    		private JRadioButton ItalianButton = new JRadioButton("Italian", true);
    		private JRadioButton PanButton = new JRadioButton("Pan", false);
    		private ButtonGroup RadioGroup = new ButtonGroup();
    		RadioGroup.add(ItalianButton);       //Identifier expected?
    		RadioGroup.add(PanButton);          //Identifier expected?
    		BasePanel.add(BaseSelectLabel);   //Identifier expected?
    		BasePanel.add(ItalianButton);       //Identifier expected?
    		BasePanel.add(PanButton);           //Identifier expected?
                                                                   //I find that this.add does not work either, which one should I be using?
    	}
    
    	public OrderFrame(){
    		super("Pizza Hut UK Order Form");
    		this.setLayout(new FlowLayout());
    		this.setSize(800,600);
    		BasePanel basepanel = new BasePanel();
    		add(basepanel);
    
    
    	}
    }
    The error lines are commented. What am I doing wrong?
    Another thing I have found is that I have seen 'this.add' and '<name>.add' used... What does the 'this.' keyword do?

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: Problem with JRadioButton and ButtonGroup - Identifier Expected error?

    First of all use standard Java naming conventions. Variable names DO NOT start with an upper case character.

    "RadioGroup" should be "radioGroup" etc...

    You need to create a constructor for your BasePanel class. Then all the add(...) statements need to be moved to the constructor.

  3. #3
    hypernova is offline Member
    Join Date
    Sep 2011
    Location
    United Kingdom
    Posts
    6
    Rep Power
    0

    Default Re: Problem with JRadioButton and ButtonGroup - Identifier Expected error?

    So lines 25 through 30 are not a constructor? What is meant by a constructor? I "googled" and found that 'a constructor is like a method, but not exactly like a method', and it creates a new instance of a class, but what does this mean and how do I use one?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Problem with JRadioButton and ButtonGroup - Identifier Expected error?

    If you don't know what constructors are then i woudl suggest stepping away from Swing and learn the basics.
    Start here, with the basics, specifically this section.

  5. #5
    hypernova is offline Member
    Join Date
    Sep 2011
    Location
    United Kingdom
    Posts
    6
    Rep Power
    0

    Default Re: Problem with JRadioButton and ButtonGroup - Identifier Expected error?

    Yep I've read most of that today. I already know roughly what a class is and why oop is used from programming in C++, but all this nesting of things is tricky concept.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: Problem with JRadioButton and ButtonGroup - Identifier Expected error?

    public OrderFrame() is a constructor for your OrderFrame class.

    Now you need a public BasePanel() for the BasePanel class.

  7. #7
    hypernova is offline Member
    Join Date
    Sep 2011
    Location
    United Kingdom
    Posts
    6
    Rep Power
    0

    Default Re: Problem with JRadioButton and ButtonGroup - Identifier Expected error?

    Ah right! I think I understand, thanks.

Similar Threads

  1. identifier expected error
    By Java Learner in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 08:13 PM
  2. Error:identifier expected(Help!)
    By chhoton in forum New To Java
    Replies: 8
    Last Post: 09-22-2009, 04:42 PM
  3. Setting selected JRadioButton in ButtonGroup
    By emceenugget in forum AWT / Swing
    Replies: 3
    Last Post: 01-29-2009, 06:05 PM
  4. Identifier expected error
    By vasu18 in forum New To Java
    Replies: 1
    Last Post: 01-01-2008, 05:49 PM
  5. Error: <identifier> expected
    By barney in forum AWT / Swing
    Replies: 2
    Last Post: 07-31-2007, 07:38 AM

Tags for this Thread

Posting Permissions

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