Hi all,

I am having trouble with class communication to reset some textfields through event handling. Any help would be appreciated. Here is a portion of my code.

The class that contains the textfields that I wish to use a reset button for:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class FinancingInformation {
	
	JTextField basePriceText;
	JTextField downPaymentText;
	JTextField salesTaxText;
	private JLabel basePriceLabel, downPaymentLabel, salesTaxLabel;
	private TitledBorder financingInformationBorder;
	private JPanel financingInformationPanel;
	
	public FinancingInformation(){
		
		financingInformationPanel = new JPanel();
		
		basePriceLabel = new JLabel("Base Price: $");
		downPaymentLabel = new JLabel("Down Payment: $");
		salesTaxLabel = new JLabel("Sales Tax: $");
				
		basePriceText = new JTextField("0.0");
		downPaymentText = new JTextField("0.0");
		salesTaxText = new JTextField("7.0");
		
		financingInformationPanel.setLayout(new GridLayout(3,2));
		financingInformationPanel.add(basePriceLabel); financingInformationPanel.add(basePriceText);
		financingInformationPanel.add(downPaymentLabel); financingInformationPanel.add(downPaymentText);
		financingInformationPanel.add(salesTaxLabel); financingInformationPanel.add(salesTaxText);
		
		financingInformationBorder = new TitledBorder("Financing Information");
		financingInformationPanel.setBorder(financingInformationBorder);
	}
	
	public JPanel getFI()
	{
		return financingInformationPanel;
	}
	
	public JTextField getBasePriceText() {
		return basePriceText;
	}

	public void setBasePriceText(JTextField basePriceText) {
		this.basePriceText = basePriceText;
	}

	public JTextField getDownPaymentText() {
		return downPaymentText;
	}

	public void setDownPaymentText(JTextField downPaymentText) {
		this.downPaymentText = downPaymentText;
	}

	public JTextField getSalesTaxText() {
		return salesTaxText;
	}

	public void setSalesTaxText(JTextField salesTaxText) {
		this.salesTaxText = salesTaxText;
	}
}

The button class:

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.*;

public class ActionButtons {
	private JButton calc, reset, exit;
	private JPanel buttonPanel;
	private TitledBorder buttonBorder;
	private FinancingInformation fi;
	
	public ActionButtons(){
		
		buttonPanel = new JPanel();
		
		fi = new FinancingInformation();
		
		calc = new JButton("Calculate");
		reset = new JButton("Reset");
		exit = new JButton("Exit");
		
		calc.addActionListener(new CalcListener());
		reset.addActionListener(new ResetListener());
		exit.addActionListener(new ExitListener());
		
		buttonPanel.setLayout(new GridLayout(1,3));
		buttonPanel.add(calc); buttonPanel.add(reset); buttonPanel.add(exit);
		
		buttonBorder = new TitledBorder("Action Buttons");
		buttonPanel.setBorder(buttonBorder);
	}
	
	private class CalcListener implements ActionListener
	{
		public void actionPerformed(ActionEvent cb)
		{
			
		}
	}
	
	private class ResetListener implements ActionListener
	{
		public void actionPerformed(ActionEvent rb)
		{
			fi.basePriceText.setText("0.0");
			fi.downPaymentText.setText("0.0");
			fi.salesTaxText.setText("7.0");
		}
	}
	
	private class ExitListener implements ActionListener
	{
		public void actionPerformed(ActionEvent eb)
		{
			System.exit(0);
		}
	}
	
	public JPanel getAB()
	{
		return buttonPanel;
	}
	
}

As of now when I run the program, when I click the reset button, nothing happens. There are not even any error messages. Thanks in advance for any help!

nothing to your question, but consider using JFormattedTextField instead of JTextField, because Number instance allows only Digits and DecimalSeparator

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.