The Artima Developer Community
Sponsored Link

Java Answers Forum
Radio button help

2 replies on 1 page. Most recent reply: Jun 16, 2002 10:19 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
jake

Posts: 83
Nickname: onorok
Registered: May, 2002

Radio button help Posted: Jun 11, 2002 10:40 PM
Reply to this message Reply
Advertisement
NO matter what radio button I press I always get "true", what am I doing wrong???

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
 
public class newAccount extends JFrame
{
	//makes the text fields.
	TextField fname=new TextField();//First name field.
	TextField lname=new TextField();//Last name field.
	TextField pnumber=new TextField();//Phone number field.
	
	DoubleField intB=new DoubleField(1);//Interest account field.
	//DoubleField simpA=new DoubleField(1);//simple account field.
	
	//Makes the two radio buttons.
	JRadioButton simpleA=new JRadioButton("Simple",true);
	JRadioButton interestA=new JRadioButton("Interest Account",false);
	
	//Makes JButton.
	JButton ok=new JButton("done");
	public boolean A=true;
	
	public newAccount()
	{
		setSize(420,470);
		setLocation(350,200);
		
		//Makes the labels.
		JLabel title=new JLabel("New Account",SwingConstants.CENTER);
		title.setFont(new Font("Times New Roman",Font.BOLD,28));
		
		JLabel fn=new JLabel("First Name");
		fn.setFont(new Font("Times New Roman",Font.BOLD,20));
		
		JLabel ln=new JLabel("Last Name");
		ln.setFont(new Font("Times New Roman",Font.BOLD,20));
		
		JLabel pn=new JLabel("Phone Number");
		pn.setFont(new Font("Times New Roman",Font.BOLD,20));
		
		JLabel Atype=new JLabel("What type of Account would you like?");
		Atype.setFont(new Font("Times New Roman",Font.BOLD,18));
		
		JLabel interA=new JLabel("Base amount is $500");
		interA.setFont(new Font("Times New Roman",Font.BOLD,14));
		
		//Sets up the Radio Buttons.
		ButtonGroup typebutton=new ButtonGroup();
		typebutton.add(simpleA);
		typebutton.add(interestA);
		
		
		//Makes a new container
		Container contentPane=getContentPane();
		contentPane.setLayout(null);
		
		//Adds components to the frame.
		contentPane.add(title);//Fields.	
		contentPane.add(fname);
		contentPane.add(lname);
		contentPane.add(pnumber);
		contentPane.add(intB);
		//contentPane.add(simpA);
		contentPane.add(fn);//Labels.
		contentPane.add(ln);
		contentPane.add(pn);
		contentPane.add(interA);
		contentPane.add(Atype);
		contentPane.add(ok);//JButton.
		
		//Adds Radio Buttons.
		contentPane.add(simpleA);
		contentPane.add(interestA);
		
		
		Insets insets=contentPane.getInsets();
		
		//Sets component location.
		title.setBounds(50+ insets.left, 3+ insets.top,300, 50);//label.
		
		fname.setBounds(160+ insets.left, 80+ insets.top,200, 20);//Field.
		fn.setBounds(35+ insets.left, 80+ insets.top,100, 20);//Label.
		
		lname.setBounds(160+ insets.left, 120+ insets.top,200, 20);//Field.
		ln.setBounds(35+ insets.left, 120+ insets.top,100, 20);//Label.
		
		pnumber.setBounds(160+ insets.left, 160+ insets.top,200, 20);//Field.
		pn.setBounds(20+ insets.left, 160+ insets.top,200, 20);//Label.
		
		//Sets Radio buttons locations.
		Atype.setBounds(50+ insets.left, 230+ insets.top,300, 20);//Label.
		
		simpleA.setBounds(80+ insets.left, 265+ insets.top,200, 20);//Radio button.
		//simpA.setBounds(196+ insets.left, 265+ insets.top,50, 20);//Field.
		
		interestA.setBounds(80+ insets.left, 305+ insets.top,200, 20);//Radio button.
		intB.setBounds(196+ insets.left, 305+ insets.top,50, 20);//Field.
		interA.setBounds(280+ insets.left, 305+ insets.top,140, 20);//JLabel.
		
		ok.setBounds(100+ insets.left, 375+ insets.top,200, 30);//JButton.
		
		//Action events.
     	ProActionListener actionListener = new ProActionListener();
     	ok.addActionListener (actionListener);
			
	}
	
	 //Caries out all the actions of the game.
     private class ProActionListener implements ActionListener
     {
 
    	public void actionPerformed(ActionEvent event)
   		{
 
       		Object source=event.getSource();
			double S=0;
			//If simple account is pressed.
			if(source==simpleA)
			{
				A=true;
			}
			
			//If interest account is pressed.
			if(source==interestA)
			{
				//S=intB.getDoubleValue();
				A=false;
			}
			
			//If ok button is pressed
       		if(source==ok)
       		{
       			Main_menu.thebank.addCustomer(fname.getText(),lname.getText(),
       				pnumber.getText(),A,S);	
       			//Main_menu.bankmain.insert(Bank.C.displayCustomer()+"",0);
       			setVisible(false);
       			
       		}
 
       	}
     }
     
     /*public static void main(String[]args)
     {
     	newAccount o=new newAccount();
     	o.show();
     }*/
}
 
 
 
 
 
 
 


Chris Maguire

Posts: 4
Nickname: lulu
Registered: Jun, 2002

Re: Radio button help Posted: Jun 12, 2002 6:14 AM
Reply to this message Reply
You're checking to see if the event source is one of the JRadioButtons but you haven't added the action listener to those JRadioButtons.
Try adding the event listener to them.

Another thing you might try is to add an action command to each JRadioButton so that you don't have to hard code the value in the event listener code.

See the example here:
http://java.sun.com/docs/books/tutorial/uiswing/components/button.html#radiobutton

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Radio button help Posted: Jun 16, 2002 10:19 PM
Reply to this message Reply
all you need to do is add some lines of code as was suggested:

simpleA.addActionListener(new ProActionListener());
interestA.addActionListener(new ProActionListener());

just like you did to your OK button.

Flat View: This topic has 2 replies on 1 page
Topic: How to add java application to the taskbar tray? Previous Topic   Next Topic Topic: Minimise frame

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use