Getting Input from JFrame using ActionListener

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 7701 Views - Last Post: 28 November 2011 - 10:21 PM Rate Topic: -----

#1 daniel_poobalan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-November 11

Getting Input from JFrame using ActionListener

Posted 26 November 2011 - 10:23 PM

Hi guys!

I have an assignment and I need to come up with a programming code for calculating the total bill for a Saloon Center.
The program requires me to come up with an interface to key in customer's details like name ,IC no, date of treatment,etc
So the codes which I came out with are as follows:

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JFrame;
import javax.swing.JButton;

public class SaloonApp  {

	public static void main(String[]args){
		
		JFrame frame = new JFrame("Che Gayah's Saloon Center");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    JPanel panel =new JPanel(new GridLayout(30,30,10,10));
	    panel.add(new JLabel("Name:"));
	    panel.add(new JTextField(5));
	  	panel.add(new JLabel("IC:"));
	    panel.add(new JTextField(5));
	    panel.add(new JLabel("Member or non member:"));
	    JRadioButton Yes,No;
	    ButtonGroup buttonGroup =new ButtonGroup();
	 	Yes=new JRadioButton("Member (25% discount for each treatment)");
	    buttonGroup.add(Yes);
	    panel.add(Yes);
	    No=new JRadioButton("Non-member");
	    buttonGroup.add(No);
	    panel.add(No);
	    panel.add(new JLabel("Date of treatment:"));
	    panel.add(new JTextField(5));
	    panel.add(new JLabel("Please select a type of treatment:"));
	    JRadioButton Body,Hair;
	    ButtonGroup TypeOfTreatment= new ButtonGroup();
	    Body=new JRadioButton("Body");
	    TypeOfTreatment.add(Body);
	    panel.add(Body);
	    Hair=new JRadioButton("Hair");
	    TypeOfTreatment.add(Hair);
	    panel.add(Hair);
	    panel.add(new JLabel("For body treatment, please make your selection:"));
	    JRadioButton SeveralParts,AllParts;
	    ButtonGroup BTreatment =new ButtonGroup();
	    SeveralParts= new JRadioButton("Several parts - RM 320.00");
	    BTreatment.add(SeveralParts);
	    panel.add(SeveralParts);
	    AllParts=new JRadioButton("All parts - RM 600.00");
	    BTreatment.add(AllParts);
	    panel.add(AllParts);
	    panel.add(new JLabel("For hair treatment, please make your selection:"));
	    panel.add(new JLabel("Rebonding "));
	    JRadioButton Yes1,No1;
	    ButtonGroup Rebonding =new ButtonGroup();
	 	Yes1=new JRadioButton("Yes (RM 250.00)");
	    Rebonding.add(Yes1);
	    panel.add(Yes1);
	   	No1=new JRadioButton("No");
	   	Rebonding.add(No1);
	    panel.add(No1);
	    panel.add(new JLabel("Cutting"));
	    JRadioButton Yes2,No2;
	    ButtonGroup Cutting =new ButtonGroup();
	 	Yes2=new JRadioButton("Yes (RM 50.00)");
	    Cutting.add(Yes2);
	    panel.add(Yes2);
	    No2=new JRadioButton("No");
	    Cutting.add(No2);
	    panel.add(No2);
	    panel.add(new JLabel("Washing"));
	    JRadioButton Yes3,No3;
	    ButtonGroup Washing =new ButtonGroup();
	 	Yes3=new JRadioButton("Yes (RM 15.00)");
	    Washing.add(Yes3);
	    panel.add(Yes3);
	    No3=new JRadioButton("No");
	    Washing.add(No3);
	    panel.add(No3);
	    panel.add(new JLabel("Dyeing "));
	    JRadioButton Yes4,No4;
	    ButtonGroup Dyeing =new ButtonGroup();
	 	Yes4=new JRadioButton("Yes(RM 100.00)");
	    Dyeing.add(Yes4);
	    panel.add(Yes4);
	    No4=new JRadioButton("No");
	    Dyeing.add(No4);
	    panel.add(No4);
	    JButton button =new JButton("Submit");
	    button.addActionListener(new ActionListener(){
	    	  public void actionPerformed(ActionEvent ae){
	    	  String str = JOptionPane.showInputDialog(null, "Please enter your name : ", 
	    	"Customer info", 1);
	    	  if(str != null)
	    	  JOptionPane.showMessageDialog(null, "Name : " + str, 
	    	"Customer info", 1);
	    	  else
	    	  JOptionPane.showMessageDialog(null, "You did not enter your name. Please try again", 
	    	"Customer info", 1);
	    	  }
	    	  });
	    
	   
	    panel.add(button);
	    frame.add(panel);
	    frame.setSize(750, 750);
	    frame.setVisible(true);
	       
	    
	    	} 
	    
	    
	   
	
	}    
	

	
 


So, what I want is for the JOptionPane to display the details of the billing. If the customer is a member, he or she will receive a 25% discount on each treatment. I wanted the program to calculate the billing for each selection of the treatment. I did a search on ActionListener for JRadioButton but not sure if it works for this case. Any advice or feedback on this matter is highly appreciated. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Getting Input from JFrame using ActionListener

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: Getting Input from JFrame using ActionListener

Posted 27 November 2011 - 06:04 AM

Not sure why any listener is required at all ..? Why not just get the values of your radio buttons?
Was This Post Helpful? 0
  • +
  • -

#3 daniel_poobalan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-November 11

Re: Getting Input from JFrame using ActionListener

Posted 27 November 2011 - 06:19 AM

Thanks for your reply. Let me give it a try and see whether it works! :smile2:
Was This Post Helpful? 0
  • +
  • -

#4 daniel_poobalan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-November 11

Re: Getting Input from JFrame using ActionListener

Posted 27 November 2011 - 07:58 AM

View Postg00se, on 27 November 2011 - 06:04 AM, said:

Not sure why any listener is required at all ..? Why not just get the values of your radio buttons?

Just a quick question. When I did a search on Google on getting input from JRadioButton, the results are mostly related to Javascript.Do I need to use Javascript to get the values from the radio buttons? FYI,my course syllabus does not cover Javascript.As far as I know, Javascript is totally a different ball game all together.
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: Getting Input from JFrame using ActionListener

Posted 27 November 2011 - 09:38 AM

Quote

When I did a search on Google on getting input from JRadioButton...


Radio buttons don't have input. They have state (selected == false/true). All you need to do is call

http://docs.oracle.c...ml#isSelected()

They have events but we've already established that you probably don't need them

This post has been edited by g00se: 27 November 2011 - 09:40 AM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Getting Input from JFrame using ActionListener

Posted 27 November 2011 - 07:22 PM

View Postdaniel_poobalan, on 27 November 2011 - 10:58 AM, said:

When I did a search on Google on getting input from JRadioButton, the results are mostly related to Javascript

You didn't Google at the good place :)
Was This Post Helpful? 0
  • +
  • -

#7 daniel_poobalan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-November 11

Re: Getting Input from JFrame using ActionListener

Posted 27 November 2011 - 07:40 PM

View Postpbl, on 27 November 2011 - 07:22 PM, said:

View Postdaniel_poobalan, on 27 November 2011 - 10:58 AM, said:

When I did a search on Google on getting input from JRadioButton, the results are mostly related to Javascript

You didn't Google at the good place :)


Well, at least you have pointed out my mistake! :bigsmile: Actually I am taking this subject for the second time in my university because I have failed the subject last year. Then, after doing some research on the internet, I've finally found this site and I thought I wanted to give it a try. So, I hope that all of you will support me as I am also learning to develop my skills to be a good programmer!
Was This Post Helpful? 0
  • +
  • -

#8 daniel_poobalan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-November 11

Re: Getting Input from JFrame using ActionListener

Posted 27 November 2011 - 08:03 PM

While I was doing a search, I found this link which is related to my problem:
JRadioButton. Let me check it out whether it works for my program.
Was This Post Helpful? 0
  • +
  • -

#9 daniel_poobalan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-November 11

Re: Getting Input from JFrame using ActionListener

Posted 27 November 2011 - 08:33 PM

While I read Tarsus' post regarding the JRadioButton, I tried using the codes as suggested by you:
ButtonGroup bg = new ButtonGroup();
for(int i = 0; i < 10; i++) {
	JRadioButton rb = new JRadioButton("Button Number: " + i, i == 5);
	bg.add(rb);
}
	
Enumeration<AbstractButton> en = bg.getElements();
while(en.hasMoreElements()) {
	JRadioButton rb = (JRadioButton) en.nextElement();
	if(rb.isSelected())
		System.out.println(rb.getText() + " is selected");
}




I tried to run the program, but it did not capture the radio button's action. As suggested by Goose, I do not need the ActionListener. Now, I am confused. How do I get the program to capture the events of the radio buttons? :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: Getting Input from JFrame using ActionListener

Posted 28 November 2011 - 07:24 AM

Quote

How do I get the program to capture the events of the radio buttons?


Why do you need them - i thought you only needed to know which ones were selected..?
Was This Post Helpful? 0
  • +
  • -

#11 daniel_poobalan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-November 11

Re: Getting Input from JFrame using ActionListener

Posted 28 November 2011 - 07:40 AM

View Postg00se, on 28 November 2011 - 07:24 AM, said:

Quote

How do I get the program to capture the events of the radio buttons?


Why do you need them - i thought you only needed to know which ones were selected..?

I need to capture the price of each treatment selected because that is the purpose of the assignment. So, I came up with this idea of using JOptionPane for each treatment selected. For example,let's say I wanted to get treatment for several parts of my body, I would select Several Parts Of Body and a JOptionPane window will show up:
SeveralParts= new JRadioButton("Several parts - RM 320.00");
	    BTreatment.add(SeveralParts);
	    SeveralParts.addActionListener(new ActionListener(){
	    	  public void actionPerformed(ActionEvent ae){
	    	  String str = JOptionPane.showInputDialog(null, "Please input Treatment Type and Price", 
	    	"Customer info", 1);
	    	  if(str != null)
	    	  JOptionPane.showMessageDialog(null, "You have selected : " + str, 
	    	"Customer info", 1);
	    	  else
	    	  JOptionPane.showMessageDialog(null, "You did not enter your details. Please try again", 
	    	"Customer info", 1);
	    	  }
	    	  });



So, what I want is for the program to capture the price of the treatment from the JOptionPane but is this possible?
Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: Getting Input from JFrame using ActionListener

Posted 28 November 2011 - 08:23 AM

Quote

I need to capture the price of each treatment selected


I don't understand what the difficulty is. The price of the treatment would depend on whether a radio button is selected or not. Find that out using the method i gave you above whilst you're doing the total
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,467
  • Joined: 27-December 08

Re: Getting Input from JFrame using ActionListener

Posted 28 November 2011 - 09:42 AM

The only ActionListener you need is for the submit JButton. It doesn't matter which JRadioButton is selected until the user submits. This is why you don't need an ActionListener for the JRadioButtons.
Was This Post Helpful? 0
  • +
  • -

#14 daniel_poobalan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-November 11

Re: Getting Input from JFrame using ActionListener

Posted 28 November 2011 - 07:01 PM

Ok, so I have done some modifications to my program and here is the updated version of the Submit button:

button.addActionListener(new ActionListener(){
	    	public void actionPerformed(ActionEvent ae){
	    	    double severalPartsOfBody= 320.00;
	    	    double allPartsOfBody= 600.00;
	    	    double rebondingPrice=250.00;
	    	    double cuttingPrice= 50.00;
	    	    double washingPrice=15.00;
	    	    double dyeingPrice=100.00;
	    	    double totalPriceOfTreatment;//if body treatment is selected
	    	    double totalPriceOfTreatment2;// if hair treatment is selected
	    	    
		    	
		    	 totalPriceOfTreatment = severalPartsOfBody+ allPartsOfBody;
		    	 totalPriceOfTreatment2= rebondingPrice + cuttingPrice + washingPrice + dyeingPrice;
		    	 
		    	 if(Body.isSelected()==true);
		    	 JOptionPane.showMessageDialog(null, "Total price of treatment is: RM" +totalPriceOfTreatment);
		    	  if(Hair.isSelected()==true);
		    	 JOptionPane.showMessageDialog(null, "Total price of treatment is: RM" +totalPriceOfTreatment2);
		    	 if (SeveralParts.isSelected()==true)
		    	    	JOptionPane.showMessageDialog(null," Total price of treatment is"+  severalPartsOfBody);
		    	    
		    	 if(AllParts.isSelected()==true)  ;
			     JOptionPane.showMessageDialog(null,"Total price of treatment is:"+  allPartsOfBody);
			    	  
		    	 if(Yes1.isSelected()==true) ;
		    	 JOptionPane.showMessageDialog(null,"Total price of treatment is:" +rebondingPrice);
		    	  
		    	 if(Yes2.isSelected()==true);
		    	 JOptionPane.showMessageDialog(null, "Total price of treatment is:"+cuttingPrice);
		    	 
		    	 if(Yes3.isSelected()==true);
		    	 JOptionPane.showMessageDialog(null, "Total price of treatment is:"+washingPrice);
		    	 
		    	 if(Yes4.isSelected()==true);
		    	 JOptionPane.showMessageDialog(null, "Total price of treatment is:" +dyeingPrice);
		    	 
	    	}}); 
	    
	    



So, when I click the submit button, it displays the price of all treatments including the ones which I did not select. I have also encountered an error where I should declare all the radio buttons as "final"(e.g. final JButton
Yes1)How do I create a loop where it only displays the price of the selected treatment?
Was This Post Helpful? 0
  • +
  • -

#15 daniel_poobalan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-November 11

Re: Getting Input from JFrame using ActionListener

Posted 28 November 2011 - 07:06 PM

View Postdaniel_poobalan, on 28 November 2011 - 07:01 PM, said:

Ok, so I have done some modifications to my program and here is the updated version of the Submit button:

button.addActionListener(new ActionListener(){
	    	public void actionPerformed(ActionEvent ae){
	    	    double severalPartsOfBody= 320.00;
	    	    double allPartsOfBody= 600.00;
	    	    double rebondingPrice=250.00;
	    	    double cuttingPrice= 50.00;
	    	    double washingPrice=15.00;
	    	    double dyeingPrice=100.00;
	    	    double totalPriceOfTreatment;//if body treatment is selected
	    	    double totalPriceOfTreatment2;// if hair treatment is selected
	    	    
		    	
		    	 totalPriceOfTreatment = severalPartsOfBody+ allPartsOfBody;
		    	 totalPriceOfTreatment2= rebondingPrice + cuttingPrice + washingPrice + dyeingPrice;
		    	 
		    	 if(Body.isSelected()==true);
		    	 JOptionPane.showMessageDialog(null, "Total price of treatment is: RM" +totalPriceOfTreatment);
		    	  if(Hair.isSelected()==true);
		    	 JOptionPane.showMessageDialog(null, "Total price of treatment is: RM" +totalPriceOfTreatment2);
		    	 if (SeveralParts.isSelected()==true)
		    	    	JOptionPane.showMessageDialog(null," Total price of treatment is"+  severalPartsOfBody);
		    	    
		    	 if(AllParts.isSelected()==true)  ;
			     JOptionPane.showMessageDialog(null,"Total price of treatment is:"+  allPartsOfBody);
			    	  
		    	 if(Yes1.isSelected()==true) ;
		    	 JOptionPane.showMessageDialog(null,"Total price of treatment is:" +rebondingPrice);
		    	  
		    	 if(Yes2.isSelected()==true);
		    	 JOptionPane.showMessageDialog(null, "Total price of treatment is:"+cuttingPrice);
		    	 
		    	 if(Yes3.isSelected()==true);
		    	 JOptionPane.showMessageDialog(null, "Total price of treatment is:"+washingPrice);
		    	 
		    	 if(Yes4.isSelected()==true);
		    	 JOptionPane.showMessageDialog(null, "Total price of treatment is:" +dyeingPrice);
		    	 
	    	}}); 
	    
	    



So, when I click the submit button, it displays the price of all treatments including the ones which I did not select. I have also encountered an error where I should declare all the radio buttons as "final"(e.g. final JButton
Yes1)How do I create a loop where it only displays the price of the selected treatment?


Oops, I forgot to include the main program! Here is the code for it:
import javax.swing.*;

public class SaloonApp  {

	public static void main(String[]args){
		
		JFrame frame = new JFrame("Che Gayah's Saloon Center");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    JPanel panel =new JPanel(new GridLayout(30,30,10,10));
	    panel.add(new JLabel("Name:"));
	    panel.add(new JTextField(5));
	  	panel.add(new JLabel("IC:"));
	    panel.add(new JTextField(5));
	    panel.add(new JLabel("Member or non member:"));
	    JRadioButton Yes,No;
	    ButtonGroup buttonGroup =new ButtonGroup();
	    
	 	Yes=new JRadioButton("Member (25% discount for each treatment)");
	    buttonGroup.add(Yes);
	    panel.add(Yes);
	    No=new JRadioButton("Non-member");
	    buttonGroup.add(No);
	    panel.add(No);
	    
	    panel.add(new JLabel("Date of treatment:"));
	    panel.add(new JTextField(5));
	    panel.add(new JLabel("Please select a type of treatment:"));
	    final JRadioButton Body;
		final JRadioButton Hair;
	    ButtonGroup TypeOfTreatment= new ButtonGroup();
	    Body=new JRadioButton("Body");
	    TypeOfTreatment.add(Body);
	    
	    panel.add(Body);
	    Hair=new JRadioButton("Hair");
	    TypeOfTreatment.add(Hair);
	    panel.add(Hair);
	    panel.add(new JLabel("For body treatment, please make your selection:"));
	    final JRadioButton SeveralParts;
		final JRadioButton AllParts;
	    ButtonGroup BTreatment =new ButtonGroup();
	    
	    SeveralParts= new JRadioButton("Several parts - RM 320.00");
	    BTreatment.add(SeveralParts);
	    
	    panel.add(SeveralParts);
	    
	    AllParts=new JRadioButton("All parts - RM 600.00");
	    BTreatment.add(AllParts);
	    
	    panel.add(AllParts);
	    panel.add(new JLabel("For hair treatment, please make your selection:"));
	    panel.add(new JLabel("Rebonding "));
	    final JRadioButton Yes1;
		JRadioButton No1;
	    ButtonGroup Rebonding =new ButtonGroup();
	 	Yes1=new JRadioButton("Yes (RM 250.00)");
	    Rebonding.add(Yes1);
	    
	    panel.add(Yes1);
	   	No1=new JRadioButton("No");
	   	Rebonding.add(No1);
	    panel.add(No1);
	    panel.add(new JLabel("Cutting"));
	    final JRadioButton Yes2;
		JRadioButton No2;
	    ButtonGroup Cutting =new ButtonGroup();
	 	Yes2=new JRadioButton("Yes (RM 50.00)");
	 	
	    Cutting.add(Yes2);
	    panel.add(Yes2);
	    No2=new JRadioButton("No");
	    Cutting.add(No2);
	    panel.add(No2);
	    panel.add(new JLabel("Washing"));
	    final JRadioButton Yes3;
	    JRadioButton No3;
	    ButtonGroup Washing =new ButtonGroup();
	    Yes3=new JRadioButton("Yes (RM 15.00)");
	    Washing.add(Yes3);
	    panel.add(Yes3);
	    No3=new JRadioButton("No");
	    Washing.add(No3);
	    panel.add(No3);
	    panel.add(new JLabel("Dyeing "));
	    final JRadioButton Yes4;
	    JRadioButton No4;
	    ButtonGroup Dyeing =new ButtonGroup();
	    Yes4=new JRadioButton("Yes(RM 100.00)");
	    Dyeing.add(Yes4);
	    panel.add(Yes4);
	    No4=new JRadioButton("No");
	    Dyeing.add(No4);
	    panel.add(No4);
	    JButton button =new JButton("Submit");


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2