Reputation: 53
I have been trying to create a JFrame program that takes two numbers and an operation(inside jcombobox) to calculate the answer. I need to take the user input for number 1 and 2 and assign the value to an int that can be used in the calculation of the answer. num1 is the int variable and num1field is the name of the textfield.
num1field.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
num1 = Integer.parseInt(num1field.getText());
num1field.setText(num1);
}
}
);
And yes the num1 int has already been declared at the top of the class. I am getting an error where it says setText.
Thanks for all the help :)
Upvotes: 0
Views: 4674
Reputation: 347334
There is no method JTextField#setText(int)
, you can only supply a String
num1field.setText(String.valueOf(num1));
Should work
You may like to take a look at How to use Formatted Text Fields and How to use Spinners which may provide you with better functionality for what you are trying to achieve
Updated with example of idea how to calculate resulting value
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
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.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class QuickCalc {
public static void main(String[] args) {
new QuickCalc();
}
public QuickCalc() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTextField numField1;
private JTextField numField2;
private JComboBox cbModifier;
private JLabel lblResult;
private JButton equals;
public TestPane() {
numField1 = new JTextField(4);
numField2 = new JTextField(4);
cbModifier = new JComboBox();
equals = new JButton("=");
lblResult = new JLabel("?");
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
model.addElement("+");
model.addElement("-");
model.addElement("/");
model.addElement("x");
cbModifier.setModel(model);
setLayout(new GridBagLayout());
add(numField1);
add(cbModifier);
add(numField2);
add(equals);
add(lblResult);
equals.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
int num1 = Integer.parseInt(numField1.getText());
int num2 = Integer.parseInt(numField2.getText());
// Make your calculations here...
// Update the lblResult with the resulting value...
lblResult.setText(String.valueOf(42));
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
lblResult.setText("Bad numbers");
}
}
});
}
}
}
Upvotes: 2