Search in sources :

Example 36 with FocusEvent

use of java.awt.event.FocusEvent in project gephi by gephi.

the class DirectoryChooserUI method createBottomPanel.

private void createBottomPanel(JFileChooser fc) {
    bottomPanel = new JPanel();
    bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));
    JPanel labelPanel = new JPanel();
    labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.PAGE_AXIS));
    labelPanel.add(Box.createRigidArea(verticalStrut1));
    JLabel fnl = new JLabel(fileNameLabelText);
    fnl.setDisplayedMnemonic(fileNameLabelMnemonic);
    fnl.setAlignmentY(0);
    labelPanel.add(fnl);
    labelPanel.add(Box.createRigidArea(new Dimension(1, 12)));
    JLabel ftl = new JLabel(filesOfTypeLabelText);
    ftl.setDisplayedMnemonic(filesOfTypeLabelMnemonic);
    labelPanel.add(ftl);
    bottomPanel.add(labelPanel);
    bottomPanel.add(Box.createRigidArea(new Dimension(15, 0)));
    JPanel fileAndFilterPanel = new JPanel();
    fileAndFilterPanel.add(Box.createRigidArea(verticalStrut3));
    fileAndFilterPanel.setLayout(new BoxLayout(fileAndFilterPanel, BoxLayout.Y_AXIS));
    filenameTextField = new JTextField(24) {

        @Override
        public Dimension getMaximumSize() {
            return new Dimension(Short.MAX_VALUE, super.getPreferredSize().height);
        }
    };
    filenameTextField.getDocument().addDocumentListener(new DocumentListener() {

        @Override
        public void insertUpdate(DocumentEvent e) {
            updateCompletions();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            updateCompletions();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
        }
    });
    filenameTextField.addKeyListener(new TextFieldKeyListener());
    fnl.setLabelFor(filenameTextField);
    filenameTextField.addFocusListener(new FocusAdapter() {

        @Override
        public void focusGained(FocusEvent e) {
            if (!getFileChooser().isMultiSelectionEnabled()) {
                tree.clearSelection();
            }
        }
    });
    // disable TAB focus transfer, we need it for completion
    Set<AWTKeyStroke> tKeys = filenameTextField.getFocusTraversalKeys(java.awt.KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
    Set<AWTKeyStroke> newTKeys = new HashSet<AWTKeyStroke>(tKeys);
    newTKeys.remove(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, 0));
    // #107305: enable at least Ctrl+TAB if we have TAB for completion
    newTKeys.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, KeyEvent.CTRL_DOWN_MASK));
    filenameTextField.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, newTKeys);
    fileAndFilterPanel.add(filenameTextField);
    fileAndFilterPanel.add(Box.createRigidArea(verticalStrut3));
    filterTypeComboBoxModel = createFilterComboBoxModel();
    fc.addPropertyChangeListener(filterTypeComboBoxModel);
    filterTypeComboBox = new JComboBox(filterTypeComboBoxModel);
    ftl.setLabelFor(filterTypeComboBox);
    filterTypeComboBox.setRenderer(createFilterComboBoxRenderer());
    fileAndFilterPanel.add(filterTypeComboBox);
    bottomPanel.add(fileAndFilterPanel);
    bottomPanel.add(Box.createRigidArea(horizontalStrut1));
    createButtonsPanel(fc);
}
Also used : DocumentListener(javax.swing.event.DocumentListener) FocusAdapter(java.awt.event.FocusAdapter) DocumentEvent(javax.swing.event.DocumentEvent) FocusEvent(java.awt.event.FocusEvent)

Example 37 with FocusEvent

use of java.awt.event.FocusEvent in project OpenNotebook by jaltekruse.

the class StringAdjustmentPanel method addPanelContent.

@Override
public void addPanelContent() {
    // to prevent the panel from looking too small, estimate the amount of lines needed to
    // show all of the text
    int numLines = 0;
    if (getGraphics() != null) {
        // one of these panels is constructed in the background to initialize some resources
        // at that time there is no graphics object to reference to measure text 
        numLines = getGraphics().getFontMetrics().stringWidth(mAtt.getValue()) / 120;
    } else {
        numLines = mAtt.getValue().length() / 20;
    }
    if (numLines < 2) {
        numLines = 2;
    }
    textArea = new JTextArea(numLines, 12);
    textArea.setEditable(true);
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    textArea.setText(mAtt.getValue());
    textArea.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent arg0) {
        }

        @Override
        public void focusLost(FocusEvent arg0) {
            applyPanelValueToObject();
        }
    });
    // need to decide if having this panel set with the enter key is worth
    // not being able to include line breaks
    textArea.addKeyListener(new KeyListener() {

        @Override
        public void keyPressed(KeyEvent ev) {
            if (ev.getKeyCode() == KeyEvent.VK_ENTER) {
                applyPanelValueToObject();
                enterJustPressed = true;
            }
        }

        @Override
        public void keyReleased(KeyEvent arg0) {
            if (arg0.getKeyCode() == KeyEvent.VK_ENTER && enterJustPressed) {
                // had issue with hitting enter to confirm dialog, dialog disappeared but enter key release event
                // was still received here if an object was selected, hence the boolean set in the keypressed
                // method and checked here
                String s = textArea.getText();
                int caretPos = textArea.getCaretPosition() - 1;
                s = s.substring(0, textArea.getCaretPosition() - 1) + s.substring(textArea.getCaretPosition());
                textArea.setText(s);
                textArea.setCaretPosition(caretPos);
                enterJustPressed = false;
            }
        }

        @Override
        public void keyTyped(KeyEvent arg0) {
        }
    });
    setLayout(new GridBagLayout());
    GridBagConstraints con = new GridBagConstraints();
    con.fill = GridBagConstraints.HORIZONTAL;
    con.weightx = .5;
    con.gridx = 0;
    con.gridwidth = 1;
    con.gridy = 0;
    con.insets = new Insets(2, 5, 0, 5);
    add(new JLabel(mAtt.getName()), con);
    JButton keyboard = new JButton(ObjectPropertiesFrame.SMALL_KEYBOARD_IMAGE);
    keyboard.setToolTipText("Show On-Screen Keyboard");
    keyboard.setMargin(new Insets(3, 3, 3, 3));
    keyboard.setFocusable(false);
    con.insets = new Insets(2, 2, 2, 2);
    con.gridx = 1;
    con.weightx = .1;
    con.weighty = .1;
    con.gridheight = 1;
    add(keyboard, con);
    keyboard.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            notebookPanel.getCurrentDocViewer().setOnScreenKeyBoardVisible(true);
        }
    });
    JButton applyChanges = new JButton("Apply");
    applyChanges.setToolTipText("Value can also be applied by hitting Enter");
    applyChanges.setMargin(new Insets(5, 3, 5, 3));
    applyChanges.setFocusable(false);
    con.gridx = 2;
    con.weightx = .1;
    con.weighty = .1;
    con.gridheight = 1;
    add(applyChanges, con);
    applyChanges.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            applyPanelValueToObject();
        }
    });
    con.fill = GridBagConstraints.BOTH;
    con.weightx = 1;
    con.weighty = 1;
    con.gridwidth = 3;
    con.gridy = 1;
    con.gridx = 0;
    con.gridheight = 3;
    scrollPane = new JScrollPane(textArea);
    scrollPane.setWheelScrollingEnabled(false);
    con.insets = new Insets(0, 5, 2, 0);
    add(scrollPane, con);
    scrollPane.addMouseWheelListener(new MouseWheelListener() {

        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            Point componentPoint = SwingUtilities.convertPoint(scrollPane, e.getPoint(), parentPanel);
            parentPanel.dispatchEvent(new MouseWheelEvent(parentPanel, e.getID(), e.getWhen(), e.getModifiers(), componentPoint.x, componentPoint.y, e.getClickCount(), e.isPopupTrigger(), e.getScrollType(), e.getScrollAmount(), e.getWheelRotation()));
        }
    });
}
Also used : JScrollPane(javax.swing.JScrollPane) GridBagConstraints(java.awt.GridBagConstraints) JTextArea(javax.swing.JTextArea) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) MouseWheelListener(java.awt.event.MouseWheelListener) Point(java.awt.Point) FocusEvent(java.awt.event.FocusEvent) Point(java.awt.Point) KeyEvent(java.awt.event.KeyEvent) ActionListener(java.awt.event.ActionListener) MouseWheelEvent(java.awt.event.MouseWheelEvent) KeyListener(java.awt.event.KeyListener) FocusListener(java.awt.event.FocusListener)

Example 38 with FocusEvent

use of java.awt.event.FocusEvent in project jna by java-native-access.

the class BalloonTipManager method getBalloonTip.

/**
   * Returns the popup window of the balloon tip.
   * @param owner the owner component for the balloon tip
   * @param content the text string to display in the balloon tip
   * @param x the x coordinate for the origin for the balloon tip in relation
   * to the owner component
   * @param y the y coordinate for the origin for the balloon tip in relation
   * to the owner component
   * @param position the position for the balloon; either above or below the
     * owner component
   * @param duration the duration in milliseconds to display balloon tip
   * @param bordercolor the background color for the balloon tip
   * @param backgroundcolor the border color for the balloon tip
   * @param textcolor the text color for the balloon tip
   * @return the popup window of the balloon tip
   */
public static Popup getBalloonTip(final Component owner, final String content, int x, int y, final Integer duration, final Color bordercolor, final Color backgroundcolor, final Color textcolor) {
    final Point origin = owner == null ? new Point(0, 0) : owner.getLocationOnScreen();
    final Window parent = owner != null ? SwingUtilities.getWindowAncestor(owner) : null;
    final String text = content != null ? content : "";
    final Integer timerDuration = duration != null ? duration : 10000;
    origin.translate(x, y);
    vpos = VPOS_BELOW;
    hpos = HPOS_LEFT;
    return new Popup() {

        private BalloonTip bt = null;

        final ComponentEar componentEar = new ComponentEar();

        final MouseEar mouseEar = new MouseEar();

        final FocusEar focusEar = new FocusEar();

        /*
       * (non-Javadoc)
       * @see javax.swing.Popup#show()
       */
        public void show() {
            hidePopupTimer = new Timer(timerDuration, new TimerAction());
            bt = new BalloonTip(parent, text, origin, bordercolor, backgroundcolor, textcolor);
            bt.pack();
            Point pt = new Point(origin);
            pt.translate(10, owner.getHeight());
            bt.setLocation(getAdjustedOrigin(pt));
            bt.setWindowMask();
            bt.setVisible(true);
            owner.addFocusListener(focusEar);
            owner.addMouseListener(mouseEar);
            parent.addMouseListener(mouseEar);
            parent.addComponentListener(componentEar);
            hidePopupTimer.start();
            isShowing = true;
        }

        /*
       * (non-Javadoc)
       * @see javax.swing.Popup#hide()
       */
        public void hide() {
            if (bt != null) {
                isShowing = false;
                hidePopupTimer.stop();
                parent.removeComponentListener(componentEar);
                parent.removeMouseListener(mouseEar);
                owner.removeMouseListener(mouseEar);
                owner.removeFocusListener(focusEar);
                bt.setVisible(false);
                bt.dispose();
            }
        }

        /*
       * Adjust the location of the balloon popup so that is drawn completely
       * on the screen and specify the orientation.
       */
        private Point getAdjustedOrigin(Point pt) {
            Point ret = new Point(pt.x, pt.y);
            GraphicsConfiguration gc = owner.getGraphicsConfiguration();
            Rectangle sBounds = gc.getBounds();
            Insets sInsets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
            sBounds.x += sInsets.left;
            sBounds.y += sInsets.top;
            sBounds.width -= (sInsets.left + sInsets.right);
            sBounds.height -= (sInsets.top + sInsets.bottom);
            if (ret.x < sBounds.x) {
                ret.x = sBounds.x;
            } else if (ret.x - sBounds.x + bt.getWidth() > sBounds.width) {
                ret.x = owner.getLocationOnScreen().x - bt.getWidth() + 43;
            }
            if (ret.x >= pt.x) {
                hpos = HPOS_LEFT;
            } else {
                hpos = HPOS_RIGHT;
            }
            if (ret.y < sBounds.y) {
                ret.y = sBounds.y;
            } else if (ret.y - sBounds.y + bt.getHeight() > sBounds.height) {
                ret.y = owner.getLocationOnScreen().y - bt.getHeight();
            }
            if (ret.y >= pt.y) {
                vpos = VPOS_BELOW;
            } else {
                vpos = VPOS_ABOVE;
            }
            return ret;
        }

        /*
       * This class handles actions from the balloon tip timer.
       */
        @SuppressWarnings("serial")
        final class TimerAction extends AbstractAction {

            /*
         * (non-Javadoc)
         * @see java.awt.event.ActionListener#actionPerformed(
         * java.awt.event.ActionEvent)
         */
            public void actionPerformed(ActionEvent e) {
                hide();
            }
        }

        /*
       * This class handles events spawned from moving the component.
       */
        final class ComponentEar extends ComponentAdapter {

            /*
         * (non-Javadoc)
         * @see java.awt.event.ComponentAdapter#componentMoved(
         * java.awt.event.ComponentEvent)
         */
            public void componentMoved(ComponentEvent e) {
                hide();
            }
        }

        /*
       * This class handles events spawned when a mouse button is pressed.
       */
        final class MouseEar extends MouseAdapter {

            /*
         * (non-Javadoc)
         * @see java.awt.event.MouseAdapter#mousePressed(
         * java.awt.event.MouseEvent)
         */
            public void mousePressed(MouseEvent e) {
                hide();
            }
        }

        /*
       * This class handles events spawned when the component loses focus.
       */
        final class FocusEar extends FocusAdapter {

            /*
         * (non-Javadoc)
         * @see java.awt.event.FocusAdapter#focusLost(
         * java.awt.event.FocusEvent)
         */
            public void focusLost(FocusEvent e) {
                hide();
            }
        }
    };
}
Also used : Window(java.awt.Window) JWindow(javax.swing.JWindow) Insets(java.awt.Insets) MouseEvent(java.awt.event.MouseEvent) ActionEvent(java.awt.event.ActionEvent) Rectangle(java.awt.Rectangle) Point(java.awt.Point) FocusEvent(java.awt.event.FocusEvent) GraphicsConfiguration(java.awt.GraphicsConfiguration) Timer(javax.swing.Timer) Popup(javax.swing.Popup) ComponentEvent(java.awt.event.ComponentEvent)

Example 39 with FocusEvent

use of java.awt.event.FocusEvent in project Botnak by Gocnak.

the class BaseTextFieldUI method installListeners.

protected void installListeners() {
    super.installListeners();
    if (AbstractLookAndFeel.getTheme().doShowFocusFrame()) {
        focusListener = new FocusListener() {

            public void focusGained(FocusEvent e) {
                if (getComponent() != null) {
                    orgBorder = getComponent().getBorder();
                    LookAndFeel laf = UIManager.getLookAndFeel();
                    if (laf instanceof AbstractLookAndFeel && orgBorder instanceof UIResource) {
                        Border focusBorder = ((AbstractLookAndFeel) laf).getBorderFactory().getFocusFrameBorder();
                        getComponent().setBorder(focusBorder);
                    }
                    getComponent().invalidate();
                    getComponent().repaint();
                }
            }

            public void focusLost(FocusEvent e) {
                if (getComponent() != null) {
                    if (orgBorder instanceof UIResource) {
                        getComponent().setBorder(orgBorder);
                    }
                    getComponent().invalidate();
                    getComponent().repaint();
                }
            }
        };
        getComponent().addFocusListener(focusListener);
    }
}
Also used : FocusListener(java.awt.event.FocusListener) FocusEvent(java.awt.event.FocusEvent) Border(javax.swing.border.Border) UIResource(javax.swing.plaf.UIResource)

Example 40 with FocusEvent

use of java.awt.event.FocusEvent in project Botnak by Gocnak.

the class BaseComboBoxUI method installListeners.

protected void installListeners() {
    super.installListeners();
    propertyChangeListener = new PropertyChangeHandler();
    comboBox.addPropertyChangeListener(propertyChangeListener);
    if (AbstractLookAndFeel.getTheme().doShowFocusFrame()) {
        focusListener = new FocusListener() {

            public void focusGained(FocusEvent e) {
                if (comboBox != null) {
                    orgBorder = comboBox.getBorder();
                    orgBackgroundColor = comboBox.getBackground();
                    LookAndFeel laf = UIManager.getLookAndFeel();
                    if (laf instanceof AbstractLookAndFeel) {
                        if (orgBorder instanceof UIResource) {
                            Border focusBorder = ((AbstractLookAndFeel) laf).getBorderFactory().getFocusFrameBorder();
                            comboBox.setBorder(focusBorder);
                        }
                        Color backgroundColor = AbstractLookAndFeel.getTheme().getFocusBackgroundColor();
                        comboBox.setBackground(backgroundColor);
                    }
                }
            }

            public void focusLost(FocusEvent e) {
                if (comboBox != null) {
                    if (orgBorder instanceof UIResource) {
                        comboBox.setBorder(orgBorder);
                    }
                    comboBox.setBackground(orgBackgroundColor);
                }
            }
        };
        comboBox.addFocusListener(focusListener);
    }
}
Also used : FocusListener(java.awt.event.FocusListener) FocusEvent(java.awt.event.FocusEvent) Border(javax.swing.border.Border) UIResource(javax.swing.plaf.UIResource)

Aggregations

FocusEvent (java.awt.event.FocusEvent)81 FocusListener (java.awt.event.FocusListener)38 FocusAdapter (java.awt.event.FocusAdapter)33 JLabel (javax.swing.JLabel)21 ActionEvent (java.awt.event.ActionEvent)18 ActionListener (java.awt.event.ActionListener)17 JPanel (javax.swing.JPanel)15 JTextField (javax.swing.JTextField)15 Dimension (java.awt.Dimension)14 JButton (javax.swing.JButton)10 KeyEvent (java.awt.event.KeyEvent)9 JCheckBox (javax.swing.JCheckBox)9 BoxLayout (javax.swing.BoxLayout)8 JComboBox (javax.swing.JComboBox)8 Border (javax.swing.border.Border)8 Component (java.awt.Component)7 GridBagConstraints (java.awt.GridBagConstraints)6 GridBagLayout (java.awt.GridBagLayout)6 Insets (java.awt.Insets)6 MouseEvent (java.awt.event.MouseEvent)6