IntelliJ Community . EmulatingIDEAButtons

 
EmulatingIDEAButtons 

HOME INDEX SEARCH CHANGES GO  
this code (with some work) will help you create buttons that resemble the buttons used in IDEA... if you can't use or don't want to use AnAction?..

    /**
     * give a button the correct size, border and rollover behavior
     * to emulate an IDEA button.
     *
     * @param theButton - the JButton to modify
     * @param isIcon - is this button an icon or text button.. icons will be sized at
     *                  24x24 while text buttons will have a width of font point size
     *                  multiplied by text length, and a height of 24.
     */
    private void setButtonBehavior(JButton theButton, boolean isIcon) {
        // icon is always 24x24...
        if (isIcon) {
            theButton.setPreferredSize(new Dimension(24, 24));
        } else {
            int points = theButton.getFont().getSize();
            int length = theButton.getText().length();
            // width of text buttons... this formula sucks...
            theButton.setPreferredSize(new Dimension((points * length), 24));
        }
        theButton.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
        theButton.setFocusPainted(false);
        theButton.addMouseListener(new MouseAdapter() {
            private final Color EXIT_COLOR = new Color(214, 211, 206);
            private final Color ENTER_COLOR = new Color(40, 40, 100);
            private final Color ENTER_BORDER_COLOR = new Color(130, 130, 170);

            public void mouseEntered(MouseEvent e) {
                JComponent jComponent = ((JComponent) e.getSource());
                // if this button is active...
                if (jComponent.isEnabled()) {
                    // background color for roll over effect... needs tuning
                    jComponent.setBackground(ENTER_BORDER_COLOR);
                    // border color on roll over... needs tuning
                    jComponent.setBorder(BorderFactory.createLineBorder(ENTER_COLOR, 1));
                }
            }

            public void mouseExited(MouseEvent e) {
                JComponent jComponent = ((JComponent) e.getSource());
                jComponent.setBackground(EXIT_COLOR);
                jComponent.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
            }
        });
    }

updated... this version behaves a bit better...

-- DanBachelder - 20 Jun 2002

e d i t a t t a c h r e f - b y d i f f s m o r e
Have ideas, requests, problems regarding this site? Send feedback.
Copyright © 2000-2003 by the contributing authors. All materials at intellij.org are the property of the contributing authors.