Search code examples
javaswinguser-interfacejtextfield

I can't input data in the fields


I am working on this assignment for school. I have been looking my code over and over. The problem is that when I try and run this program I can't input data into the fields. When I try to select the fields to enter the data it won't let me. Someone please help me!

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class OfficeAreaCalculator extends JFrame{

private JFrame mainFrame;
private JButton calculateButton;
private JButton exitButton;
private JTextField lengthField;
private JTextField widthField;
private JTextField areaField;
private JLabel lengthLabel;
private JLabel widthLabel;
private JLabel areaLabel;

public OfficeAreaCalculator()
{
    mainFrame = new JFrame("Office Area Calculator");

    exitButton = new JButton("Exit");
    lengthLabel = new JLabel("Enter the length of the office:");
    widthLabel = new JLabel("Enter the width of the office:");
    areaLabel = new JLabel("Office area:");
    lengthField = new JTextField(5);
    widthField = new JTextField(5);
    areaField = new JTextField(5);
    areaField.setEditable(false);
    calculateButton = new JButton("Calculate");

    Container c = mainFrame.getContentPane();
    c.setLayout(new FlowLayout());

    c.add(lengthLabel);
    c.add(lengthField);
    c.add(widthLabel);
    c.add(widthField);
    c.add(areaLabel);
    c.add(areaField);
    c.add(calculateButton);
    c.add(exitButton);

    calculateButton.setMnemonic('C');
    exitButton.setMnemonic('X');

    mainFrame.setSize(260, 150);

    mainFrame.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    CalculateButtonHandler chandler = new CalculateButtonHandler();
    calculateButton.addActionListener(chandler);

    ExitButtonHandler ehandler = new ExitButtonHandler();
    exitButton.addActionListener(ehandler);

    FocusHandler fhandler = new FocusHandler();
    lengthField.addFocusListener(fhandler);
    widthField.addFocusListener(fhandler);
    areaField.addFocusListener(fhandler);

    mainFrame.setVisible(true);
}

class CalculateButtonHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        DecimalFormat num = new DecimalFormat(",###.##");
        double width, length, area;
        String instring;

        instring = lengthField.getText();
        if (instring.equals(""));
        {
            instring = ("0");
            lengthField.setText("0");
        }
        length = Double.parseDouble(instring);

        instring = widthField.getText();
        if (instring.equals(""))
        {
            instring = "0";
            widthField.setText("0");
        }
        width = Double.parseDouble(instring);

        area = length * width;
        areaField.setText(num.format(area));

    }

}

class ExitButtonHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e) {
        System.exit(0);
    }       
}

class FocusHandler implements FocusListener
{
    public void focusGained(FocusEvent e)
    {
        if (e.getSource() == lengthField || e.getSource() == widthField)
        {
            areaField.setText("");
        }
        else if (e.getSource() == areaField);
        {
            calculateButton.requestFocus();
        }
    }

    public void focusLost(FocusEvent e)
    {
        if (e.getSource() == widthField)
        {
            calculateButton.requestFocus();
        }
    }
}

public static void main(String arg[])
{
    new OfficeAreaCalculator();
}
}

Solution

  • General advice. Don't write your whole program all at once before you start testing.

    1) Just create the GUI with all your components. That is remove all the listener code and see if it works.

    2) Add back in the listeners one at a time and see if each listener works.

    3) When the code stops working then you know where the problem is and you will know where to spend your time doing your debugging and you will be able to give us more information to help solve the problem.

    As it turns out the problem is a typo:

        else if (e.getSource() == areaField);
    

    You should not have the ";" at the end of the statement, this is causing the calculateButton.requestFocus(); statement to be executed every time to try to place focus on either of the text fields.

    As a general rule you should NOT be using a FocusListener to try to control focus of components. Focus will move naturally when you use the Tab key. Your "areaField" should not really be a text field. It should probably be a JLabel which will never gain focus anyway so focus will go directly to the button from the width text field.