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

Thread: GUI Not Loading

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

    Default GUI Not Loading

    I'm trying to make a basic GUI program that computes interest based on a few user-specified values.

    When I run the driver without adding a listener to my buttons, everything works properly, but when I go to add the listener to the button, the GUI doesn't show when I run the driver.

    Driver class:

    package calculator;
     
    import javax.swing.JFrame;
    import javax.swing.WindowConstants;
     
     
    public class InterestGUI {
     
    	public static void createAndShowGUI() {
    		JFrame frame = new JFrame("Interest Calculator");
    		frame.setContentPane(new calcGUI());
    		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    		frame.pack();
    		frame.setVisible(true);
    	}
     
    	public static void main(String[] args) {
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				createAndShowGUI();
    			}
    		});
    	}
    }

    GUI Class
    package calculator;
     
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JTextField;
     
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.NumberFormat;
     
    public class calcGUI extends JPanel{
     
        private JButton simpleInterestButton = new JButton("Compute Simple Interest");
        private JButton compoundInterestButton = new JButton("Compute Compound Interest");
        private JLabel principal = new JLabel("Principal:");
        private JLabel rate = new JLabel("Rate(Percentage):");
        private JLabel years = new JLabel("Years:");
        private JTextField principalInput = new JTextField(7);
        private JTextField rateInput = new JTextField(7);
        private JTextField yearsInput = new JTextField(7);
        private JLabel interestResult = new JLabel(interestTotal);
        private static String interestTotal;
        private JPanel topPanel = new JPanel();
        private JPanel midPanel = new JPanel();
        private JPanel bottomPanel = new JPanel();
     
    	public class simpleInterestButtonListener implements ActionListener {
    		private String principalValStr = principalInput.getText();
    		private double principalValInt = Integer.parseInt(principalValStr.trim());
    		private String rateValStr = rateInput.getText();
    		private double rateValInt = Integer.parseInt(rateValStr.trim());
    		private String yearsValStr = yearsInput.getText();
    		private double yearsValInt = Integer.parseInt(yearsValStr.trim());
    		private double simpleInterest = 0;
    		private String simpleInterestStr = "";
     
    		public void actionPerformed(ActionEvent arg0) {
    			simpleInterest = principalValInt + (principalValInt*(rateValInt/100)*yearsValInt);
    			simpleInterestStr = NumberFormat.getCurrencyInstance().format(simpleInterest);
    			interestTotal = "Computed simple interest is " + simpleInterestStr;
    		}
    	}
     
    	public calcGUI() {
    		topPanel.add(principal);
    		topPanel.add(principalInput);
    		topPanel.add(rate);
    		topPanel.add(rateInput);
    		topPanel.add(years);
    		topPanel.add(yearsInput);
    		add(topPanel);
    		simpleInterestButton.addActionListener(new simpleInterestButtonListener());
    		midPanel.add(simpleInterestButton);
     
    		compoundInterestButton.addActionListener(new ActionListener() {
    			private String principalValStr = principalInput.getText();
    			private double principalValInt = Integer.parseInt(principalValStr.trim());
    			private String rateValStr = rateInput.getText();
    			private double rateValInt = Integer.parseInt(rateValStr.trim());
    			private String yearsValStr = yearsInput.getText();
    			private double yearsValInt = Integer.parseInt(yearsValStr.trim());
    			private double compoundInterest = 0;
    			private String compoundInterestStr = "";
     
    			public void actionPerformed(ActionEvent arg0) {
    				compoundInterest = (int) (principalValInt*Math.pow((1+rateValInt/100),yearsValInt));
    				compoundInterestStr = NumberFormat.getCurrencyInstance().format(compoundInterest);
    				interestTotal = "Computed simple interest is " + compoundInterestStr;
    			}
    		});
    		midPanel.add(compoundInterestButton);
    		add(midPanel);
    		bottomPanel.add(interestResult);
    		add(bottomPanel);
    	}
    }
    Last edited by ADAMWD; February 23rd, 2011 at 02:15 PM.


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

    Default Re: GUI Not Loading

    I figured out that I was placing the variables in the incorrect spot for the button listener class, so now the GUI shows everything properly, except for the interestResult JLabel which is supposed to display the computed interest.

    Right now, nothing happens when either button is pressed. Also, how can I format it so that the buttons are below the text fields, and the interestResult label below the buttons, rather than everything being in a line.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: GUI Not Loading

    Quote Originally Posted by ADAMWD View Post
    Also, how can I format it so that the buttons are below the text fields, and the interestResult label below the buttons, rather than everything being in a line.
    Recommended reading: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: GUI Not Loading

    Quote Originally Posted by ADAMWD View Post
    Right now, nothing happens when either button is pressed.
    You want to set the interestTotal into the interestResult label after each calculation.

Similar Threads

  1. Loading array
    By joshft91 in forum Collections and Generics
    Replies: 3
    Last Post: January 19th, 2011, 11:30 AM
  2. Loading a file in a different folder
    By tiemykim in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 15th, 2010, 08:53 AM
  3. Loading in images
    By eXcellion in forum Java Theory & Questions
    Replies: 3
    Last Post: January 17th, 2010, 12:13 PM
  4. Prob in Loading Textures
    By sikriyogesh in forum Java Theory & Questions
    Replies: 0
    Last Post: December 15th, 2009, 05:59 AM
  5. Replies: 3
    Last Post: April 14th, 2009, 08:35 AM