Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Default please help me in grouping

    i have created 5 radio buttons in visual editor and i need to group them.below are the codes for the RBs:

    private JRadioButton getJRadioButton_a() {
    if (jRadioButton_a == null) {
    jRadioButton_a = new JRadioButton();
    jRadioButton_a.setBounds(new java.awt.Rectangle(150,77,41,29));
    jRadioButton_a.setText("A");
    }
    return jRadioButton_a;
    }

    private JRadioButton getJRadioButton_b() {
    if (jRadioButton_b == null) {
    jRadioButton_b = new JRadioButton();
    jRadioButton_b.setBounds(new java.awt.Rectangle(190,77,41,29));
    jRadioButton_b.setText("B");
    }
    return jRadioButton_b;
    }

    private JRadioButton getJRadioButton_c() {
    if (jRadioButton_c == null) {
    jRadioButton_c = new JRadioButton();
    jRadioButton_c.setBounds(new java.awt.Rectangle(230,77,41,29));
    jRadioButton_c.setText("C");
    }
    return jRadioButton_c;
    }


    private JRadioButton getJRadioButton_d() {
    if (jRadioButton_d == null) {
    jRadioButton_d = new JRadioButton();
    jRadioButton_d.setBounds(new java.awt.Rectangle(270,77,41,29));
    jRadioButton_d.setText("D");
    }
    return jRadioButton_d;
    }


    private JRadioButton getJRadioButton_e() {
    if (jRadioButton_e == null) {
    jRadioButton_e = new JRadioButton();
    jRadioButton_e.setBounds(new java.awt.Rectangle(310,77,41,29));
    jRadioButton_e.setText("E");
    }
    return jRadioButton_e;
    }


    //someone please help me.i need all the help urgently as im new to this module.thank you so much!
    Last edited by need_help_in_java_badly; 07-29-2009 at 04:50 PM.

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

    Default

    I'd avoid absolute positioning here (null layout for the container and setBounds for the component) and instead have the container that holds the radiobuttons use layout manager(s). GridLayout often works well with radiobuttons.

    For more specific help things you can do to help us include posting more code, compilable code would be best, or even a picture of the state of your GUI now and as you'd like it to be.

    Best of luck, and HTH.

  3. #3
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Default

    the problem is my panel has got other combo boxes,text fields and buttons which requires me to use absolute positioning therefore i may not be able to use as what u have mentioned above.

  4. #4
    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 need_help_in_java_badly View Post
    the problem is my panel has got other combo boxes,text fields and buttons which requires me to use absolute positioning therefore i may not be able to use as what u have mentioned above.
    Yeah, you still can and should use the layout managers. Again, please show a picture of your current app and how you'd like the app to look like and we can show you some sample code.

    Again, best of luck.

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

    Default

    For instance, say I wanted to arrange radiobuttons in a column on the left side of a GUI. I'd place the radiobuttons in their own JPanel that uses a BorderLayout, say,
    Java Code:
    JPanel radioPanel = new JPanel(new GridLayout(0, 1, 10, 10));
    the first two parameters, 0, and 1 tell the panel to create a grid of variable number of rows but with one column. The next two parameters tell the layout to place 10 points between each component added to the jpanel.
    I can then add the radio buttons like so:
    Java Code:
      private static final String[] RADIO_BUTTON_STRINGS = { "Sunday", "Monday", 
        "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
      //.....
    
      public FuSwing1() {
        //...
        
        JPanel radioPanel = new JPanel(new GridLayout(0, 1, 10, 10));
        for (int i = 0; i < RADIO_BUTTON_STRINGS.length; i++) {
          JRadioButton radioBtn = new JRadioButton(RADIO_BUTTON_STRINGS[i]);
          radioBtn.setActionCommand(RADIO_BUTTON_STRINGS[i]);
          radioPanel.add(radioBtn);
          btnGroup.add(radioBtn);
        }
    I'd have the main JPanel of the GUI use a BorderLayout, and then add the radioPanel BorderLayout.WEST to place it to the left:

    Java Code:
        mainPanel.setLayout(new BorderLayout(10, 10)); 
        mainPanel.add(radioPanel, BorderLayout.WEST);
    A small sample program could look like so (you may have to add a package name for this to compile on your system):
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class FuSwing1 {
      
      private static final String[] BUTTON_STRINGS = { "One", "Two", "Three",
          "Four", "Five" };
      private static final String[] RADIO_BUTTON_STRINGS = { "Sunday", "Monday", 
        "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
      private JPanel mainPanel = new JPanel();
      private ButtonGroup btnGroup = new ButtonGroup();
    
      public FuSwing1() {
        JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 10, 10));
        for (int i = 0; i < BUTTON_STRINGS.length; i++) {
          JButton button = new JButton(BUTTON_STRINGS[i]);
          buttonPanel.add(button);
        }
        
        JPanel radioPanel = new JPanel(new GridLayout(0, 1, 10, 10));
        for (int i = 0; i < RADIO_BUTTON_STRINGS.length; i++) {
          JRadioButton radioBtn = new JRadioButton(RADIO_BUTTON_STRINGS[i]);
          radioBtn.setActionCommand(RADIO_BUTTON_STRINGS[i]);
          radioPanel.add(radioBtn);
          btnGroup.add(radioBtn);
        }
        
        JPanel centerPanel = new JPanel() {
          @Override
          protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setPaint(new GradientPaint(0, 0, Color.red, 100, 100, Color.blue, true));
            g2.fillOval(0, 0, getWidth(), getHeight());
          }
        };
        centerPanel.setPreferredSize(new Dimension(800, 800));
        
        mainPanel.setPreferredSize(new Dimension(650, 500));
        mainPanel.setLayout(new BorderLayout(10, 10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
        mainPanel.add(radioPanel, BorderLayout.WEST);
        mainPanel.add(new JScrollPane(centerPanel), BorderLayout.CENTER);
      }
      
      public JPanel getMainPanel() {
        return mainPanel;
      }
      
      
      private static void createAndShowGUI() {
        
        JFrame frame = new JFrame("FuSwing1 Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new FuSwing1().getMainPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            createAndShowGUI();
          }
        });
    
      }
      
    }

  6. #6
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Default

    thank you so much for the codes fubarable! it helped me big time:)

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

    Default

    Cool. I'm glad it helped. Please let us know how your project turns out.

Posting Permissions

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