Example usage for javax.swing JTextField removeKeyListener

List of usage examples for javax.swing JTextField removeKeyListener

Introduction

In this page you can find the example usage for javax.swing JTextField removeKeyListener.

Prototype

public synchronized void removeKeyListener(KeyListener l) 

Source Link

Document

Removes the specified key listener so that it no longer receives key events from this component.

Usage

From source file:uk.ac.liverpool.narrative.SolutionGraph.java

/**
 * @param template//from  w  w w .j a  v a  2s .c  om
 * @param out
 * @param in
 * @param current
 * @return
 * @throws IOException
 */
private StringBuilder mainLoop(final Template template, final JTextArea out, final JTextField in,
        STRIPSState current) throws IOException {
    Collection<Action> aa = ggraph.getOutEdges(current);
    StringBuilder ssf = new StringBuilder();
    if (aa == null) {
        out.append("No action from the start?\n");
        return ssf;
    }

    do {

        if (aa.size() == 1) {
            Action a = aa.iterator().next();
            out.append(actionToString(template, a));
            ssf.append(actionToString(template, a));
            if (a != null) {
                current = ggraph.getOpposite(current, a);

            }
        } else if (aa.size() > 1) {
            int n = 0;
            final Map<Integer, Action> hm = new MapWrapper<Integer, Action>();
            out.append("   Please select the next action: " + "\n");
            Iterator<Action> it = aa.iterator();
            while (it.hasNext()) {
                n++;
                Action a = it.next();
                out.append("   " + n + ") " + actionToString(template, a));
                hm.put(n, a);
            }
            out.append("" + "\n");
            final STRIPSState c2 = current;
            in.addKeyListener(new KeyAdapter() {
                @Override
                public void keyReleased(KeyEvent e) {
                    if (e.getKeyCode() != KeyEvent.VK_ENTER)
                        return;
                    int a = 0;
                    String s = in.getText();
                    try {
                        a = Integer.parseInt(s);
                    } catch (Exception x) {

                    }
                    try {
                        a = Integer.parseInt(s);
                        Action a2 = hm.get(a);
                        if (a2 != null) {
                            out.append(actionToString(template, a2));
                            in.removeKeyListener(this);
                            in.setText("");
                            mainLoop(template, out, in, ggraph.getOpposite(c2, a2));
                        }

                    } catch (Exception x) {
                        return;
                    }
                    super.keyReleased(e);
                }
            });
            return ssf;
        } else {
            break;
        }

        aa = ggraph.getOutEdges(current);
    } while (aa.size() > 0);
    out.append("The end");
    return ssf;
}