I am creating a program that lets user input the values of function x, snip of the code below:
JLabel f1= new JLabel (" x1 = ");
JLabel f2 = new JLabel (" x2 = ");
JButton submit = new JButton("Submit values");
submit.addActionListener(this);
JTextField x1 = new JTextField();
JTextField x2 = new JTextField();
inputPanel.add(f1);
inputPanel.add(x1);
inputPanel.add(f2);
inputPanel.add(x2);
inputPanel.add(submit);
It looks something like this:
x1 = [input field] x2 =[input field] (submit values)
My ActionPerformed method looks like this:
public void actionPerformed(ActionEvent e)
{
if("submit".equals(e.getActionCommand()))
{
System.out.println("click");
}
}
I've added the System.out.println to check if program knows when I click the submit button, but there is nothing being printed out to the console, my question is why and how do I change it?
and another thing I want to ask is how can I take the input of both x1, x2 at the same time? I understand that I will probably need an if function to check if none of the fields are blank?
The problems is with your actionCommand.
JButton submit = new JButton("Submit values");
If you don't explicitly set an action command, it reverts to the Button text
.
If your button text is too long or inconvenient to use then do the following:
submit.setActionCommand("submit");