Example usage for java.awt.event FocusListener FocusListener

List of usage examples for java.awt.event FocusListener FocusListener

Introduction

In this page you can find the example usage for java.awt.event FocusListener FocusListener.

Prototype

FocusListener

Source Link

Usage

From source file:Main.java

public void init() {
    tf1 = new JTextField(5);

    tf1.addFocusListener(new FocusListener() {
        public void focusGained(FocusEvent e) {
        };/*from  w  ww  .  jav  a2  s  . c o m*/

        public void focusLost(FocusEvent e) {
            if (!e.isTemporary()) {
                String content = tf1.getText();
                if (!content.equals("a")) {
                    System.out.println("illegal value! " + content);
                    SwingUtilities.invokeLater(new FocusGrabber(tf1));
                }
            }
        }
    });
}

From source file:org.nekorp.workflow.desktop.view.binding.JTextFieldWithValidation.java

/**
 * Creates new form JTextFieldWithValidation
 *//*from   w  w  w  .  j  a  v a2  s  .  c o m*/
public JTextFieldWithValidation() {
    this.initComponents();
    this.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
            textField.requestFocus();
        }

        @Override
        public void focusLost(FocusEvent e) {
            //nada
        }
    });
}

From source file:Main.java

public Main() {
    formTextFieldFormat = NumberFormat.getNumberInstance();
    formTextFieldFormat.setMinimumFractionDigits(2);
    formTextFieldFormat.setMaximumFractionDigits(2);
    formTextFieldFormat.setRoundingMode(RoundingMode.HALF_UP);

    focusLabel.setPreferredSize(new Dimension(120, 27));
    formTextField = new JFormattedTextField(formTextFieldFormat);
    formTextField.setValue(amount);/*from   w w w  .  jav  a 2 s .  c  om*/
    formTextField.setPreferredSize(new Dimension(120, 27));
    formTextField.setHorizontalAlignment(SwingConstants.RIGHT);
    formTextField.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
            formTextField.requestFocus();
            formTextField.setText(formTextField.getText());
            formTextField.selectAll();
        }

        @Override
        public void focusLost(FocusEvent e) {
            double t1a1 = (((Number) formTextField.getValue()).doubleValue());
            if (t1a1 < 1000) {
                formTextField.setValue(amount);
            }
        }
    });

    docLabel.setPreferredSize(new Dimension(120, 27));

    formTextField1 = new JFormattedTextField(formTextFieldFormat);
    formTextField1.setValue(amount);

    formTextField1.setHorizontalAlignment(SwingConstants.RIGHT);
    formTextField1.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
            formTextField1.requestFocus();
            formTextField1.setText(formTextField1.getText());
            formTextField1.selectAll();
        }

        @Override
        public void focusLost(FocusEvent e) {
        }
    });
    formTextField1.getDocument().addDocumentListener(docListener);

    pnl = new JPanel();
    pnl.setBorder(new EmptyBorder(2, 2, 2, 2));
    pnl.setLayout(new GridLayout(2, 2));
    pnl.add(focusLabel);
    pnl.add(formTextField);
    pnl.add(docLabel);
    pnl.add(formTextField1);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(pnl, BorderLayout.CENTER);
    frame.setLocation(200, 200);
    frame.pack();
    frame.setVisible(true);
    formTextFieldFocus1();
}

From source file:qic.ui.ConfigPanel.java

public ConfigPanel() {
    textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_PROPERTIES_FILE);
    setViewportView(textArea);//from   w w w. j  a v a 2 s .c  o m

    textArea.addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {
            saveAndReloadConfig();
            Main.reloadLookAndFeel();
        }

        @Override
        public void focusGained(FocusEvent e) {
            loadConfigToTextArea();
        }

    });
}

From source file:qic.ui.TermsPanel.java

public TermsPanel() {
    setLayout(new BorderLayout());

    setupTextArea();//  w  w w.  j  av a2  s.c o m
    setupFileTree();

    textArea.addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {
            saveCurrentScriptToFile();
        }

        @Override
        public void focusGained(FocusEvent e) {
        }

    });

}

From source file:qic.ui.ScriptsPanel.java

public ScriptsPanel() {
    setLayout(new BorderLayout());

    setupTextArea();//w  w w  .  j a  v  a  2 s.c o m
    setupFileTree();

    textArea.addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {
            saveCurrentScriptToFile();
        }

        @Override
        public void focusGained(FocusEvent e) {
        }

    });

}

From source file:be.ac.ua.comp.scarletnebula.gui.BetterTextField.java

/**
 * Enables the JTextField to have a placeholder and sets the initial text to
 * placeholder. Call this somewhere before showing the textfield.
 * //from   w w  w  . j a  v  a 2s. c om
 * Concretly this placeholder will take the shape of gray text that will
 * appear when the field contains no text and is not focussed and that will
 * disappear when the field receives focus.
 * 
 * @param placeholder
 */
public void setPlaceHolder(final String placeholder) {
    setText(placeholder);
    final Color originalTextColor = getForeground();
    setForeground(Colors.Gray.color());
    placeholderDisplayed = true;

    addFocusListener(new FocusListener() {
        @Override
        public void focusLost(final FocusEvent e) {
            if (getText().length() == 0) {
                setForeground(Colors.Gray.alpha(1.0f));
                setText(placeholder);
                placeholderDisplayed = true;
                originalPlaceholderText = true;
            }
        }

        @Override
        public void focusGained(final FocusEvent e) {
            log.info("Focus gained");
            if (originalPlaceholderText) {
                log.info("Original text");
                originalPlaceholderText = false;
                setForeground(originalTextColor);
                setText("");
                placeholderDisplayed = false;
            }
        }
    });
}

From source file:userinterface.graph.GraphOptions.java

/** Creates a new instance of MultiGraphOptions */
public GraphOptions(GUIPlugin plugin, JPanel theModel, JFrame gui, String title) {
    super(gui, title);

    gop = new GraphOptionsPanel(plugin, gui, theModel);

    gop.setPreferredSize(new Dimension(400, 650));

    this.getContentPane().add(gop);

    JPanel p = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    this.getContentPane().add(p, BorderLayout.SOUTH);

    this.getContentPane().setSize(400, 650);

    JButton jb = new JButton("Close");

    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            gop.stopEditors();/*from   w  w w . j  ava  2s .co m*/
            setVisible(false);
        }
    });

    jb.addFocusListener(new FocusListener() {
        /**
         * Invoked when a component gains the keyboard focus.
         */
        public void focusGained(FocusEvent e) {
        }

        /**
         * Invoked when a component loses the keyboard focus.
         */
        public void focusLost(FocusEvent e) {
            //gop.stopEditors();
        }

    });

    p.add(jb);

    pack();
    setLocationRelativeTo(getParent()); // centre
    //show();
    setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
}

From source file:com.eviware.soapui.impl.rest.panels.component.RestResourceEditor.java

public RestResourceEditor(final RestResource editingRestResource, MutableBoolean updating) {
    super(editingRestResource.getFullPath());
    this.editingRestResource = editingRestResource;
    this.updating = updating;
    setName(REST_RESOURCE_EDITOR_TEXT_FIELD);

    if (isResourceLonely(editingRestResource)) {
        getDocument().addDocumentListener(new LonelyDocumentListener());
        addFocusListener(new FocusListener() {
            public void focusLost(FocusEvent e) {
                scanForTemplateParameters(editingRestResource);
                removeMatrixParameters();
            }//  w w w  . ja v  a2s  .  c o  m

            /**
             * Matrix parameters should not be added directly on the rest resource.
             * The parameter editor should be used. Hence they are removed from the rest resource editor
             * text field at this time.
             */
            private void removeMatrixParameters() {
                setText(getText().split(";")[0]);
            }

            public void focusGained(FocusEvent e) {
            }
        });

    } else {
        Color originalBackground = getBackground();
        Border originalBorder = getBorder();
        setEditable(false);
        setBackground(originalBackground);
        setBorder(originalBorder);
        setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
        mouseListener = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                final RestResource focusedResource = new RestResourceFinder(editingRestResource)
                        .findResourceAt(lastSelectedPosition);
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        openPopup(focusedResource);
                    }
                });
            }
        };
        addMouseListener(mouseListener);
        addCaretListener(new CaretListener() {
            @Override
            public void caretUpdate(final CaretEvent e) {
                lastSelectedPosition = e.getDot();
            }

        });
    }
}

From source file:DigitalClock.java

public DigitalClock() {
    // Set default values for our properties
    setFormat(DateFormat.getTimeInstance(DateFormat.MEDIUM, getLocale()));
    setUpdateFrequency(1000); // Update once a second

    // Specify a Swing TransferHandler object to do the dirty work of
    // copy-and-paste and drag-and-drop for us. This one will transfer
    // the value of the "time" property. Since this property is read-only
    // it will allow drags but not drops.
    setTransferHandler(new TransferHandler("time"));

    // Since JLabel does not normally support drag-and-drop, we need an
    // event handler to detect a drag and start the transfer.
    addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
            getTransferHandler().exportAsDrag(DigitalClock.this, e, TransferHandler.COPY);
        }/*from  w  w w.  j  a v  a2s.com*/
    });

    // Before we can have a keyboard binding for a Copy command,
    // the component needs to be able to accept keyboard focus.
    setFocusable(true);
    // Request focus when we're clicked on
    addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            requestFocus();
        }
    });
    // Use a LineBorder to indicate when we've got the keyboard focus
    addFocusListener(new FocusListener() {
        public void focusGained(FocusEvent e) {
            setBorder(LineBorder.createBlackLineBorder());
        }

        public void focusLost(FocusEvent e) {
            setBorder(null);
        }
    });

    // Now bind the Ctrl-C keystroke to a "Copy" command.
    InputMap im = new InputMap();
    im.setParent(getInputMap(WHEN_FOCUSED));
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK), "Copy");
    setInputMap(WHEN_FOCUSED, im);

    // And bind the "Copy" command to a pre-defined Action that performs
    // a copy using the TransferHandler we've installed.
    ActionMap am = new ActionMap();
    am.setParent(getActionMap());
    am.put("Copy", TransferHandler.getCopyAction());
    setActionMap(am);

    // Create a javax.swing.Timer object that will generate ActionEvents
    // to tell us when to update the displayed time. Every updateFrequency
    // milliseconds, this timer will cause the actionPerformed() method
    // to be invoked. (For non-GUI applications, see java.util.Timer.)
    timer = new Timer(updateFrequency, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setText(getTime()); // set label to current time string
        }
    });
    timer.setInitialDelay(0); // Do the first update immediately
    timer.start(); // Start timing now!
}