Hello everyone-

I'm currently working on a GUI solution for a school assignment which creates a tabbed window with only the 'Pool' tab actually populated. I have successfully created all of the labels, textfields and buttons for the 'pool' pane, but I am really struggling with setting up the listener for the 'Calculate' button. This button needs to generate the following: get the user input from the length, width and depth textboxs, parse into integers, and then multiply together to form the volume which should display in the last textfield. I have tried several approaches but to no avail. My window displays all of the components correctly and allows the user to enter values into field 1-3, but when the calculate button is pressed nothing seems to happen. Any help would be greatly appreciated.
My code looks like this:

package fp;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;




public class FinalProject extends JFrame
{
	private JTextField txt1;
	private JTextField txt2;
	private JTextField txt3;
	private JTextField txt4;
	
	public FinalProject()
	{
		//set msg for title bar
		setTitle("Final Project");
        
		//create the main pane for the tabs
		JTabbedPane jtp = new JTabbedPane();
	        getContentPane().add(jtp);     
	        
	        //create the tabbed panes for the main frame	        
	        JPanel jp1 = new JPanel();
	        jtp.addTab("GENERAL", jp1);
	        JPanel jp2 = new JPanel();
	        jtp.addTab("OPTIONS", jp2);
	        JPanel jp3 = new JPanel();
	        jtp.addTab("CUSTOMERS", jp3);
	        JPanel jp4 = new JPanel();
	        jtp.addTab("CONTRACTORS", jp4);
	        JPanel jp5 = new JPanel();
	        jtp.addTab("POOL", jp5);
	        jp5.doLayout();      
	            
	        JPanel jp6 = new JPanel();
	        jtp.addTab("HOT TUB", jp6);
	        JPanel jp7 = new JPanel();
	        jtp.addTab("DEPTH CALC", jp7);	        
	        JPanel jp8 = new JPanel();
	        jtp.addTab("LENGTH CALC", jp8);
	        
	        //Create panels within the pool pane
	        JPanel poolPanel1 = new JPanel();
	        jp5.add(poolPanel1);
	        JPanel poolPanel2 = new JPanel();
	        jp5.add(poolPanel2);
	        JPanel poolPanel3 = new JPanel();
	        jp5.add(poolPanel3);
	        JPanel poolPanel4 = new JPanel();
	        jp5.add(poolPanel4);
	        JPanel poolPanel5 = new JPanel();
	        jp5.add(poolPanel5);
	        
	    	//create the components and add to the pool pane 
	    	JLabel plabel1 = new JLabel();
	    	plabel1.setText("Enter the pool's length(ft): ");
	    	poolPanel1.add(plabel1);
	        JTextField txt1 = new JTextField(4);	        
	        poolPanel1.add(txt1);	        
	        	    	
	        JLabel plabel2 = new JLabel();
	        plabel2.setText("Enter the pool's width(ft): ");
	        poolPanel2.add(plabel2);
	        JTextField txt2 = new JTextField(4);
	        poolPanel2.add(txt2);	         
	        
	        JLabel plabel3 = new JLabel();
	        plabel3.setText("Enter the pool's average depth(ft): ");
	        poolPanel3.add(plabel3);
	        JTextField txt3 = new JTextField(2);
	        poolPanel3.add(txt3);
	                
	        JLabel plabel4 = new JLabel();
	        plabel4.setText("The volume of the swimming pool is: ");
	        poolPanel4.add(plabel4);
	        JTextField txt4 = new JTextField(4);
	        poolPanel4.add(txt4);            
    		
	        JButton calcBtn = new JButton();
	        calcBtn.setText("Calculate");
	        poolPanel5.add(calcBtn);
	        calcBtn.addActionListener(new CalcBtnListener());
	        		     
	        
	        JButton clrBtn = new JButton();
	        clrBtn.setText("Clear");
	        poolPanel5.add(clrBtn);      
	            
	}//end FinalProject() Constructor

	//create inner class for CalcBtnListener
	class CalcBtnListener implements ActionListener
	{
	public void actionPerformed(ActionEvent e)
		{
			int length = Integer.parseInt(txt1.getText());
			int width = Integer.parseInt(txt2.getText());
			int depth = Integer.parseInt(txt3.getText());
			int volume = length*width*depth;
	        txt4.setText(" " + volume);
        }
	};
	//end inner class
		
	//main method
	public static void main(String[] args)
	{
		FinalProject fl = new FinalProject();
		fl.setLocationRelativeTo(null);
		fl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		fl.setVisible(true);
		fl.setSize(400, 300);	
	}//end main 
	
}

be sure doing

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at Frm.FinalProject$CalcBtnListener.actionPerformed(FinalProject.java:88)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6263)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
        at java.awt.Component.processEvent(Component.java:6028)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4630)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2478)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Member Avatar for ztini

You need to initialize your JTextFields with strings if you want the text. For instance, JTextField txt1 = new JTextField("1");

By passing just an integer (new JTextField(1)) that is setting the number of columns in the JTextField.

See the API for more information.

Ok-well after 24 hours of ripping hair out, I just realized my button was updating the class variable, but not my instance variable. I removed the datatype in the the following line to make the button update the instance variable instead.

txt4 = new JTextField(4);

DUH Being a noob is hard, lol.

no wrote that correctly

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class FinalProject {

    private static final long serialVersionUID = 1L;
    private JTextField txt1;
    private JTextField txt2;
    private JTextField txt3;
    private JTextField txt4;
    private JFrame frm;

    public FinalProject() {
        JTabbedPane jtp = new JTabbedPane();
        JPanel jp1 = new JPanel();
        jtp.addTab("GENERAL", jp1);
        JPanel jp2 = new JPanel();
        jtp.addTab("OPTIONS", jp2);
        JPanel jp3 = new JPanel();
        jtp.addTab("CUSTOMERS", jp3);
        JPanel jp4 = new JPanel();
        jtp.addTab("CONTRACTORS", jp4);
        JPanel jp5 = new JPanel();
        jtp.addTab("POOL", jp5);
        jp5.doLayout();
        JPanel jp6 = new JPanel();
        jtp.addTab("HOT TUB", jp6);
        JPanel jp7 = new JPanel();
        jtp.addTab("DEPTH CALC", jp7);
        JPanel jp8 = new JPanel();
        jtp.addTab("LENGTH CALC", jp8);
        JPanel poolPanel1 = new JPanel();
        jp5.add(poolPanel1);
        JPanel poolPanel2 = new JPanel();
        jp5.add(poolPanel2);
        JPanel poolPanel3 = new JPanel();
        jp5.add(poolPanel3);
        JPanel poolPanel4 = new JPanel();
        jp5.add(poolPanel4);
        JPanel poolPanel5 = new JPanel();
        jp5.add(poolPanel5);
        JLabel plabel1 = new JLabel();
        plabel1.setText("Enter the pool's length(ft): ");
        poolPanel1.add(plabel1);
        txt1 = new JTextField(4);
        poolPanel1.add(txt1);
        JLabel plabel2 = new JLabel();
        plabel2.setText("Enter the pool's width(ft): ");
        poolPanel2.add(plabel2);
        txt2 = new JTextField(4);
        poolPanel2.add(txt2);
        JLabel plabel3 = new JLabel();
        plabel3.setText("Enter the pool's average depth(ft): ");
        poolPanel3.add(plabel3);
        txt3 = new JTextField(2);
        poolPanel3.add(txt3);
        JLabel plabel4 = new JLabel();
        plabel4.setText("The volume of the swimming pool is: ");
        poolPanel4.add(plabel4);
        txt4 = new JTextField(4);
        poolPanel4.add(txt4);
        JButton calcBtn = new JButton();
        calcBtn.setText("Calculate");
        poolPanel5.add(calcBtn);
        calcBtn.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calcBtnActionPerformed(evt);
            }

            private void calcBtnActionPerformed(ActionEvent evt) {
                int length = Integer.parseInt(txt1.getText());
                int width = Integer.parseInt(txt2.getText());
                int depth = Integer.parseInt(txt3.getText());
                int volume = length * width * depth;
                txt4.setText(" " + volume);
            }
        });
        JButton clrBtn = new JButton();
        clrBtn.setText("Clear");
        poolPanel5.add(clrBtn);
        frm = new JFrame("Final Project");
        frm.add(jtp, BorderLayout.CENTER);
        frm.setLocationRelativeTo(null);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setSize(400, 300);
        frm.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                FinalProject fl = new FinalProject();
            }
        });
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.