How To Clear Textarea In Java
#1
-
- New D.I.C Head
Reputation: 0
- Posts: 16
- Joined: 28-January 09
Clearing Text from a Text Field
Posted 29 August 2009 - 02:02 PM
The following code runs a simple window. The only thing I don't know how to do is to clear the text from the field after the user hit the Calculate button. When the user hits the Calculate button whatever the user put in the field stays there. Is there any methods that would do this for me?
import javax.swing.*; import java.awt.event.*; import java.text.DecimalFormat; public class PropertyTaxWindow extends JFrame { private JPanel panel; private JLabel messageLabel; private JTextField valueTextField; private JButton calcButton; private JButton exitButton; private final int WINDOW_WIDTH = 400; private final int WINDOW_HEIGHT = 100; // Constructor public PropertyTaxWindow() { // Set the window title setTitle("Assessment Value and Property Tax Calculator"); // Set the size of the window setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Specify what happens when the close button is clicked setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Build the panel and add it to the frame buildPanel(); // Add the panel to the frame's content pane add(panel); // Display the window setVisible(true); } // buildPanel() method private void buildPanel() { // Create a label to display instructions messageLabel = new JLabel("Enter the actual value of the property"); // Create a text field valueTextField = new JTextField(10); // Create a calculate button calcButton = new JButton("Calculate"); // Add an action listener to the calcButton calcButton.addActionListener(new CalcButtonListener()); // Create an exit button exitButton = new JButton("Exit"); // Add an action listener to the exitButton exitButton.addActionListener(new ExitButtonListener()); // Create JPanel object panel = new JPanel(); // Add componets to the panel panel.add(messageLabel); panel.add(valueTextField); panel.add(calcButton); panel.add(exitButton); } // Private inner class that handles the event when the user clicks the Calculate Button private class CalcButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { String input; float actualValue; float assessmentValue; float propertyTax; final float ASSESSMENT_VALUE_PERCENT = 0.6f; final float TAX = 0.64f; final float DIVIDER = 100.0F; // Get the text entered input = valueTextField.getText(); // Convert to a float actualValue = Float.parseFloat(input); // Calculate the assessment value assessmentValue = actualValue * ASSESSMENT_VALUE_PERCENT; // Calculate property tax propertyTax = assessmentValue / DIVIDER * TAX; // Format the output to two decimal places DecimalFormat decimal = new DecimalFormat("0.00"); // Display the result JOptionPane.showMessageDialog(null, "Actual Value: $"+ decimal.format(actualValue) + "\nAssessment Value: $"+ decimal.format(assessmentValue) + "\nProperty Tax: $"+ decimal.format(propertyTax)); } } // Private inner class for the Exit Button private class ExitButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } // Main method that runs the program public static void main(String[] args) { new PropertyTaxWindow(); } }
*Edited: intermediate tag removed
This post has been edited by pbl: 29 August 2009 - 10:14 PM
Is This A Good Question/Topic? 0
#2 Locke
Reputation: 552
- Posts: 5,624
- Joined: 20-March 08
Re: Clearing Text from a Text Field
Posted 29 August 2009 - 02:03 PM
You can just use valueTextField.setText(""); to reset the text to empty.
#3 Tyler E
-
- New D.I.C Head
Reputation: 0
- Posts: 16
- Joined: 28-January 09
Re: Clearing Text from a Text Field
Posted 29 August 2009 - 02:11 PM
That worked great thank you. Now is there any way to put the focus back on that field.
#4 syfran
Reputation: 83
- Posts: 1,103
- Joined: 12-July 09
Re: Clearing Text from a Text Field
Posted 29 August 2009 - 02:34 PM
call requestFocus() or grabFocus() on the textField
This post has been edited by syfran: 29 August 2009 - 02:35 PM
#5 Locke
Reputation: 552
- Posts: 5,624
- Joined: 20-March 08
Re: Clearing Text from a Text Field
Posted 29 August 2009 - 03:29 PM
syfran, on 29 Aug, 2009 - 03:34 PM, said:
call requestFocus() or grabFocus() on the textField
It's better practice to use requestFocusInWindow(), as the requestFocus() method's procedure is platform dependent.
This post has been edited by Locke: 29 August 2009 - 03:30 PM
#6 Tyler E
-
- New D.I.C Head
Reputation: 0
- Posts: 16
- Joined: 28-January 09
Re: Clearing Text from a Text Field
Posted 29 August 2009 - 05:23 PM
Thank you both, my program works the way I want it to now
#7 pbl
Reputation: 8381
- Posts: 31,956
- Joined: 06-March 08
Re: Clearing Text from a Text Field
Posted 29 August 2009 - 10:14 PM
Really basic question.
"Intermediate" tag removed
How To Clear Textarea In Java
Source: https://www.dreamincode.net/forums/topic/122861-clearing-text-from-a-text-field/
Posted by: mccleskeyutmacksmay.blogspot.com
0 Response to "How To Clear Textarea In Java"
Post a Comment