Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * JLibs: Common Utilities for Java
 * Copyright (C) 2009  Santhosh Kumar T <santhosh.tekuri@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {
    /**
     * Programmatically perform action on textfield.This does the same
     * thing as if the user had pressed enter key in textfield.
     *
     * @param textField textField on which action to be preformed
     */
    public static void doAction(JTextField 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);
            listener.actionPerformed(event);
        }
    }
}