# Java ActionListener?



## magnethead (Mar 18, 2006)

Can anybody see anything too opbvious in this? It's for beginner Computer Science class and nobody sees anything obviously wrong? It keeps going to else else (print line 9)


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

public class UsingFrames {
	JFrame frame;
	JPanel panel;
	JButton jb1;
	JLabel jl1;
	JLabel jl2;
	JLabel jl3;
	JLabel jl4;
	JLabel jl5;
	JLabel jl6;
	JTextField tf1;
	JButton jb2;
	JButton jb3;
	double output;
	ButtonGroup bg1;
	JRadioButton rb1;
	JRadioButton rb2;
	JRadioButton rb3;
	ButtonGroup bg2;
	JRadioButton rb4;
	JRadioButton rb5;
	JRadioButton rb6;
	
	
	UsingFrames(){
		initialize();
	}
	
	private void initialize(){
		frame = new JFrame("Java Based Thermal Converter");
		panel = new JPanel();
		
		frame.getContentPane().add(panel);
		
		panel.setLayout(new GridLayout(0,3,5,5));
				
		
				bg1 = new ButtonGroup();
						rb1 = new JRadioButton("From Fahrenheit");
							rb1.setFocusable(false);
							rb1.addActionListener(new ButtonHandler());
							rb1.setActionCommand("FF");
							rb1.setSelected(true);							
					bg1.add(rb1);
					panel.add(rb1);
					
						rb2 = new JRadioButton("From Celsius");
							rb2.setFocusable(false);
							rb2.addActionListener(new ButtonHandler());
							rb1.setActionCommand("FC");							
					bg1.add(rb2);
					panel.add(rb2);
					
						rb3 = new JRadioButton("From Kelvin");
							rb3.setFocusable(false);
							rb3.addActionListener(new ButtonHandler());	
							rb1.setActionCommand("FK");													
					bg1.add(rb3);
					panel.add(rb3);


			jl3 = new JLabel(" ");
		panel.add(jl3);		
					
			tf1 = new JTextField("0.0");
		 	tf1.setPreferredSize(new Dimension(50,25));			
		panel.add(tf1);
					
			jl4 = new JLabel(" ");
		panel.add(jl4);	
									
		
		bg2 = new ButtonGroup();
						rb4 = new JRadioButton("To Fahrenheit");
							rb4.setFocusable(false);
							rb4.addActionListener(new ButtonHandler());	
							rb4.setActionCommand("TF");
							rb4.setSelected(true);														
					bg2.add(rb4);
					panel.add(rb4);
					
						rb5 = new JRadioButton("To Celsius");
							rb5.setFocusable(false);
							rb5.addActionListener(new ButtonHandler());
							rb5.setActionCommand("TC");
					bg2.add(rb5);
					panel.add(rb5);
					
						rb6 = new JRadioButton("To Kelvin");
							rb6.setFocusable(false);
							rb6.addActionListener(new ButtonHandler());	
							rb6.setActionCommand("TK");					
					bg2.add(rb6);
					panel.add(rb6);
		           
		
			jb2 = new JButton("Quit");
			jb2.addActionListener(new ButtonHandler());
		panel.add(jb2);
		

			jb3 = new JButton("Convert");
			jb3.addActionListener(new ButtonHandler());
		panel.add(jb3);
		
			
			jb1 = new JButton("Clear");
			jb1.addActionListener(new ButtonHandler());
		panel.add(jb1);
				
		frame.pack();
		frame.setVisible(true);
		
				
	}
	class ButtonHandler implements ActionListener{
		
 			public void actionPerformed(ActionEvent e) {
 				String option = e.getActionCommand();
 				if (option.equals("Convert"))
 				{ 
 				//if from F	
 					if(bg1.getSelection().equals("FF")){
 						if(bg2.getSelection().equals("TF")){
 								System.out.println("1");
				  				String input = tf1.getText();
				 				output = Double.parseDouble(input);
				 				output = output;
				
				 				tf1.setText(Double.toString(output)); 							
 							}
 						else if(bg2.getSelection().equals("TC")){
 								System.out.println("2"); 							
				  				String input = tf1.getText();
				 				output = Double.parseDouble(input);
				 				output = (output - 32.0) * 5.0 / 9.0;
				
				 				tf1.setText(Double.toString(output)); 							
 							} 
 						else {
 								System.out.println("2"); 							
				  				String input = tf1.getText();
				 				output = Double.parseDouble(input);
				 				output = (output - 32.0) * 5.0 / 9.0;
				 				output = output + 273.15;
				
				 				tf1.setText(Double.toString(output));  								
 							} 															
 						}
 				//if from C
 					else if(bg1.getSelection().equals("FC")){
 					
 						if(bg2.getSelection().equals("TF")){
 								System.out.println("4"); 							
				  				String input = tf1.getText();
				 				output = Double.parseDouble(input);
				 				output = (output  * 9.0 / 5.0) + 32.0;
				
				 				tf1.setText(Double.toString(output)); 							
 							}
 						else if(bg2.getSelection().equals("TC")){
 								System.out.println("5"); 							
				  				String input = tf1.getText();
				 				output = Double.parseDouble(input);
				 				output = output;
				
				 				tf1.setText(Double.toString(output)); 							
 							} 
 						else {
 								System.out.println("6"); 							
				  				String input = tf1.getText();
				 				output = Double.parseDouble(input);
				 				output = output + 273.15;
				
				 				tf1.setText(Double.toString(output));	 							
 							} 					 						
 						
 					} 					
 				//if from K
 					else {
 						if(bg2.getSelection().equals("TF")){
 								System.out.println("7"); 							
				  				String input = tf1.getText();
				 				output = Double.parseDouble(input);
				 				output = ((output * 9)/5) - 459.67;
				
				 				tf1.setText(Double.toString(output)); 							
 							}
 						else if(bg2.getSelection().equals("TC")){
 								System.out.println("8"); 							
				  				String input = tf1.getText();
				 				output = Double.parseDouble(input);
				 				output = output - 273.15;
				
				 				tf1.setText(Double.toString(output)); 							
 							} 
 						else {
 								System.out.println("9"); 							
				  				String input = tf1.getText();
				 				output = Double.parseDouble(input);
				 				output = output;
				
				 				tf1.setText(Double.toString(output)); 							
 							} 					 						
 					}
 					
 				
 				} 												
  				else if (e.getActionCommand().equals("Clear"))
  					{
 						tf1.setText("0.0");	
 					}
 				else if (e.getActionCommand().equals("Quit"))
 					{
 						System.exit(0);
 					}
 					
 				}	
 		 	
	}
	

         
}



class UsingFrames_App{
	       public static void main(String [] args){
        	UsingFrames uf = new UsingFrames();

               	
        	
        }

	
	
}

//			rbtnF.setFont(new Font("SansSerif", Font.BOLD, 16));
//			rbtnF.setMnemonic('F');
```


----------



## magnethead (Mar 18, 2006)

can't do an actionlistener onmn a button group- MUCH easier to just use *radio button name*.isSelected(){....

rb1.isSelected(){....} etc

works now, i just need to teach JCreator to compile and spit out a jar file.


----------

