title: Binary to Decimal Applet with radio buttons
body: <p>I'm trying to make an applet that converts binary to decimal and decimal to binary.  I have already written applets that do each individual but now I want to make one which uses radio buttons to select the conversion the user wants to do and then have the convert button carry out that conversion.  I am stuck at the moment and not quite sure where to go from here...  It doesn't currently compile.</p>
  
  <p>I also want to include an arrow that points either up or down depending on the radio button selected... I've tried to implement the Unicode for said arrow into a JLabel but they do not accept characters, how would one go about this?</p>
  
  <p>Thank you very much any help is greatly appreciated.</p>
  
  <p>Heres my current mess of code...</p>
  
  <p>EDIT:</p>
  
  <pre><code>import java.util.Scanner;
  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  import java.text.DecimalFormat;
  
  public class binaryAndDecimalConvert extends JApplet
  {
      private JPanel bPanel;
      private JPanel dPanel;
      private JPanel buttonPanel;
      private JPanel radioPanel;
      private JPanel arrowPanel;
  
      private JLabel arrowUp;
      private JLabel arrowDown;
  
      private JTextField binaryTxt;
      private JTextField decimalTxt;
  
      private ButtonGroup radioButtonGroup;
      private JRadioButton binaryConvButton;
      private JRadioButton decimalConvButton;
  
      public void init()
      {
          Font font = new Font("display font", Font.BOLD, 15);
  
  
  
          //build the panels
          buildBpanel();
          buildArrowPanel();
          buildDpanel();
          buildButtonPanel();
          buildRadioPanel();
  
  
  
          //create Layout Manager.
          setLayout(new GridLayout(5, 1));
  
          // Add the panels to the content pan.
          add(bPanel);
          add(arrowPanel);
          add(dPanel);
          add(buttonPanel);
          add(radioPanel);
      }
  
  
      private void buildDpanel()
      {
          dPanel = new JPanel();
          dPanel.setBackground(Color.pink);
          JLabel message2 = new JLabel("Decimal Number: ");
  
          decimalTxt = new JTextField(15);
  
          dPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
  
          dPanel.add(message2);
          dPanel.add(decimalTxt);
      }
  
  
  
      private void buildBpanel()
      {
          //create the panel
          bPanel = new JPanel();
          bPanel.setBackground(Color.pink);
          //create a label to display a mssage
          JLabel message1 = new JLabel("Binary Number: ");
  
          //create a text field for the binary number
          binaryTxt = new JTextField(15);
  
  
          //create a layout manager for the panel
          bPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
  
          ///add the label and text field to the panel
          bPanel.add(message1);
          bPanel.add(binaryTxt);
      }
  
      public void buildRadioPanel()
      {
          radioPanel = new JPanel();
          radioPanel.setBackground(Color.pink);
  
          binaryConvButton = new JRadioButton("Binary to Decimal");
          decimalConvButton = new JRadioButton("Decimal to Binary");
  
          radioButtonGroup = new ButtonGroup();
          radioButtonGroup.add(binaryConvButton);
          radioButtonGroup.add(decimalConvButton);
  
          binaryConvButton.addActionListener(new RadioButtonListener());
          decimalConvButton.addActionListener(new RadioButtonListener());
  
          binaryConvButton.addActionListener(new RadioButtonListener());
          decimalConvButton.addActionListener(new RadioButtonListener());
  
          radioPanel.add(binaryConvButton);
          radioPanel.add(decimalConvButton);
  
          binaryConvButton.setEnabled(true);
      }
  
      public void buildArrowPanel()
      {
          arrowPanel = new JPanel();
  
          arrowUp = new JLabel("\u2191");
          arrowDown = new JLabel("\u2193");
  
          arrowPanel.setBackground(Color.pink);
  
          arrowPanel.add(arrowDown);
  
      }
  
      private class RadioButtonListener implements ActionListener
      {
          public void actionPerformed(ActionEvent e)
          {
              if(e.getSource() == binaryConvButton)
              {
                  arrowPanel.add(arrowUp);
              }
              else if(e.getSource() == decimalConvButton)
                  arrowPanel.add(arrowDown);
  
  
  
  
          }
      }
  
  
  
      private void buildButtonPanel()
      {
          buttonPanel = new JPanel();
          buttonPanel.setBackground(Color.pink);
          JButton convButton = new JButton("Convert");
  
          convButton.addActionListener(new ButtonListener());
          buttonPanel.add(convButton);
  
      }
  
      private class ButtonListener implements ActionListener
      {
          public void actionPerformed(ActionEvent e)
          {
              //binary to decimal conversion
              String decimalNum= "";
              int decimal1 = 0;
              String binaryNum = "";
              int power = 1;
              int dec;
  
              if(e.getSource() == decimalConvButton)
              {
              binaryNum=binaryTxt.getText();
              for(int i = 1; i &lt;= binaryNum.length(); i++)
              {
                  if(binaryNum.charAt(binaryNum.length()-i) == '1')
                  {
                      decimal1 = (decimal1 + power);
                  }
              power = (power*2);
              }
              decimalNum = Integer.toString(decimal1);
              decimalTxt.setText(decimalNum);
              }
  
              //decimal to binary
              if(e.getSource() == binaryConvButton)
              {
                    dec = Integer.parseInt(decimalTxt.getText());
                    while (dec != 0)
                    {
                          binaryNum = (dec % 2) + binaryNum;
                          dec /= 2;
                    }
                  binaryTxt.setText(binaryNum);
  
              }
          }
  
  
      }
  }
  </code></pre>