Results 1 to 8 of 8
  1. #1
    rclausing is offline Member
    Join Date
    Oct 2009
    Location
    Oregon
    Posts
    22
    Rep Power
    0

    Default Adding a panel to a panel

    The following code produces the error:

    "java:28: cannot find symbol
    symbol : method add(javax.swing.ButtonGroup,java.lang.String)
    location: class PizzaTab
    add (size, BorderLayout.NORTH);
    ^
    1 error

    Tool completed with exit code 1"

    Can someone point me in the right direction? Is this just a syntax prob or can I not add a buttongroup to a BorderLayout Panel?

    Java Code:
    import javax.swing.*;
    public class OrderForm
    {
    	public static void main (String[] args)
    	{
    		JFrame frame = new JFrame ("Pizza Order Form");
    		frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
    		JTabbedPane tp = new JTabbedPane();
    		tp.addTab ("Pizza", new PizzaTab());
    
    		frame.getContentPane().add(tp);
    		frame.pack();
    		frame.setVisible(true);
    
    
    
    	}
    
    }
    and my tab:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class PizzaTab extends JPanel
    {
    
    
        public PizzaTab()
        {
    		setLayout (new BorderLayout());
    
    		setBackground (Color.cyan);
    
    
    			ButtonGroup size = new ButtonGroup();
    		   	JRadioButton b1 = new JRadioButton("small");
            	JRadioButton b2 = new JRadioButton("medium");
            	JRadioButton b3 = new JRadioButton("large");
    
            	size.add (b1);
            	size.add (b2);
            	size.add (b3);
    
    
    		add (size, BorderLayout.NORTH);
    
    
    
    
    
    
    
        }  // End
    } // End of the PizzaTab

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

    Default

    You're trying to add a ButtonGroup to a container and that won't work. A ButtonGroup isn't a visual component that can be added to anything but rather a logical entity that prevents more than one JRadioButton from being selected at the same time. What you'll need to do is add the radio buttons to both a button group AND a JPanel and then add that JPanel to your container.

  3. #3
    rclausing is offline Member
    Join Date
    Oct 2009
    Location
    Oregon
    Posts
    22
    Rep Power
    0

    Default

    OK, that was my original direction but I couldn't get my buttons to show up in this code:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class PizzaTab extends JPanel
    {
    
    
        public PizzaTab()
        {
    		setLayout (new BorderLayout());
    
    		setBackground (Color.cyan);
    
    			JPanel pizzasize = new JPanel();
    			ButtonGroup size = new ButtonGroup();
    		   	JRadioButton b1 = new JRadioButton(" small ");
            	JRadioButton b2 = new JRadioButton("medium");
            	JRadioButton b3 = new JRadioButton("large");
    
            	size.add (b1);
            	size.add (b2);
            	size.add (b3);
    
    
    		add (pizzasize, BorderLayout.NORTH);
    
    
    
    
    
    
    
        }  // End
    } // End of the PizzaTab
    as this does not add the button group to the new Jpanel. I really want to strangle my instructor - I feel like I am trying to learn French but I have no English to French dictionary to reference. Checking and guessing code syntax sucks. It's like trying to shoot an empty pop can with a BB gun while blindfolded. And since I don't have any class time between when a project is assigned and when it is due, I don't have time to ask questions. The only added benefit I can see is that once I figure out how to do something, I likely won't forget.

    Is it permissible for someone to show me the correct syntax for adding the ButtonGroup size to my JPanel pizzasize? I guessed on 3 or 4 different ways but they were not correct.

    ie pizzasize.size.add (b1) or size.add (pizzasize, BorderLayout.NORTH) etc

  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 rclausing View Post
    OK, that was my original direction but I couldn't get my buttons to show up in this code:
    Of course not. Where do you add the radio buttons to the pizza size JPanel?

    as this does not add the button group to the new Jpanel.
    Is it permissible for someone to show me the correct syntax for adding the ButtonGroup size to my JPanel pizzasize?
    Again, you do not add a ButtonGroup to any panel. A ButtonGroup is not a component; it's not a physical entity; it's not visible and will never be visible and should not be placed anywhere (how else can I say this?). The ButtonGroup is used mainly to make sure that only one radiobutton is selected at a time. You can also use the button group to figure out which radio button is currently selected via its getSelection() method which returns the ButtonModel of the selected button (or null if no button has been selected).

    Java Code:
    pizzasize.setLayout(new GridLayout(0, 1, 0, 10));
    pizzasize.add(b1);
    ...
    Last edited by Fubarable; 02-01-2010 at 11:48 PM.

  5. #5
    rclausing is offline Member
    Join Date
    Oct 2009
    Location
    Oregon
    Posts
    22
    Rep Power
    0

    Default

    Interesting... So FlowLayout is not the default (like it is everywhere else) and you have to set the layout manager of the JPanel in order to declare and nest it?

    Java Code:
    public PizzaTab()
        {
    		setLayout (new BorderLayout());
    
    		setBackground (Color.cyan);
    
    			JPanel pizzasize = new JPanel();
    			ButtonGroup size = new ButtonGroup();
    		   	JRadioButton b1 = new JRadioButton(" small ");
            	JRadioButton b2 = new JRadioButton("medium");
            	JRadioButton b3 = new JRadioButton("large");
    
            	pizzasize.setLayout(new FlowLayout());
            	pizzasize.add (b1);
            	pizzasize.add (b2);
            	pizzasize.add (b3);
    
    
    		add (pizzasize, BorderLayout.NORTH);
    This seems to work, except that I am able to choose all 3 of the buttons. But I want to understand how this needs to work first. A nested panel has no default layout manager therefore one needs to be chosen in order to nest the panel?

  6. #6
    rclausing is offline Member
    Join Date
    Oct 2009
    Location
    Oregon
    Posts
    22
    Rep Power
    0

    Default

    OK I re-read this part a dozen times:

    A ButtonGroup is not a component; it's not a physical entity; it's not visible and will never be visible and should not be placed anywhere
    I get it now. First I have to add the buttons into the ButtonGroup, and then I can add the buttons to the JPanel. The ButtonGroup being an abstract way of connecting a certain number of buttons so that only one of the group can be selected. This way, I could have lots of buttons in the panel, with some or all of them being grouped (or not) as is needed?


    (Sokath! His eyes open!!)
    Last edited by rclausing; 02-02-2010 at 02:51 AM.

  7. #7
    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 rclausing View Post
    I get it now. First I have to add the buttons into the ButtonGroup, and then I can add the buttons to the JPanel. The ButtonGroup being an abstract way of connecting a certain number of buttons so that only one of the group can be selected. This way, I could have lots of buttons in the panel, with some or all of them being grouped (or not) as is needed?
    (Sokath! His eyes open!!)
    Yep.

    Also, the button group can allow you to know which radio button has been selected. For example, this app has three separate button groups that each control 3 radio buttons:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    import javax.swing.border.TitledBorder;
    
    public class RadioDemo {
       private JPanel mainPanel = new JPanel();
       private RadioPanel[] radioPanels = {
         new RadioPanel("Porridge", new String[] {"Too Hot", "Too Cold", "Just Right"}),
         new RadioPanel("Chair", new String[] {"Too Big", "Too Small", "Just Right"}),
         new RadioPanel("Bed", new String[] {"Too Hard", "Too Soft", "Just Right"}),
       };
    
       public RadioDemo() {
          JPanel radioPanelHolder = new JPanel();
          radioPanelHolder.setLayout(new GridLayout(1, 0, 15, 0));
          for (RadioPanel radioPanel : radioPanels) {
             radioPanelHolder.add(radioPanel.getMainPanel());
          }
          
          JButton getSelectionsBtn = new JButton("Get Selections");
          getSelectionsBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                getSelectionsActionPerformed();
             }
          });
          JPanel getSelectionsBtnPanel = new JPanel();
          getSelectionsBtnPanel.add(getSelectionsBtn);
          
          mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
          mainPanel.setLayout(new BorderLayout(20, 20));
          mainPanel.add(radioPanelHolder, BorderLayout.CENTER);
          mainPanel.add(getSelectionsBtnPanel, BorderLayout.SOUTH);
       }
    
       private void getSelectionsActionPerformed() {
          for (RadioPanel radioPanel : radioPanels) {
             System.out.println("Selection for " + radioPanel.getTitle() + ": "
                      + radioPanel.getRadioBtnSelection());
          }
          System.out.println();
       }
    
       public JComponent getComponent() {
          return mainPanel;
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("Goldilocks App");
          frame.getContentPane().add(new RadioDemo().getComponent());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    class RadioPanel {
       private JPanel mainPanel = new JPanel();
       private ButtonGroup buttongroup = new ButtonGroup();
       private String title;
       
       public RadioPanel(String title, String[] selections) {
          this.title = title;
          mainPanel.setLayout(new GridLayout(0, 1, 0, 20));
          mainPanel.setBorder(new TitledBorder(title));
          for (String selection : selections) {
             JRadioButton radiobtn = new JRadioButton(selection);
             radiobtn.setActionCommand(selection);
             buttongroup.add(radiobtn);
             mainPanel.add(radiobtn);
          }
       }
       
       public JPanel getMainPanel() {
          return mainPanel;
       }
       
       public String getTitle() {
          return title;
       }
       
       public String getRadioBtnSelection() {
          ButtonModel btnModel = buttongroup.getSelection();
          if (btnModel != null) {
             return btnModel.getActionCommand();
          } else {
             return "";
          }
       }
    }

  8. #8
    rclausing is offline Member
    Join Date
    Oct 2009
    Location
    Oregon
    Posts
    22
    Rep Power
    0

    Default

    Nice! thank you for that. I am sure it will come in handy! And thank you very much for the prompt replies!

Similar Threads

  1. Adding panels into a panel
    By kcakir in forum AWT / Swing
    Replies: 7
    Last Post: 12-07-2009, 06:19 PM
  2. Adding components to a panel
    By jboy in forum New To Java
    Replies: 1
    Last Post: 10-10-2009, 02:02 PM
  3. Interactive Panel
    By homistar in forum Java 2D
    Replies: 4
    Last Post: 10-06-2009, 10:16 AM
  4. How to set Image in Panel ?
    By sudmitra in forum New To Java
    Replies: 0
    Last Post: 09-12-2009, 11:07 PM
  5. panel positioning
    By shwein in forum New To Java
    Replies: 4
    Last Post: 09-09-2008, 06:15 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
  •