Please help me with centering a component and clearing a JRadioButton

Hi, could someone help me to cener the label and clear the selected JRadioButton?
I have put a comment beside the problematic place. Thanks a lot! ann
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ButtonAction extends JPanel implements ActionListener {
     private JRadioButton button1, button2;
     private JButton send, clear;
     private JTextField text;
     private JLabel label;
     private ButtonGroup group;
     public ButtonAction() {
          setPreferredSize(new Dimension(300, 200));
          setLayout(new GridLayout(0, 1, 5, 5));
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        setBorder(BorderFactory.createLineBorder(Color.blue));
        JPanel p1 = new JPanel();
        button1 = new JRadioButton("Button One");
          button2 = new JRadioButton("Button Two");
          group = new ButtonGroup();
          group.add(button1);
          group.add(button2);
          p1.add(button1);
          p1.add(button2);
          JPanel p2 = new JPanel();
          send = new JButton("Send");
          clear = new JButton("Clear");
          label = new JLabel(" ");
          label.setAlignmentX(Component.CENTER_ALIGNMENT);    //this doesn't work
          send.addActionListener(this);
          clear.addActionListener(this);
          p2.add(send);p2.add(clear);
          add(p1); add(p2); add(label);
     public void DisplayGUI(){
          JFrame frame = new JFrame();
          frame.getContentPane().add(this);
          frame.pack();
          frame.setVisible(true);
     public String getButton(){
          String result = "";
          if (button1.isSelected())
               result += "\t You have clicked the first button";
          else if (button2.isSelected())
               result += "\t You have clicked the second button";
          return result;
     public void setButton(){                  // this method doesn't work
          if (button1.isSelected() == true)
               button1.setSelected(false);
          else if (button2.isSelected() == true)
               button2.setSelected(false);
     public void actionPerformed (ActionEvent e) {
          if (e.getSource() == send) {
               label.setText(getButton());
          else if (e.getSource() == clear) {
               setButton();
               label.setText("no button is selected");
     public static void main(String[] args) {
          ButtonAction ba = new ButtonAction();
          ba.DisplayGUI();
}

setHorizontalAlignmentX(Component.CENTER_ALIGNMENT) is a container method and I don't think it does what you think. Try this instead:label = new JLabel("Some text");
//label.setHorizontalAlignmentX(Component.CENTER_ALIGNMENT);    //this doesn't work
label.setHorizontalAlignment(SwingConstants.CENTER);When you alter the state of a radio button you should probably use the group methods - because the group "coordinates" the state of the buttons as a whole. Note that setSelection() isn't quite like a user click in that it doesn't fire a method. Now, in your case you are essentially clearing the selection of the group and ButtonGroup provides a method for that.     public void setButton(){                  // this method doesn't work
    if (button1.isSelected() == true)
    button1.setSelected(false);
    else if (button2.isSelected() == true)
    button2.setSelected(false);
    group.clearSelection();
}Note we don't usually write if(foo == true), it's enough to say if(foo).
You will get better and faster help from the Swing forum and it helps everyone if posts are made to the right place. http://forum.java.sun.com/forum.jspa?forumID=57

Similar Messages

Maybe you are looking for

  • ILife 08 iWeb 08  - Podcast Not Working  HELP!

    I may not have explained myself in my last post, so I'll try again. o.k. Who has had a problem with the new 08 stuff? I am not a computer genius and do my podcast in garageband, share it with iWeb, and publish with .mac. I am not good at code and hav

  • Give Discount/Surcharges to PR00

    Hi Experts, Please can you explain how to give discount to PR00 base value? For example, currently the pricing logic is PR00 value * quantity = gross value. Any discount or surcharges will be based on this gross value. How to change it to give discou

  • Log shipping is not restoring log files ata particular time

    Hi, I have configured log shipping and it restores all the log files upto a particular time after which it throws error and not in consistent sate. I tried deleting and again configuring log shipping couple of times but no success. Can any one tell m

  • Getting BAPI header and library files

    Hi, I'm going to develop a job scheduling system that satisfies XBP 2.0 certification requirements. I received some documents of XBP 2.0 from a SAP ICC consultant and reviewed them. Now I'm going to prepare for setting up a development environment an

  • Problems downloading multiple ftp files

    Hi community! I'm new to this board, and i've joined because I got a severe problem. I need to download some thousands of files the path of which I'v got stored i an SQLServer Database. So, i'm just running through my db and for every line I download