Results 1 to 8 of 8
Thread: Adding a panel to a panel
- 02-01-2010, 06: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?
and my tab: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, 10: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:
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.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?
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).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 10:48 PM.
- 02-01-2010, 11:18 PM #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?
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?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, 01:48 AM #6
Member
- Join Date
- Oct 2009
- Location
- Oregon
- Posts
- 22
- Rep Power
- 0
OK I re-read this part a dozen times:
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?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 01: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, 05: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, 05:19 PM -
Adding components to a panel
By jboy in forum New To JavaReplies: 1Last Post: 10-10-2009, 01:02 PM -
Interactive Panel
By homistar in forum Java 2DReplies: 4Last Post: 10-06-2009, 09:16 AM -
How to set Image in Panel ?
By sudmitra in forum New To JavaReplies: 0Last Post: 09-12-2009, 10:07 PM -
panel positioning
By shwein in forum New To JavaReplies: 4Last Post: 09-09-2008, 05:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks