Java Utililty Methods JTextField

List of utility methods to do JTextField

Description

The list of methods to do JTextField are organized into topic(s).

Method

voidbrowseFileForField(JTextField textField, JFileChooser fileChooser, Component parent)
Uses the fileChooser to browse a (not further filtered) file and put the path to the file in the given JTextField .
fileChooser.setMultiSelectionEnabled(false);
int returnVal = fileChooser.showOpenDialog(parent);
if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = fileChooser.getSelectedFile();
    textField.setText(file.getAbsolutePath());
voidcheckJTextFieldNotEmpty(javax.swing.JTextField field, javax.swing.JLabel promptName, ArrayList errors)
check J Text Field Not Empty
if (!isJTextFieldNotEmpty(field))
    errors.add(promptName.getText());
voidcleanTextFields(JTextField... textFields)
clean Text Fields
for (JTextField textField : textFields)
    textField.setText(null);
JButtoncreateBrowseButton(final JTextField txtInput, final Window parent, final boolean directoryBrowser)
create Browse Button
JButton btnChooser = new JButton();
btnChooser.setText("...");
btnChooser.setToolTipText("Browse for the " + (directoryBrowser ? "directory" : "file"));
btnChooser.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(
...
JPanelcreateFieldWithLabel(String label, JTextField textField)
create Field With Label
JPanel p = new JPanel(new BorderLayout());
textField.setMinimumSize(new Dimension(500, 50));
p.add(new JLabel(label), BorderLayout.WEST);
p.add(textField, BorderLayout.CENTER);
return p;
JPanelcreateOutputPanel(JLabel outputFileNameLabel, JButton chooseOutputFileButton, JTextField outputFileTextField, String borderTitle)
create Output Panel
JPanel pickerPanel = new JPanel();
outputFileNameLabel = new JLabel();
chooseOutputFileButton.setText("Choose");
Border border = BorderFactory.createTitledBorder(borderTitle);
outputFileTextField.setText("A file name");
pickerPanel.setBorder(border);
pickerPanel.add(chooseOutputFileButton);
pickerPanel.add(outputFileNameLabel);
...
voiddateCompleteOnlyFormat(JTextField pJTextField, KeyEvent e)
date Complete Only Format
char keyChar = e.getKeyChar();
if (Character.isDigit(keyChar)) {
    if ((pJTextField.getText().trim().length() == 2) || (pJTextField.getText().trim().length() == 5))
        pJTextField.setText(pJTextField.getText().trim() + "/");
voiddoAction(JTextField textField)
Programmatically perform action on textfield.This does the same thing as if the user had pressed enter key in textfield.
String command = null;
if (textField.getAction() != null)
    command = (String) textField.getAction().getValue(Action.ACTION_COMMAND_KEY);
ActionEvent event = null;
for (ActionListener listener : textField.getActionListeners()) {
    if (event == null)
        event = new ActionEvent(textField, ActionEvent.ACTION_PERFORMED, command,
                System.currentTimeMillis(), 0);
...
voidfieldSetErrorVisual(final JTextField field)
field Set Error Visual
field.setBorder(BorderFactory.createLineBorder(Color.RED));
field.setBackground(Color.PINK);
voidimplyDisabled(final JCheckBox checked, final boolean checkedState, final JTextField changed)
Checks state of the checked checkbox and if state is checkedState than to disable changed text field and clean it.
ActionListener l = new ActionListener() {
    String previousState;
    public void actionPerformed(ActionEvent e) {
        if (checked.isSelected() == checkedState) {
            if (previousState == null) {
                previousState = changed.getText();
            changed.setEnabled(false);
...