Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: code issue, should be silple been working on it for four hours

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default code issue, should be silple been working on it for four hours

    Any idea why this is not working. public void setResultValue2() should be putting data in the " textArea2.setText(Double.toString(result2));"

     
    //public class week2 {
    	import javax.swing.*;
     
    	import java.awt.*;
    	import java.awt.event.*;
    	import java.text.*;
    	import java.util.EventObject;
    	import javax.swing.*;
    	import javax.swing.border.Border;
    import java.awt.*;
     
     
     
    	// Creates class for class
    	public class week3o4 extends JFrame implements ActionListener
    	{
    		private static final long serialVersionUID = 8887922790267066604L;
    		// Radio Button Selection
    	JPanel rowOne = new JPanel();
    	JButton SELECTIONbutton1 = new JButton ("7 Years/5.35%");
    	JButton SELECTIONbutton2 = new JButton("15 Years/5.5%");
    	JButton SELECTIONbutton3 = new JButton("30 Years/5.75%");	
     
     
     
    	JPanel R2 = new JPanel();
    	JLabel PRINCIPLE = new JLabel("Principle Amount in Dollars:   ", JLabel.LEFT);
    	JTextField MORTGAGEpaymentTEXT = new JTextField(10);
    	JLabel TERMlabel = new JLabel("Mortgage Term in Years:   ", JLabel.LEFT);
    	JTextField TERMtextBOX = new JTextField(8);
    	JLabel INTERESTtextLABEL = new JLabel("Interest Rate Percentage:   ", JLabel.LEFT);
    	JTextField INTERESTrateTEXT = new JTextField(5);
    	JLabel MONTHLYpayLABEL = new JLabel("Monthly Payment $   ", JLabel.LEFT);
    	JTextField MPAYTEXT = new JTextField(10);
    	JLabel PRINCIPLEPAYMENT = new JLabel("Principle Amount without interest   ", JLabel.LEFT);  ////
    	JTextField MORTGAGEpaymentbox = new JTextField(10);	
     
     
    	// Create buttons to calculate payment,exit and clear fields
    	JPanel BOTTOMrow = new JPanel();
    	JButton calcBu = new JButton("CALCULATE");
    	JButton clBu = new JButton("CLEAR");
    	JButton exBu = new JButton("EXIT");
     
     
    	public week3o4(){
    	super("CALCULATOR");
    	setSize(400,500);
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	Container pane = getContentPane();
    	FlowLayout layout = new FlowLayout (FlowLayout.CENTER);
    	pane.setLayout(layout);
     
     
     
     
    		// Add to the screen
    	FlowLayout layout1 = new FlowLayout();
    	rowOne.setLayout(layout1);
     
    		// Add  Button Group
    	ButtonGroup btnGrp = new ButtonGroup();
    	btnGrp.add(SELECTIONbutton1);
    	btnGrp.add(SELECTIONbutton2);
    	btnGrp.add(SELECTIONbutton3);
    	rowOne.add(SELECTIONbutton1);
    	rowOne.add(SELECTIONbutton2);
    	rowOne.add(SELECTIONbutton3);
    	pane.add(rowOne);
     
    		// Create a border around the radio buttons
    	Border titledRadioBorder =
    	BorderFactory.createTitledBorder("Choose one");
    	rowOne.setBorder(titledRadioBorder);
     
    		// Listeners
    	SELECTIONbutton1.addActionListener(this);
    	SELECTIONbutton2.addActionListener(this);
    	SELECTIONbutton3.addActionListener(this);
     
     
     
     
     
     
    	// text boxes and labels and places default values
    	GridLayout layout2 = new GridLayout (4, 1);
    	R2.setLayout(layout2);
    	R2.add(PRINCIPLE);
    	R2.add(MORTGAGEpaymentTEXT);
    	//MORTGAGEpaymentTEXT.setText("200000");
    	R2.add(TERMlabel);
    	R2.add(TERMtextBOX);
    	//TERMtextBOX.setText("30");
    	R2.add(INTERESTtextLABEL);
    	R2.add(INTERESTrateTEXT);
    	//INTERESTrateTEXT.setText("5.75");
    	R2.add(MONTHLYpayLABEL);
    	R2.add(MPAYTEXT);
    	MPAYTEXT.setEnabled(false);
     
     
     
    	pane.add(R2);
     
     
    	// Create Label for Text Box
    	JPanel rowFour = new JPanel();
    	JLabel paymentLabel = new JLabel ("Payment #");
    	JLabel balLabel = new JLabel (" Balance");
    	JLabel ytdPrincLabel = new JLabel (" Principal");
    	JLabel ytdIntLabel = new JLabel (" Interest");
     
    	// Display area
    	JPanel rowFive = new JPanel(new FlowLayout());
    	JTextArea textArea2 = new JTextArea(10, 31);
    	JScrollPane scroll = new JScrollPane(textArea2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
    	JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
     
     
    	// Add Text Box Label
    	FlowLayout layout4 = new FlowLayout (FlowLayout.LEFT, 10, 10);
    	rowFour.setLayout (layout4);
    	rowFour.add(paymentLabel);
    	rowFour.add(balLabel);
    	rowFour.add(ytdPrincLabel);
    	rowFour.add(ytdIntLabel);
    	pane.add(rowFour);
     
    	// Add Text Box
    	FlowLayout layout5 = new FlowLayout (FlowLayout.CENTER, 10, 10);
    	rowFive.setLayout(layout5);
    	rowFive.add(scroll);
    	pane.add(rowFive);
    	setContentPane(pane);
    	setVisible(true);
    	//JTextField textArea2 = new JTextField(10);
    	//R2.add(textArea2); ////////
    	//textArea2.setEnabled(false); /////////
     
     
     
    	// format adding the buttons to the row   
    	FlowLayout LO3 = new FlowLayout (FlowLayout.CENTER, 10, 10);
    	BOTTOMrow.setLayout (LO3);
    	BOTTOMrow.add(calcBu);
    	BOTTOMrow.add(clBu);
    	BOTTOMrow.add(exBu);
    	pane.add(BOTTOMrow);
     
    	// listeners
    	calcBu.addActionListener(this);
    	clBu.addActionListener(this);
    	exBu.addActionListener(this);
    	setContentPane(pane);
    	setVisible(true);
    	}
     
    	public static void main(String[] args)throws ParseException {
    	       @SuppressWarnings("unused")
    		week3o4 frame = new week3o4();
    	}
     
    	public void setResultValue()
    	 {
    	   double PRINCIPLE = Double.parseDouble(MORTGAGEpaymentTEXT.getText());
    	   double term = Double.parseDouble(TERMtextBOX.getText());
    	   double rate = Double.parseDouble(INTERESTrateTEXT.getText()) / 100.;
    	   double result = (PRINCIPLE * ( rate/12))/(1-( Math.pow (1/( 1 +(rate/12)), (term*12))));
    	   //equation cited from [url=http://www.hughchou.org/calc/formula.html]Mortgage calculations -- how loan amortization works[/url]
     
    	   MPAYTEXT.setText(Double.toString(result));
    	}         
     
    	public void setResultValue2()
    	{
    		double PRINCIPLE = Double.parseDouble(MORTGAGEpaymentTEXT.getText());
    		double term = Double.parseDouble(TERMtextBOX.getText());
    		double rate = Double.parseDouble(INTERESTrateTEXT.getText());
    		double result2 = (PRINCIPLE * ( rate/12))/(1-( Math.pow (1/( 1 +(rate/12)), (term*12))));
     
     
     
     
    	} 
     
     
    	public void actionPerformed(ActionEvent event) {
    		 if (event.getSource() == SELECTIONbutton1) {
    		    	MORTGAGEpaymentTEXT.setText("200000");;
    		    	MPAYTEXT.setText("");;
    		    	INTERESTrateTEXT.setText("5.35");;
    		    	TERMtextBOX.setText("7");;
    		    }
    		 if (event.getSource() == SELECTIONbutton2) {
    		  	    MORTGAGEpaymentTEXT.setText("200000");;
    		  	    MPAYTEXT.setText("");;
    		  	    INTERESTrateTEXT.setText("5.50");;
    		  	    TERMtextBOX.setText("15");;
    		    }
    		 if (event.getSource() == SELECTIONbutton3) {
    			  	 MORTGAGEpaymentTEXT.setText("200000");;
    			  	 MPAYTEXT.setText("");;
    			  	 INTERESTrateTEXT.setText("5.75");;
    			  	 TERMtextBOX.setText("30");;
    		    }
     
    		if (event.getSource() == calcBu) {
    	        setResultValue();
    			setResultValue2();}
     
    	    if (event.getSource() == clBu) {
    	    	MORTGAGEpaymentTEXT.setText("");;
    	    	MPAYTEXT.setText("");;
    	    	INTERESTrateTEXT.setText("");;
    	    	TERMtextBOX.setText("");;
    	    }
    	Object command = event.getSource();
    		if (command == exBu){
    	System.exit(0);
    	}
    	}
    	}
    //code citations from Mortgage calculator with GUI - DevX.com Forums
    Last edited by helloworld922; September 29th, 2011 at 07:23 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: code issue, should be silple been working on it for four hours

    why this is not working.
    Could you please explain what is "not working"?
    Post the full text of the error messages if any.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: code issue, should be silple been working on it for four hours

    There is not an error message. Just a lack of action. if you run it as an app there is a text box in the bottom that needs to receive data. public void setResultValue2() should be setting text in textArea2. At this point I don't care about the math I just want some product to show up there.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: code issue, should be silple been working on it for four hours

    Where in your code is the action to happen?
    Where is this method call?
    textArea2.setText(

  5. #5

    Default Re: code issue, should be silple been working on it for four hours

    You aren't doing anything after calculating...
    public void setResultValue2()
    	{
    		double PRINCIPLE = Double.parseDouble(MORTGAGEpaymentTEXT.getText());
    		double term = Double.parseDouble(TERMtextBOX.getText());
    		double rate = Double.parseDouble(INTERESTrateTEXT.getText());
    		double result2 = (PRINCIPLE * ( rate/12))/(1-( Math.pow (1/( 1 +(rate/12)), (term*12))));
    	}
    You should be either returning something after this (if the return type wasn't void) or setting the Text yourself.

    public void setResultValue2()
    	{
    		double PRINCIPLE = Double.parseDouble(MORTGAGEpaymentTEXT.getText());
    		double term = Double.parseDouble(TERMtextBOX.getText());
    		double rate = Double.parseDouble(INTERESTrateTEXT.getText());
    		double result2 = (PRINCIPLE * ( rate/12))/(1-( Math.pow (1/( 1 +(rate/12)), (term*12))));
                    // textArea2.setText(Double.toString(result2)); //this is your code, but I do not see anything that defines textArea2
                    // maybe you got your variable names confused
    	}
    Last edited by kenster421; September 30th, 2011 at 01:56 PM. Reason: Syntax Highlighting
    Kenneth Walter
    Software Developer
    http://kennywalter.com

Similar Threads

  1. Replies: 4
    Last Post: August 10th, 2011, 11:16 AM
  2. My code is not working
    By mike2452 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 9th, 2011, 06:17 AM
  3. Getting code working with gui
    By Nostromo in forum AWT / Java Swing
    Replies: 2
    Last Post: March 21st, 2011, 09:34 PM
  4. Code working, but not the way I thought...
    By JLogan3o13 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 28th, 2010, 01:34 PM
  5. Code Stop working after converting to jar?
    By Ron6566 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 16th, 2010, 12:17 PM