stuck in a Java program

Discussion in 'OT Technology' started by unknown_soldier, Mar 1, 2005.

  1. stuck programming a GUI in Java

    I know there are rules about doing homework of others, but I need help with the concept. I've been messing with this for two weeks and can't figure it out, so I made a similar program (not identical) so that I could post for help.

    Situation: I have to have two radio buttons in the menu bar, and use a different formula to calculate different values (one formula for one radio button, one formula for the other). I'm not sure how to have a button verify which Radio button is chosen and therefor which formula to use. I think I have everything in there properly, just maybe not in the proper order or within the right constructs.

    I have set this one up to calculate temperatures, one converting from Fahrenheit to Celsius, the other vice versa. Can anyone show me what I'm missing? I've tried it a few different ways.
    If I remove the first two lines of the "Calculate" button code(right below this paragraph), the calculation works perfect. How can I make the "if" statements work within the button action?
    -- String item = e.getActionCommand();
    -- if (item.equals("Fahrenheit to Celsius"));

    Can anyone help? :bowdown:

    Code:
    import javax.swing.*;
    import java.text.*;
    import java.lang.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Temp extends JFrame implements ActionListener{
       private JMenuItem exitMenuItem, convertMenuItem;  					//top menu's
       private JButton exitButton, calculateButton; 						//buttons
       private JCheckBoxMenuItem resizableMenuItem;
       private JRadioButtonMenuItem ftocMenuItem, ctofMenuItem;
       private JTextField oneTextField, twoTextField;
    
       public Temp(){
          setTitle("Menu Test");
          Toolkit tk = Toolkit.getDefaultToolkit();
          Dimension d = tk.getScreenSize();
          int width = 250;
          int height = 250;
          setBounds((d.width - width)/2, (d.height - height)/2, width, height);
          setResizable(false);
          addWindowListener(new WindowAdapter(){
             public void windowClosing(WindowEvent e){
                System.exit(0);
             }
          });
    	               JPanel panel = new JPanel();              		    //create the panel
    	     	       panel.setLayout(new FlowLayout(FlowLayout.CENTER));
    	     	       calculateButton = new JButton("Calculate");          //create the buttons
    	     	       calculateButton.addActionListener(this);
    	     	       exitButton = new JButton("Exit");
    			       exitButton.addActionListener(this);
    		     	   panel.add(calculateButton);
    	     	       panel.add(exitButton);
    
    	     	       Container contentPane = getContentPane();    	   //BorderLayout by default
    	     	       contentPane.add(panel, BorderLayout.SOUTH);   	   //add the panel
    
    
          JMenu fileMenu = new JMenu("File");                             //create the menu bar
          fileMenu.setMnemonic('F');
          exitMenuItem = new JMenuItem("Exit", 'x');
          exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,
             Event.CTRL_MASK));
          fileMenu.add(exitMenuItem);
    
          JMenu toolsMenu = new JMenu("Tools");
          toolsMenu.setMnemonic('T');
    
          resizableMenuItem = new JCheckBoxMenuItem("Resizable");
          resizableMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,
             Event.CTRL_MASK));
    
          toolsMenu.add(resizableMenuItem);
          toolsMenu.addSeparator();
    
          exitMenuItem.addActionListener(this);
          resizableMenuItem.addActionListener(this);
    
          convertMenuItem = new JMenu("Convert");
          convertMenuItem.setMnemonic('C');
          ButtonGroup group = new ButtonGroup();
          ftocMenuItem = new JRadioButtonMenuItem("Fahrenheit to Celsius", true);		//F to C
          ctofMenuItem = new JRadioButtonMenuItem("Celsius to Fahrenheit");				//C to F
    			group.add(ftocMenuItem);  //add radio buttons to group
    		  	group.add(ctofMenuItem);  //add radio buttons to group
          convertMenuItem.add(ftocMenuItem);
          convertMenuItem.add(ctofMenuItem);
    
    	  ftocMenuItem.addActionListener(this);
    	  ctofMenuItem.addActionListener(this);
    
          toolsMenu.add(convertMenuItem);
    
          JMenuBar menuBar = new JMenuBar();
          menuBar.add(fileMenu);
          menuBar.add(toolsMenu);
    
          setJMenuBar(menuBar);
    
    			  JPanel textPanel = new JPanel();              		    //create the panel
    			  textPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    
    //////////////////////////////////////////////////////////////////////
    				 				 oneTextField = new JTextField(5);
    								 twoTextField = new JTextField(5);
    								 textPanel.add(oneTextField);
    								 textPanel.add(twoTextField);
    //////////////////////////////////////////////////////////////////////
               twoTextField.setEditable(false);
         	       contentPane.add(textPanel, BorderLayout.NORTH);   	   //add the panel
    
      }
    
         public void actionPerformed(ActionEvent e){
        try{  Object source = e.getSource();
                if (source instanceof JMenuItem){                 //Menu ActionEvents
                   JMenuItem item = (JMenuItem) source;
                   String text = item.getText();
                   if (text.equals("Exit"))
                      System.exit(0);
                   if (text.equals("Resizable"))
                      JOptionPane.showMessageDialog
                          (null, "The ability to resize the frame has been changed.");
                   if (text.equals("Resizable")){
    				 if (resizableMenuItem.isSelected())
    				 	setResizable(true);
    				 else
    					setResizable(false);
    					}
    //////////////////Removed to test other code////////////////////////////
    /*			   String item = e.getActionCommand();
    			   				NumberFormat number = NumberFormat.getNumberInstance();
    			   			   	if (text.equals("Fahrenheit to Celsius")){
    			   					  String dataString = oneTextField.getText();
    			   							double data = Double.parseDouble(dataString);
    			   							double dataConv = (data-32)*5/9;
    			   							String dataConvString = number.format(dataConv);
    			   				}
    			   				if (text.equals("Celcius to Fahrenheit")){
    			   					  String dataString = oneTextField.getText();
    			   						 double data = Double.parseDouble(dataString);
    			   						 double dataConv = (data*5/9)+32;
    			   						 String dataConvString = number.format(dataConv);
    							}
    */
    //////////////////////////////////////////////
    
    
             }
    
    		NumberFormat number = NumberFormat.getNumberInstance();
    
    				 if (source == exitButton) System.exit(0);    //Button ActionEvents
    		         else if (source == calculateButton) {
    //if I remove this line and the one below, calculation works//		String item = e.getActionCommand();
    						//	if (item.equals("Fahrenheit to Celsius"))
    							{
    									String dataString = oneTextField.getText();
    				 					double data = Double.parseDouble(dataString);
    				 					double dataConv = (data-32)*5/9;
    				 					String dataConvString = number.format(dataConv);
    									twoTextField.setText(dataConvString);
    								}
    														//							     String dataString = oneTextField.getText();
    														//								 double data = Double.parseDouble(dataString);
    														//								 String dataConvString = number.format(dataConv);
    														//								 twoTextField.setText(dataConvString);
    
    	/*String item = e.getActionCommand();
            if (item.equals(ftocMenuItem)){
    			  String dataString = oneTextField.getText();
    					double data = Double.parseDouble(dataString);
    					double dataConv = (data-32)*5/9;
    					String dataConvString = number.format(dataConv);
    					twoTextField.setText(dataConvString);
    		}
    		 else if (item.equals(ctofMenuItem)){
    			  String dataString = oneTextField.getText();
    				 double data = Double.parseDouble(dataString);
    				 double dataConv = (data*5/9)+32;
    				 String dataConvString = number.format(dataConv);
    				 twoTextField.setText(dataConvString);
    					}*/
    		 }
    	}
     	 catch (NumberFormatException nfe){
    		 JOptionPane.showMessageDialog(null, "Number Format Exception: \n"
    		 								+ "Make Sure Numbers are entered.");
    								}
             }
    
       public static void main(String[] args){
          JFrame frame = new Temp();
          frame.show();
       }
    }
    
     
    Last edited by a moderator: Mar 1, 2005
  2. MrMan

    MrMan New Member

    Joined:
    Jul 13, 2004
    Messages:
    308
    Likes Received:
    0
    I really do not like Java, and I haven't done it in a while. I also didn't fully look at your code, only skimmed it. I searched and saw that you do not have the function getSelectedCheckbox() which can see which checkbox you selected. More information below:

    http://java.sun.com/j2se/1.3/docs/api/java/awt/CheckboxGroup.html#getSelectedCheckbox()
     
  3. this is what I was trying to make work:

    String item = e.getActionCommand();
    if (item.equals(ftocMenuItem)){
    String dataString = oneTextField.getText();
    double data = Double.parseDouble(dataString);
    double dataConv = (data-32)*5/9;
    String dataConvString = number.format(dataConv);
    twoTextField.setText(dataConvString);
    }
    else if (item.equals(ctofMenuItem)){
    String dataString = oneTextField.getText();
    double data = Double.parseDouble(dataString);
    double dataConv = (data*5/9)+32;
    String dataConvString = number.format(dataConv);
    twoTextField.setText(dataConvString);
    }


    I've looked through all of those pages trying to figure it out, but I can't see what I'm missing in the radio buttons. The code works if I take out the "if" statement, but without the if statements, I don't know how to distringuish which formulas to use
     
    Last edited by a moderator: Mar 1, 2005
  4. MrMan

    MrMan New Member

    Joined:
    Jul 13, 2004
    Messages:
    308
    Likes Received:
    0
    Hmm, well I know it's not working because you're trying to compare a String to JRadioButtonMenuItem

    String item = e.getActionCommand();
    if (item.equals(ftocMenuItem))

    item is of type String, and ftocMenuItem is a JRadioButtonMenuItem. Not sure how to make it work, I played around with it for a while and can't figure it out.
     
  5. thanks for your help and troubles. I may just put the entire project on hold for a while and come back later, see if I can figure it out with a clear mind.
     
  6. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    i didnt read most of your code, but i think i get what you're doing. the easiest way to do it is:
    public void actionPerformed(ActionEvent e)
    {
    if(ftocMenuItem.isSelected())
    calculeteCelcius(whatever varialbes);
    else if(ctofMenuItem.isSelected())
    calculateFahr(whatever variables);
    }
     

Share This Page