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 14 of 14

Thread: Changing my GUI help

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Changing my GUI help

    I hope this is posted in the correct forum, I have a question about changing my GUI. This is for school so I do not want anyone to write the code for me, but pointing me to a beginners tutorial on Java GUI's that I can view offline would be great as I only have limited online access (have to drive 20 minutes to get it). I currently have the following code for a mortgage calculator:

     
    package mortgageCalc;
     
    import java.awt.*;
    import java.text.NumberFormat;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
     
     
    public class MortgageCalc extends JFrame {  //This creates all the buttons and windows
     
    	JLabel firstNumLabel = new JLabel("Mortgage Principal:");
        JTextField firstNumField = new JTextField("0");
        JLabel secondNumLabel = new JLabel("Mortgage Terms (in years):");
        JTextField secondNumField = new JTextField("0");
        JLabel thirdNumLabel = new JLabel("Annual Percentage Rate (APR)");
        JTextField thirdNumField = new JTextField("0");
        JLabel resultLabel = new JLabel("Payment Amount:");
        JTextField resultField = new JTextField("0");
        JButton calcButton = new JButton("Calculate Payment");
        JButton closeButton = new JButton("Close");
        JButton resetButton = new JButton("Reset Calculator");
        public int pymtAmt = 0;
        public int intRate = 0;
        public int monMorTerms = 0;
     
     
        public MortgageCalc() {  //this creates the GUI
            super("Mortgage Calculator");
            setLocation(500, 200);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
     
            setLayout(new GridLayout(6, 2, 10, 10));
     
            add(firstNumLabel);
            add(firstNumField);
            add(secondNumLabel);
            add(secondNumField);
            add(thirdNumLabel);
            add(thirdNumField);
            add(resultLabel);
            add(resultField);
            add(calcButton);
            add(closeButton);
            add(resetButton);
     
            resultField.setEditable(false);
     
            closeButton.addActionListener(  //defines what the close button does
                    new ActionListener() {
     
                        public void actionPerformed(ActionEvent evt) {
                            System.exit(0);
                        }
                    });
     
            calcButton.addActionListener( //defines what the calculate button does
                    new ActionListener() {
     
                        public void actionPerformed(ActionEvent evt) {
                            String firstNumText = firstNumField.getText();
                            String secondNumText = secondNumField.getText();
                            String thirdNumText = thirdNumField.getText();
                            double firstNum = Double.parseDouble(firstNumText);
                            double secondNum = Double.parseDouble(secondNumText);
                            double thirdNum = Double.parseDouble(thirdNumText);
                            double intRate = thirdNum / 100 / 12;
                            double monMorTerms = secondNum * 12;
                            double result = (firstNum * intRate) / (1 - Math.pow(1 + intRate, -monMorTerms));
                            NumberFormat nf = NumberFormat.getCurrencyInstance();
                            resultField.setText("" + nf.format(result));
                        }
                    });
     
            resetButton.addActionListener(new ActionListener() { //defines what the reset button does
            	public void actionPerformed(ActionEvent e) {
            		firstNumField.setText("0");
            		secondNumField.setText("0");
            		thirdNumField.setText("0");
            		resultField.setText("");
            	}
    		});
     
     
            pack();
            setVisible(true);
        }
     
        public static void main(String[] args) { //creates a new instance of the MortgageCalc class
            MortgageCalc app = new MortgageCalc();
        }
    }


    Now I have to alter this to have a drop down box with choices of loans such as 7 years at 5.35%, 15 years at 5.5%, or 30 years at 5.75%, then use an array for the mortgage data for the different loans. Display the mortgage payment amount followed by the loan balance and interest paid for each payment over the term of the loan. Allow the user to loop back and enter a new amount and make a new selection or quit.

    So far when trying to alter my program, I thought the following code would display a similar window, removing some of the previous buttons and adding a checkbox, but so far it has errors so I am not sure if I am headed in the right direction or not.

     
    package weekThree;
     
    import java.awt.*;
    import java.text.NumberFormat;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
     
    //This class creates a Mortgage Calculator in it's own window with buttons and text fields used to calculate mortgage payments
     
    public class MortgageCalc extends JFrame {  //This creates all the buttons and windows
     
    	JLabel firstNumLabel = new JLabel("Mortgage Principal:");
        JTextField firstNumField = new JTextField("0");
        JButton calcButton = new JButton("Calculate Payment");
        JButton closeButton = new JButton("Close");
        JButton resetButton = new JButton("Reset Calculator");
        public int pymtAmt = 0;
        public int intRate = 0;
        public int monMorTerms = 0;
        JComboBox mortgageChoices;
     
        public JPanel createComboBox() {
        	mortgageChoices = new JComboBox();
        	mortgageChoices.addItem("7 years at 5.35%");
        	mortgageChoices.addItem("15 years at 5.5%");
        	mortgageChoices.addItem("30 years at 5.75%");
        	mortgageChoices.setEditable(false);
        	JPanel panel = new JPanel();
    		return panel;
     
        }
     
     
        public MortgageCalc() {  //this creates the GUI
            super("Mortgage Calculator");
            setLocation(500, 200);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
     
            setLayout(new GridLayout(6, 2, 10, 10));
     
            add(firstNumLabel);
            add(firstNumField);
            add(mortgageChoices);
            add(calcButton);
            add(closeButton);
            add(resetButton);
     
     
     
            closeButton.addActionListener(  //defines what the close button does
                    new ActionListener() {
     
                        public void actionPerformed(ActionEvent evt) {
                            System.exit(0);
                        }
                    });
     
            calcButton.addActionListener( //defines what the calculate button does
                    new ActionListener() {
     
                        public void actionPerformed(ActionEvent evt) {
                            String firstNumText = firstNumField.getText();
                            double firstNum = Double.parseDouble(firstNumText);
                            NumberFormat nf = NumberFormat.getCurrencyInstance();
     
                        }
                    });
     
            resetButton.addActionListener(new ActionListener() { //defines what the reset button does
            	public void actionPerformed(ActionEvent e) {
            		firstNumField.setText("0");
            	}
    		});
     
     
            pack();
            setVisible(true);
        }
     
        public static void main(String[] args) { //creates a new instance of the MortgageCalc class
            MortgageCalc app = new MortgageCalc();
        }
    }

    I am not sure if I am creating the combobox in the right place or if it should be somewhere else. Any suggestions as to downloadable video tutorials or pdfs would be great. I am currently reading Big Java and Beginning Java, but they seem to be a little over my head. I have also read Beginning Java Programming for Dummies but it did not really cover GUI's to this extent. Again I do not wish for anyone to write it for me, but suggestions as to how I should do it or better yet to learning materials I can grasp and read offline would be great. Thank you.
    Last edited by Xrrak; August 7th, 2011 at 05:15 PM.


  2. #2
    Junior Member
    Join Date
    Apr 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing my GUI help

    Sorry I tried editing to show the correct indentations, but it did not work.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,354
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Changing my GUI help

    to show the correct indentations
    Wrap your code in code tags: BB Code List - Java Programming Forums

    Here are some links for the Tutorial:
    The Java™ Tutorials
    The Really Big Index
    Trail: Essential Classes: Table of Contents (The Java™ Tutorials)

  4. #4
    Junior Member
    Join Date
    Apr 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing my GUI help

    Thank you for both the indention tip as well as the links!

  5. #5
    Junior Member
    Join Date
    Apr 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing my GUI help

    I thought I had made some progress with my combobox but now I am getting an illegal start of an expression error at the main method and I am not sure why.

    import java.awt.*;
    import java.text.NumberFormat;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import static java.lang.Math.*;
     
     
    public class MortgageCalc extends JFrame {  //This creates all the buttons and windows
        JLabel firstNumLabel = new JLabel("Mortgage Principal:");
        JTextField firstNumField = new JTextField("0");
        JLabel resultLabel = new JLabel("Payment Amount:");
        JTextField resultField = new JTextField("0");
        JLabel comboOptions = new JLabel("Term Options");
        JButton calcButton = new JButton("Calculate Payment");
        JButton closeButton = new JButton("Close");
        JButton resetButton = new JButton("Reset Calculator");
        public int pymtAmt = 0;
        public double intRate = 0;
        public int monMorTerms = 0;
        String[] loanTerms = { "7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%" };
        JComboBox mortgageTerms = new JComboBox(loanTerms);
     
     
     
     
     
        public MortgageCalc() {  //this creates an instance of the MortgageCalc class
            super("Mortgage Calculator"); //this makes a title for the window
            setLocation(500, 200); //this stes the location of the window
            this.setDefaultCloseOperation(EXIT_ON_CLOSE); //this states the program will end when the window is closed
     
            setLayout(new GridLayout(6, 2, 10, 10)); //this defines the layout of the window
     
            add(firstNumLabel); //these lines add the elements to the window
            add(firstNumField);
            add(comboOptions);
            add(mortgageTerms);
            add(resultLabel);
            add(resultField);
            add(calcButton);
            add(closeButton);
            add(resetButton);
     
            resultField.setEditable(false); //this indicates the result field can not be edited by the user
     
            mortgageTerms.addActionListener(
                    new ActionListener() {
     
                        public void actionPerformed(ActionEvent evt) {
                            switch (mortgageTerms.getSelectedIndex()) {
                                case 0: monMorTerms = 84; intRate = 5.35 / 100 / 12;     break;
                                case 1: monMorTerms = 180; intRate = 5.5 / 100 / 12;    break;
                                case 2: monMorTerms = 360; intRate = 5.75 / 100 / 12; break;
                            }
                    });
     
     
     
            closeButton.addActionListener(  //defines what the close button does
                    new ActionListener() {
     
                        public void actionPerformed(ActionEvent evt) {
                            System.exit(0);
                        }
                    });
     
            calcButton.addActionListener( //defines what the calculate button does
                    new ActionListener() {
     
                        public void actionPerformed(ActionEvent evt) {
                            String firstNumText = firstNumField.getText();
                            double firstNum = Double.parseDouble(firstNumText);
                            double result = (firstNum * intRate) / (1 - Math.pow(1 + intRate, -monMorTerms));
                            NumberFormat nf = NumberFormat.getCurrencyInstance();
                            resultField.setText("" + nf.format(result));
                        }
                    });
     
            resetButton.addActionListener(new ActionListener() { //defines what the reset button does
            	public void actionPerformed(ActionEvent e) {
            		firstNumField.setText("0");
            		resultField.setText("");
     
                    }
     
    	});
     
     
            pack();
            setVisible(true);
     
     
            public static void main(String[] args) { //creates a new instance of the MortgageCalc class
                MortgageCalc app = new MortgageCalc();
        }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,354
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Changing my GUI help

    am getting an illegal start of an expression error
    Please copy and paste here the full text of the error message.

    Is there a missing }?

  7. #7
    Junior Member
    Join Date
    Apr 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing my GUI help

    This is the full text of the error shown in NetBeans IDE:

    illegal start of expression

    Illegal static declaration in inner class <anonymous week3.MortgageCalc$1>
    modifier 'static' is only allowed in constant variable declarations

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,354
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Changing my GUI help

    illegal start of expression
    That's not much use without the source line number. Where in your program does the IDE say the problem is?
    Did you check for matching or missing { }s?
    That error message means that the compiler is confused by the statement it read. It was expecting something besides what it read.

    Illegal static declaration in inner class <anonymous week3.MortgageCalc$1>
    modifier 'static' is only allowed in constant variable declarations
    Again. Where is this error? What lines in the program?

  9. #9
    Junior Member
    Join Date
    Apr 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing my GUI help

    Sorry the error is flagged at lines 100 and 105. I followed the same format as the other listeners, like lines 75 through 85. I have tried adding a } but that just made more errors.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,354
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Changing my GUI help

    the error is flagged at lines 100 and 105
    But I can't see the contents of your editor to know which lines those are.
    Please post lines 100 thru 105 with comments on them showing their line numbers

    Where did you add the }?

    You need to go through all of your code and make sure that all the {s are correctly paired with a }.

    Some editors have a function that will find matching {}s when you put the cursor on the leading { it will find the matching }
    Last edited by Norm; August 10th, 2011 at 02:41 PM.

  11. #11
    Junior Member
    Join Date
    Apr 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing my GUI help

    Again sorry line 100 is: public static void main(String[] args) { //creates a new instance of the MortgageCalc class and line 105 is the last }. I have tried adding the } between the last two }.

    the full section of 100 through 105 is:

            public static void main(String[] args) { //creates a new instance of the MortgageCalc class
                MortgageCalc app = new MortgageCalc();
        }
     
     
    }

    and the format I used is the same as the section:
            calcButton.addActionListener( //defines what the calculate button does
                    new ActionListener() {
     
                        public void actionPerformed(ActionEvent evt) {
                            String firstNumText = firstNumField.getText();
                            double firstNum = Double.parseDouble(firstNumText);
                            double result = (firstNum * intRate) / (1 - Math.pow(1 + intRate, -monMorTerms));
                            NumberFormat nf = NumberFormat.getCurrencyInstance();
                            resultField.setText("" + nf.format(result));
                        }
                    });

  12. #12
    Junior Member
    Join Date
    Apr 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing my GUI help

    And I am referring to using the format when I added the section:
            mortgageTerms.addActionListener(
                    new ActionListener() {
     
                        public void actionPerformed(ActionEvent evt) {
                            switch (mortgageTerms.getSelectedIndex()) {
                                case 0: monMorTerms = 84; intRate = 5.35 / 100 / 12;     break;
                                case 1: monMorTerms = 180; intRate = 5.5 / 100 / 12;    break;
                                case 2: monMorTerms = 360; intRate = 5.75 / 100 / 12; break;
                            }
    which is when the error appeared.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,354
    Thanks
    49
    Thanked 2,206 Times in 2,179 Posts

    Default Re: Changing my GUI help

    You need to go through all of your code and make sure that all the {s are correctly paired with a }.

  14. #14
    Junior Member
    Join Date
    Apr 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Changing my GUI help

    Thanks Norm, thank goodness NetBeans IDE has a feature where you can highlight one { or } and the corresponding one highlights as well. I was missing a couple. Didn't realize the IDE did that until I went digging lol. Thanks again.

Similar Threads

  1. changing GUI
    By timmin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 3rd, 2011, 08:16 AM
  2. Changing how a Window is drawn.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 8th, 2011, 08:16 PM
  3. Boolean Value Not Changing
    By bosox960 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 21st, 2010, 02:11 PM
  4. Parameter's value is never changing
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 27th, 2009, 05:29 AM