Results 1 to 3 of 3
  1. #1
    acat is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default JButton convert JRadioButton

    Hi, guys!
    could someone help convert the JButton to JRadioButton?
    this code also have some problems!!

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class Food_Price extends JFrame
    {
    //Class Data;
    Food Food_Array [];

    //GUI Components
    JButton Foods_Buttons [];
    JButton Food1_Button = new JButton ("food1");
    JButton Food2_Button = new JButton ("food2");
    JButton Food3_Button = new JButton ("food3");
    JButton Food4_Button = new JButton ("food4");

    JLabel Output_Label1 = new JLabel ("");
    JLabel Output_Label2 = new JLabel ("");

    private class Food_Button_Listener implements ActionListener
    {
    public void actionPerformed (ActionEvent e)
    {
    for(int k = 0; k < Foods_Buttons.length; k++)
    {
    if (e.getSource () == Foods_Buttons [k])
    {
    Output_Label1.setText ("" + Food_Array[k].Get_Name ());
    Output_Label2.setText ("" + Food_Array[k].Get_Price());
    }
    }

    }
    }


    private void Create_Array_With_Hardcoded_Data ()
    {
    Food_Array = new Food [4];

    Food_Array [0] = new Food ("food 1",20.0);
    Food_Array [1] = new Food ("food 2",30.0);
    Food_Array [2] = new Food ("food 3",40.0);
    Food_Array [3] = new Food ("food 4",50.0);
    }

    //constructor
    public Food_Price ()
    {
    setLayout (new FlowLayout());

    Create_Array_With_Hardcoded_Data ();

    Foods_Buttons = new JButton[Food_Array.length];

    for (int k = 0; k < Food_Array.length; k++ )
    {
    Foods_Buttons [k] = new JButton(Food_Array [k].Get_Name());

    add (Foods_Buttons [k]);

    Foods_Buttons [k].addActionListener (new Food_Button_Listener());
    }

    setBounds (10,10,500,500);
    setVisible(true);
    }

    public static void main (String args[])
    {
    new Food_Price ();

    }
    }





    public class foods
    {

    private String Name;
    private double Price;

    public foods()
    {
    Name = "";
    Price = 0.0;
    }


    public foods (String In_Name, double In_Price)
    {
    Name = In_Name;
    Price = In_Price;
    }


    public foods (String In_Name)
    {
    Name = In_Name;
    }


    public String Get_Name()
    {
    return Name;
    }

    public double Get_Price()
    {
    return Price;
    }


    public void Set_Name(String In_Name)
    {
    Name = In_Name;
    }

    }
    Last edited by acat; 04-11-2011 at 07:26 AM.

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    I'm not sure you can convert a JButton to a JRadioButton... Also saying that your code also has problems doesn't tell us anything.

    We here at Java Forums, contrary to popular belief, cannot read minds. So if you're getting an error message, post it here. If you aren't getting the expected output, post what you're getting and what you're supposed to get.

    Until you do that, there's nothing we can do for you. At least until I develop mind reading.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

Similar Threads

  1. Help on using JRadioButton with images
    By Nych in forum New To Java
    Replies: 3
    Last Post: 03-16-2011, 08:12 AM
  2. JRadioButton
    By mcajavaprogramer in forum AWT / Swing
    Replies: 1
    Last Post: 11-05-2010, 01:31 PM
  3. JRadioButton help (see pasted code)
    By ocarabal in forum New To Java
    Replies: 4
    Last Post: 06-02-2010, 01:56 AM
  4. BackGround JRadioButton
    By antonello12 in forum AWT / Swing
    Replies: 1
    Last Post: 10-10-2009, 06:33 AM
  5. JRadioButton set object color
    By King8654 in forum AWT / Swing
    Replies: 4
    Last Post: 04-24-2008, 09:52 AM

Posting Permissions

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