Results 1 to 8 of 8
Thread: Adding a panel to a panel
- 02-01-2010, 07:51 PM #1
Member
- Join Date
- Oct 2009
- Location
- Oregon
- Posts
- 22
- Rep Power
- 0
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); } }
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
-
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.
- 02-01-2010, 11:39 PM #3
Member
- Join Date
- Oct 2009
- Location
- Oregon
- Posts
- 22
- Rep Power
- 0
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
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
-
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?
Java Code:pizzasize.setLayout(new GridLayout(0, 1, 0, 10)); pizzasize.add(b1); ...
Last edited by Fubarable; 02-01-2010 at 11:48 PM.
- 02-02-2010, 12:18 AM #5
Member
- Join Date
- Oct 2009
- Location
- Oregon
- Posts
- 22
- Rep Power
- 0
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);
- 02-02-2010, 02:48 AM #6
Member
- Join Date
- Oct 2009
- Location
- Oregon
- Posts
- 22
- Rep Power
- 0
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
(Sokath! His eyes open!!)Last edited by rclausing; 02-02-2010 at 02:51 AM.
-
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 ""; } } }
- 02-02-2010, 06:56 AM #8
Member
- Join Date
- Oct 2009
- Location
- Oregon
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
Adding panels into a panel
By kcakir in forum AWT / SwingReplies: 7Last Post: 12-07-2009, 06:19 PM -
Adding components to a panel
By jboy in forum New To JavaReplies: 1Last Post: 10-10-2009, 02:02 PM -
Interactive Panel
By homistar in forum Java 2DReplies: 4Last Post: 10-06-2009, 10:16 AM -
How to set Image in Panel ?
By sudmitra in forum New To JavaReplies: 0Last Post: 09-12-2009, 11:07 PM -
panel positioning
By shwein in forum New To JavaReplies: 4Last Post: 09-09-2008, 06:15 PM
Bookmarks