Create a JText field upon button click
I have 2 JText fields where I put in a 1st number in textfield1 and a 2nd number in textfield 2. I then have a button for "Total" What I want to do is when the Total button is pressed, create a 3rd JText field and display the total (number1 + number2) in that field. How would I do this?
Re: Create a JText field upon button click
Unless you intend the total to be user editable a JLabel would make more sense than a text field. Either way create all the components when you construct the gui. Then, in the event handler for the button, do the addition and set the text in the label or field.
Re: Create a JText field upon button click
I have been trying to use JTextField, but set editable to false and here is the line I run:
Code :
JLabel areaLabel = new JLabel("Answer Equals: ");
JTextField areaField = new JTextField(5);
areaField.setEditable(false);
But I get two compile errors:
illegal start of type:
areaField.setEditable(false);
error: (identifier) expected
areaField.setEditable(false);
Re: Create a JText field upon button click
It looks OK to me.
Post complete code and the full compiler message (together with an indication of which lines of your code it is referring to). This might involve paring down you code to an example that illustrates just the problem you are having.
----
Just noticed that this is duplicated at Hidden JText Field (was going to posta link because of the similarity...)
This is very irksome.