Search in sources :

Example 86 with Container

use of java.awt.Container in project jdk8u_jdk by JetBrains.

the class JOptionPane method showInternalInputDialog.

/**
     * Prompts the user for input in a blocking internal dialog where
     * the initial selection, possible selections, and all other
     * options can be specified. The user will able to choose from
     * <code>selectionValues</code>, where <code>null</code>
     * implies the user can input
     * whatever they wish, usually by means of a <code>JTextField</code>.
     * <code>initialSelectionValue</code> is the initial value to prompt
     * the user with. It is up to the UI to decide how best to represent
     * the <code>selectionValues</code>, but usually a
     * <code>JComboBox</code>, <code>JList</code>, or
     * <code>JTextField</code> will be used.
     *
     * @param parentComponent the parent <code>Component</code> for the dialog
     * @param message  the <code>Object</code> to display
     * @param title    the <code>String</code> to display in the dialog
     *          title bar
     * @param messageType the type of message to be displayed:
     *                  <code>ERROR_MESSAGE</code>, <code>INFORMATION_MESSAGE</code>,
     *                  <code>WARNING_MESSAGE</code>,
     *                  <code>QUESTION_MESSAGE</code>, or <code>PLAIN_MESSAGE</code>
     * @param icon     the <code>Icon</code> image to display
     * @param selectionValues an array of <code>Objects</code> that
     *                  gives the possible selections
     * @param initialSelectionValue the value used to initialize the input
     *                  field
     * @return user's input, or <code>null</code> meaning the user
     *          canceled the input
     */
public static Object showInternalInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue) {
    JOptionPane pane = new JOptionPane(message, messageType, OK_CANCEL_OPTION, icon, null, null);
    pane.putClientProperty(PopupFactory_FORCE_HEAVYWEIGHT_POPUP, Boolean.TRUE);
    Component fo = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    pane.setWantsInput(true);
    pane.setSelectionValues(selectionValues);
    pane.setInitialSelectionValue(initialSelectionValue);
    JInternalFrame dialog = pane.createInternalFrame(parentComponent, title);
    pane.selectInitialValue();
    dialog.setVisible(true);
    /* Since all input will be blocked until this dialog is dismissed,
         * make sure its parent containers are visible first (this component
         * is tested below).  This is necessary for JApplets, because
         * because an applet normally isn't made visible until after its
         * start() method returns -- if this method is called from start(),
         * the applet will appear to hang while an invisible modal frame
         * waits for input.
         */
    if (dialog.isVisible() && !dialog.isShowing()) {
        Container parent = dialog.getParent();
        while (parent != null) {
            if (parent.isVisible() == false) {
                parent.setVisible(true);
            }
            parent = parent.getParent();
        }
    }
    // Use reflection to get Container.startLWModal.
    try {
        Method method = AccessController.doPrivileged(new ModalPrivilegedAction(Container.class, "startLWModal"));
        if (method != null) {
            method.invoke(dialog, (Object[]) null);
        }
    } catch (IllegalAccessException ex) {
    } catch (IllegalArgumentException ex) {
    } catch (InvocationTargetException ex) {
    }
    if (parentComponent instanceof JInternalFrame) {
        try {
            ((JInternalFrame) parentComponent).setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
    }
    if (fo != null && fo.isShowing()) {
        fo.requestFocus();
    }
    Object value = pane.getInputValue();
    if (value == UNINITIALIZED_VALUE) {
        return null;
    }
    return value;
}
Also used : Container(java.awt.Container) Method(java.lang.reflect.Method) Component(java.awt.Component) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 87 with Container

use of java.awt.Container in project jdk8u_jdk by JetBrains.

the class JOptionPane method createInternalFrame.

/**
     * Creates and returns an instance of <code>JInternalFrame</code>.
     * The internal frame is created with the specified title,
     * and wrapping the <code>JOptionPane</code>.
     * The returned <code>JInternalFrame</code> is
     * added to the <code>JDesktopPane</code> ancestor of
     * <code>parentComponent</code>, or components
     * parent if one its ancestors isn't a <code>JDesktopPane</code>,
     * or if <code>parentComponent</code>
     * doesn't have a parent then a <code>RuntimeException</code> is thrown.
     *
     * @param parentComponent  the parent <code>Component</code> for
     *          the internal frame
     * @param title    the <code>String</code> to display in the
     *          frame's title bar
     * @return a <code>JInternalFrame</code> containing a
     *          <code>JOptionPane</code>
     * @exception RuntimeException if <code>parentComponent</code> does
     *          not have a valid parent
     */
public JInternalFrame createInternalFrame(Component parentComponent, String title) {
    Container parent = JOptionPane.getDesktopPaneForComponent(parentComponent);
    if (parent == null && (parentComponent == null || (parent = parentComponent.getParent()) == null)) {
        throw new RuntimeException("JOptionPane: parentComponent does " + "not have a valid parent");
    }
    // Option dialogs should be closable only
    final JInternalFrame iFrame = new JInternalFrame(title, false, true, false, false);
    iFrame.putClientProperty("JInternalFrame.frameType", "optionDialog");
    iFrame.putClientProperty("JInternalFrame.messageType", Integer.valueOf(getMessageType()));
    iFrame.addInternalFrameListener(new InternalFrameAdapter() {

        public void internalFrameClosing(InternalFrameEvent e) {
            if (getValue() == UNINITIALIZED_VALUE) {
                setValue(null);
            }
        }
    });
    addPropertyChangeListener(new PropertyChangeListener() {

        public void propertyChange(PropertyChangeEvent event) {
            // (newValue = null in that case).  Otherwise, close the dialog.
            if (iFrame.isVisible() && event.getSource() == JOptionPane.this && event.getPropertyName().equals(VALUE_PROPERTY)) {
                // Use reflection to get Container.stopLWModal().
                try {
                    Method method = AccessController.doPrivileged(new ModalPrivilegedAction(Container.class, "stopLWModal"));
                    if (method != null) {
                        method.invoke(iFrame, (Object[]) null);
                    }
                } catch (IllegalAccessException ex) {
                } catch (IllegalArgumentException ex) {
                } catch (InvocationTargetException ex) {
                }
                try {
                    iFrame.setClosed(true);
                } catch (java.beans.PropertyVetoException e) {
                }
                iFrame.setVisible(false);
            }
        }
    });
    iFrame.getContentPane().add(this, BorderLayout.CENTER);
    if (parent instanceof JDesktopPane) {
        parent.add(iFrame, JLayeredPane.MODAL_LAYER);
    } else {
        parent.add(iFrame, BorderLayout.CENTER);
    }
    Dimension iFrameSize = iFrame.getPreferredSize();
    Dimension rootSize = parent.getSize();
    Dimension parentSize = parentComponent.getSize();
    iFrame.setBounds((rootSize.width - iFrameSize.width) / 2, (rootSize.height - iFrameSize.height) / 2, iFrameSize.width, iFrameSize.height);
    // We want dialog centered relative to its parent component
    Point iFrameCoord = SwingUtilities.convertPoint(parentComponent, 0, 0, parent);
    int x = (parentSize.width - iFrameSize.width) / 2 + iFrameCoord.x;
    int y = (parentSize.height - iFrameSize.height) / 2 + iFrameCoord.y;
    // If possible, dialog should be fully visible
    int ovrx = x + iFrameSize.width - rootSize.width;
    int ovry = y + iFrameSize.height - rootSize.height;
    x = Math.max((ovrx > 0 ? x - ovrx : x), 0);
    y = Math.max((ovry > 0 ? y - ovry : y), 0);
    iFrame.setBounds(x, y, iFrameSize.width, iFrameSize.height);
    parent.validate();
    try {
        iFrame.setSelected(true);
    } catch (java.beans.PropertyVetoException e) {
    }
    return iFrame;
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) InternalFrameEvent(javax.swing.event.InternalFrameEvent) Method(java.lang.reflect.Method) Dimension(java.awt.Dimension) Point(java.awt.Point) InvocationTargetException(java.lang.reflect.InvocationTargetException) Point(java.awt.Point) Container(java.awt.Container) InternalFrameAdapter(javax.swing.event.InternalFrameAdapter)

Example 88 with Container

use of java.awt.Container in project jdk8u_jdk by JetBrains.

the class JOptionPane method initDialog.

private void initDialog(final JDialog dialog, int style, Component parentComponent) {
    dialog.setComponentOrientation(this.getComponentOrientation());
    Container contentPane = dialog.getContentPane();
    contentPane.setLayout(new BorderLayout());
    contentPane.add(this, BorderLayout.CENTER);
    dialog.setResizable(false);
    if (JDialog.isDefaultLookAndFeelDecorated()) {
        boolean supportsWindowDecorations = UIManager.getLookAndFeel().getSupportsWindowDecorations();
        if (supportsWindowDecorations) {
            dialog.setUndecorated(true);
            getRootPane().setWindowDecorationStyle(style);
        }
    }
    dialog.pack();
    dialog.setLocationRelativeTo(parentComponent);
    final PropertyChangeListener listener = new PropertyChangeListener() {

        public void propertyChange(PropertyChangeEvent event) {
            // (newValue = null in that case).  Otherwise, close the dialog.
            if (dialog.isVisible() && event.getSource() == JOptionPane.this && (event.getPropertyName().equals(VALUE_PROPERTY)) && event.getNewValue() != null && event.getNewValue() != JOptionPane.UNINITIALIZED_VALUE) {
                dialog.setVisible(false);
            }
        }
    };
    WindowAdapter adapter = new WindowAdapter() {

        private boolean gotFocus = false;

        public void windowClosing(WindowEvent we) {
            setValue(null);
        }

        public void windowClosed(WindowEvent e) {
            removePropertyChangeListener(listener);
            dialog.getContentPane().removeAll();
        }

        public void windowGainedFocus(WindowEvent we) {
            // Once window gets focus, set initial focus
            if (!gotFocus) {
                selectInitialValue();
                gotFocus = true;
            }
        }
    };
    dialog.addWindowListener(adapter);
    dialog.addWindowFocusListener(adapter);
    dialog.addComponentListener(new ComponentAdapter() {

        public void componentShown(ComponentEvent ce) {
            // reset value to ensure closing works properly
            setValue(JOptionPane.UNINITIALIZED_VALUE);
        }
    });
    addPropertyChangeListener(listener);
}
Also used : Container(java.awt.Container) PropertyChangeEvent(java.beans.PropertyChangeEvent) BorderLayout(java.awt.BorderLayout) PropertyChangeListener(java.beans.PropertyChangeListener) WindowEvent(java.awt.event.WindowEvent) WindowAdapter(java.awt.event.WindowAdapter) ComponentEvent(java.awt.event.ComponentEvent) ComponentAdapter(java.awt.event.ComponentAdapter)

Example 89 with Container

use of java.awt.Container in project jdk8u_jdk by JetBrains.

the class LWCursorManager method findComponent.

/**
     * Returns the first visible, enabled and showing component under cursor.
     * Returns null for modal blocked windows.
     *
     * @param cursorPos Current cursor position.
     * @return Component or null.
     */
private static final Component findComponent(final Point cursorPos) {
    final LWComponentPeer<?, ?> peer = LWWindowPeer.getPeerUnderCursor();
    Component c = null;
    if (peer != null && peer.getWindowPeerOrSelf().getBlocker() == null) {
        c = peer.getTarget();
        if (c instanceof Container) {
            final Point p = peer.getLocationOnScreen();
            c = AWTAccessor.getContainerAccessor().findComponentAt((Container) c, cursorPos.x - p.x, cursorPos.y - p.y, false);
        }
        while (c != null) {
            final Object p = AWTAccessor.getComponentAccessor().getPeer(c);
            if (c.isVisible() && c.isEnabled() && p != null) {
                break;
            }
            c = c.getParent();
        }
    }
    return c;
}
Also used : Container(java.awt.Container) Point(java.awt.Point) Component(java.awt.Component)

Example 90 with Container

use of java.awt.Container in project jdk8u_jdk by JetBrains.

the class MotifGraphicsUtils method paintMenuItem.

/**
   * This method is not being used to paint menu item since
   * 6.0 This code left for compatibility only. Do not use or
   * override it, this will not cause any visible effect.
   */
public static void paintMenuItem(Graphics g, JComponent c, Icon checkIcon, Icon arrowIcon, Color background, Color foreground, int defaultTextIconGap) {
    JMenuItem b = (JMenuItem) c;
    ButtonModel model = b.getModel();
    Dimension size = b.getSize();
    Insets i = c.getInsets();
    Rectangle viewRect = new Rectangle(size);
    viewRect.x += i.left;
    viewRect.y += i.top;
    viewRect.width -= (i.right + viewRect.x);
    viewRect.height -= (i.bottom + viewRect.y);
    Rectangle iconRect = new Rectangle();
    Rectangle textRect = new Rectangle();
    Rectangle acceleratorRect = new Rectangle();
    Rectangle checkRect = new Rectangle();
    Rectangle arrowRect = new Rectangle();
    Font holdf = g.getFont();
    Font f = c.getFont();
    g.setFont(f);
    FontMetrics fm = SwingUtilities2.getFontMetrics(c, g, f);
    FontMetrics fmAccel = SwingUtilities2.getFontMetrics(c, g, UIManager.getFont("MenuItem.acceleratorFont"));
    if (c.isOpaque()) {
        if (model.isArmed() || (c instanceof JMenu && model.isSelected())) {
            g.setColor(background);
        } else {
            g.setColor(c.getBackground());
        }
        g.fillRect(0, 0, size.width, size.height);
    }
    // get Accelerator text
    KeyStroke accelerator = b.getAccelerator();
    String acceleratorText = "";
    if (accelerator != null) {
        int modifiers = accelerator.getModifiers();
        if (modifiers > 0) {
            acceleratorText = KeyEvent.getKeyModifiersText(modifiers);
            acceleratorText += "+";
        }
        acceleratorText += KeyEvent.getKeyText(accelerator.getKeyCode());
    }
    // layout the text and icon
    String text = layoutMenuItem(c, fm, b.getText(), fmAccel, acceleratorText, b.getIcon(), checkIcon, arrowIcon, b.getVerticalAlignment(), b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect, acceleratorRect, checkRect, arrowRect, b.getText() == null ? 0 : defaultTextIconGap, defaultTextIconGap);
    // Paint the Check
    Color holdc = g.getColor();
    if (checkIcon != null) {
        if (model.isArmed() || (c instanceof JMenu && model.isSelected()))
            g.setColor(foreground);
        checkIcon.paintIcon(c, g, checkRect.x, checkRect.y);
        g.setColor(holdc);
    }
    // Paint the Icon
    if (b.getIcon() != null) {
        Icon icon;
        if (!model.isEnabled()) {
            icon = b.getDisabledIcon();
        } else if (model.isPressed() && model.isArmed()) {
            icon = b.getPressedIcon();
            if (icon == null) {
                // Use default icon
                icon = b.getIcon();
            }
        } else {
            icon = b.getIcon();
        }
        if (icon != null) {
            icon.paintIcon(c, g, iconRect.x, iconRect.y);
        }
    }
    // Draw the Text
    if (text != null && !text.equals("")) {
        // Once BasicHTML becomes public, use BasicHTML.propertyKey
        // instead of the hardcoded string below!
        View v = (View) c.getClientProperty("html");
        if (v != null) {
            v.paint(g, textRect);
        } else {
            int mnemIndex = b.getDisplayedMnemonicIndex();
            if (!model.isEnabled()) {
                // *** paint the text disabled
                g.setColor(b.getBackground().brighter());
                SwingUtilities2.drawStringUnderlineCharAt(b, g, text, mnemIndex, textRect.x, textRect.y + fmAccel.getAscent());
                g.setColor(b.getBackground().darker());
                SwingUtilities2.drawStringUnderlineCharAt(b, g, text, mnemIndex, textRect.x - 1, textRect.y + fmAccel.getAscent() - 1);
            } else {
                // *** paint the text normally
                if (model.isArmed() || (c instanceof JMenu && model.isSelected())) {
                    g.setColor(foreground);
                } else {
                    g.setColor(b.getForeground());
                }
                SwingUtilities2.drawStringUnderlineCharAt(b, g, text, mnemIndex, textRect.x, textRect.y + fm.getAscent());
            }
        }
    }
    // Draw the Accelerator Text
    if (acceleratorText != null && !acceleratorText.equals("")) {
        //Get the maxAccWidth from the parent to calculate the offset.
        int accOffset = 0;
        Container parent = b.getParent();
        if (parent != null && parent instanceof JComponent) {
            JComponent p = (JComponent) parent;
            Integer maxValueInt = (Integer) p.getClientProperty(MotifGraphicsUtils.MAX_ACC_WIDTH);
            int maxValue = maxValueInt != null ? maxValueInt.intValue() : acceleratorRect.width;
            //Calculate the offset, with which the accelerator texts will be drawn with.
            accOffset = maxValue - acceleratorRect.width;
        }
        g.setFont(UIManager.getFont("MenuItem.acceleratorFont"));
        if (!model.isEnabled()) {
            // *** paint the acceleratorText disabled
            g.setColor(b.getBackground().brighter());
            SwingUtilities2.drawString(c, g, acceleratorText, acceleratorRect.x - accOffset, acceleratorRect.y + fm.getAscent());
            g.setColor(b.getBackground().darker());
            SwingUtilities2.drawString(c, g, acceleratorText, acceleratorRect.x - accOffset - 1, acceleratorRect.y + fm.getAscent() - 1);
        } else {
            // *** paint the acceleratorText normally
            if (model.isArmed() || (c instanceof JMenu && model.isSelected())) {
                g.setColor(foreground);
            } else {
                g.setColor(b.getForeground());
            }
            SwingUtilities2.drawString(c, g, acceleratorText, acceleratorRect.x - accOffset, acceleratorRect.y + fmAccel.getAscent());
        }
    }
    // Paint the Arrow
    if (arrowIcon != null) {
        if (model.isArmed() || (c instanceof JMenu && model.isSelected()))
            g.setColor(foreground);
        if (!(b.getParent() instanceof JMenuBar))
            arrowIcon.paintIcon(c, g, arrowRect.x, arrowRect.y);
    }
    g.setColor(holdc);
    g.setFont(holdf);
}
Also used : Insets(java.awt.Insets) Color(java.awt.Color) Rectangle(java.awt.Rectangle) Dimension(java.awt.Dimension) View(javax.swing.text.View) Font(java.awt.Font) Container(java.awt.Container) FontMetrics(java.awt.FontMetrics)

Aggregations

Container (java.awt.Container)250 JPanel (javax.swing.JPanel)70 JLabel (javax.swing.JLabel)65 JButton (javax.swing.JButton)56 JScrollPane (javax.swing.JScrollPane)55 BoxLayout (javax.swing.BoxLayout)50 FlowLayout (java.awt.FlowLayout)49 Component (java.awt.Component)46 BorderLayout (java.awt.BorderLayout)44 Dimension (java.awt.Dimension)39 ActionEvent (java.awt.event.ActionEvent)38 ActionListener (java.awt.event.ActionListener)26 JComponent (javax.swing.JComponent)22 JFrame (javax.swing.JFrame)22 JmriJFrame (jmri.util.JmriJFrame)20 Border (javax.swing.border.Border)18 JTable (javax.swing.JTable)17 JTextArea (javax.swing.JTextArea)17 Frame (java.awt.Frame)16 JTextField (javax.swing.JTextField)16