Steve Moore
Steve Moore

Reputation: 1

JButton not working with textfield

im having a little trouble with this GUI. Im new to making GUI's so if its something basic im sorry for asking. I cant seem to find the answer to my problem anywhere on google. The problem im having is when i click on any of the buttons i get an error when any one of the textfields are left empty. here is my code so far

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JOptionPane;

public class MainGUI extends JFrame implements ActionListener{

ArrayList<Object> list;
JTextField name, surname, age, height, weight;
JComboBox<String> sportList, gender;
JButton btnSave;
JButton btnExit;
JButton btnclrtxt;


/**
 * @param args
 */
public MainGUI() {
    super("Adding members");
    this.setLayout(new BorderLayout());
    JPanel panel = new JPanel(new FlowLayout());
    name = new JTextField(10);
    surname = new JTextField(10);
    age = new JTextField(10);
    height = new JTextField(5);
    weight = new JTextField(10);
    gender = new JComboBox<String>(new String[] { "Male", "Female" });
    sportList = new JComboBox<String>(new String[] { "Football",
            "HandBall", "BasketBall", "Rugby", "Hockey", "Tennis" });

    panel.add(new JLabel("Name:"));
    panel.add(name);
    panel.add(new JLabel("Surname:"));
    panel.add(surname);
    panel.add(new JLabel("Age:"));
    panel.add(age);
    panel.add(new JLabel("Gender:"));
    panel.add(gender);
    panel.add(new JLabel("Height:"));
    panel.add(height);
    panel.add(new JLabel("Weight:"));
    panel.add(weight);
    panel.add(new JLabel("Sport Speciality:"));
    panel.add(sportList);

    btnSave = new JButton("Save member");
    panel.add(btnSave);
    btnSave.addActionListener(this);

    btnclrtxt = new JButton("Clear text");
    panel.add(btnclrtxt);
    btnclrtxt.addActionListener(this);

    btnExit = new JButton("Exit");
    panel.add(btnExit);
    btnExit.addActionListener(this);

    this.pack();
    this.add(panel);
    this.setSize(400, 300);
    this.setVisible(true);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    new MainGUI();
          }

public void actionPerformed(ActionEvent e) {

            String namestr = name.getText().toLowerCase();
    String surnameStr = surname.getText().toLowerCase();
    int ageNum = Integer.parseInt(age.getText().toLowerCase());
    double heightNum = Double.parseDouble(height.getText().toLowerCase());
    double weightNum = Double.parseDouble(weight.getText().toLowerCase());
    String sportsStr = sportList.getSelectedItem().toString();
    String genderStr = gender.getSelectedItem().toString();

              if(e.getSource()==btnSave)  {
        list = new ArrayList<Object>();
        list.add("Name is "+namestr);
        list.add("Surname is "+surnameStr);
        list.add("Age is "+ageNum);
        list.add("Height is "+heightNum);
        list.add("Weight is "+weightNum);
        list.add("Sport "+sportsStr);
        list.add("Gender "+genderStr);
        FileRead.writeFile(list);
        System.out.println("Done!");
                }

    if(e.getSource()==btnExit)  {
                dispose();
                                    System.exit(0);
                           }

    if(e.getSource()==btnclrtxt)  {
         name.setText("");
         surname.setText("");
         age.setText("");
         height.setText("");
         weight.setText("");
               }
 }
}

Upvotes: 0

Views: 209

Answers (2)

I suspect this is because of these fields:

int ageNum = Integer.parseInt(age.getText().toLowerCase());
double heightNum = Double.parseDouble(height.getText().toLowerCase());
double weightNum = Double.parseDouble(weight.getText().toLowerCase());

The parsing will throw an exception if any of these fields is empty, or contains an incorrect value; make sure you check they contain a (valid) value. Use a JFormattedTextField, for example.

Upvotes: 2

mKorbel
mKorbel

Reputation: 109815

wotking, but you don't test if JTextField.isEmpty, then parsing throws correct exception, instead of that to use

  • JFormattedTextField with NumberFormatter, then default valur is 0 (zero)

  • JSpinner with SpinnerNumberModel, then default valur is 0 (zero)

  • add DocumentFilter to plain JTextField with filtering non_numeric chars, but still required to test if JTextField.isEmpty()

Upvotes: 2

Related Questions