Example usage for javax.swing JPanel addMouseListener

List of usage examples for javax.swing JPanel addMouseListener

Introduction

In this page you can find the example usage for javax.swing JPanel addMouseListener.

Prototype

public synchronized void addMouseListener(MouseListener l) 

Source Link

Document

Adds the specified mouse listener to receive mouse events from this component.

Usage

From source file:Main.java

public static void main(String[] args) {
    JComboBox combo = new JComboBox();
    combo.setEditable(true);/*from   w ww .  j  a  v a2 s  .  c  o  m*/
    for (int i = 0; i < 10; i++) {
        combo.addItem(i);
    }
    JLabel tip = new JLabel();
    tip.setText("Outside combobox");
    JPanel panel = new JPanel();
    panel.add(combo);
    panel.add(tip);
    panel.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseEntered(MouseEvent e) {
            tip.setText("Outside combobox");
        }

        @Override
        public void mouseExited(MouseEvent e) {
            Component c = SwingUtilities.getDeepestComponentAt(e.getComponent(), e.getX(), e.getY());
            tip.setText(c != null && SwingUtilities.isDescendingFrom(c, combo) ? "Inside combo box"
                    : "Outside combobox");
        }
    });
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

From source file:MenuDemo1.java

public static void main(String[] args) {
    // Create a window for this demo
    JFrame frame = new JFrame("Menu Demo");
    JPanel panel = new JPanel();
    frame.getContentPane().add(panel, "Center");

    // Create an action listener for the menu items we will create
    // The MenuItemActionListener class is defined below
    ActionListener listener = new MenuItemActionListener(panel);

    // Create some menu panes, and fill them with menu items
    // The menuItem() method is important.  It is defined below.
    JMenu file = new JMenu("File");
    file.setMnemonic('F');
    file.add(menuItem("New", listener, "new", 'N', KeyEvent.VK_N));
    file.add(menuItem("Open...", listener, "open", 'O', KeyEvent.VK_O));
    file.add(menuItem("Save", listener, "save", 'S', KeyEvent.VK_S));
    file.add(menuItem("Save As...", listener, "saveas", 'A', KeyEvent.VK_A));

    JMenu edit = new JMenu("Edit");
    edit.setMnemonic('E');
    edit.add(menuItem("Cut", listener, "cut", 0, KeyEvent.VK_X));
    edit.add(menuItem("Copy", listener, "copy", 'C', KeyEvent.VK_C));
    edit.add(menuItem("Paste", listener, "paste", 0, KeyEvent.VK_V));

    // Create a menubar and add these panes to it.
    JMenuBar menubar = new JMenuBar();
    menubar.add(file);/*from w  w w. jav a2 s.co m*/
    menubar.add(edit);

    // Add menubar to the main window.  Note special method to add menubars
    frame.setJMenuBar(menubar);

    // Now create a popup menu and add the some stuff to it
    final JPopupMenu popup = new JPopupMenu();
    popup.add(menuItem("Open...", listener, "open", 0, 0));
    popup.addSeparator(); // Add a separator between items
    JMenu colors = new JMenu("Colors"); // Create a submenu
    popup.add(colors); // and add it to the popup menu
    // Now fill the submenu with mutually-exclusive radio buttons
    ButtonGroup colorgroup = new ButtonGroup();
    colors.add(radioItem("Red", listener, "color(red)", colorgroup));
    colors.add(radioItem("Green", listener, "color(green)", colorgroup));
    colors.add(radioItem("Blue", listener, "color(blue)", colorgroup));

    // Arrange to display the popup menu when the user clicks in the window
    panel.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            // Check whether this is the right type of event to pop up a popup
            // menu on this platform.  Usually checks for right button down.
            if (e.isPopupTrigger())
                popup.show((Component) e.getSource(), e.getX(), e.getY());
        }
    });

    // Finally, make our main window appear
    frame.setSize(450, 300);
    frame.setVisible(true);
}

From source file:MenuTest.java

public MenuFrame() {
    setTitle("MenuTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    JMenu fileMenu = new JMenu("File");
    fileMenu.add(new TestAction("New"));

    // demonstrate accelerators

    JMenuItem openItem = fileMenu.add(new TestAction("Open"));
    openItem.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));

    fileMenu.addSeparator();/*from www  . j  a v a2 s.  co m*/

    saveAction = new TestAction("Save");
    JMenuItem saveItem = fileMenu.add(saveAction);
    saveItem.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));

    saveAsAction = new TestAction("Save As");
    fileMenu.add(saveAsAction);
    fileMenu.addSeparator();

    fileMenu.add(new AbstractAction("Exit") {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });

    // demonstrate check box and radio button menus

    readonlyItem = new JCheckBoxMenuItem("Read-only");
    readonlyItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            boolean saveOk = !readonlyItem.isSelected();
            saveAction.setEnabled(saveOk);
            saveAsAction.setEnabled(saveOk);
        }
    });

    ButtonGroup group = new ButtonGroup();

    JRadioButtonMenuItem insertItem = new JRadioButtonMenuItem("Insert");
    insertItem.setSelected(true);
    JRadioButtonMenuItem overtypeItem = new JRadioButtonMenuItem("Overtype");

    group.add(insertItem);
    group.add(overtypeItem);

    // demonstrate icons

    Action cutAction = new TestAction("Cut");
    cutAction.putValue(Action.SMALL_ICON, new ImageIcon("cut.gif"));
    Action copyAction = new TestAction("Copy");
    copyAction.putValue(Action.SMALL_ICON, new ImageIcon("copy.gif"));
    Action pasteAction = new TestAction("Paste");
    pasteAction.putValue(Action.SMALL_ICON, new ImageIcon("paste.gif"));

    JMenu editMenu = new JMenu("Edit");
    editMenu.add(cutAction);
    editMenu.add(copyAction);
    editMenu.add(pasteAction);

    // demonstrate nested menus

    JMenu optionMenu = new JMenu("Options");

    optionMenu.add(readonlyItem);
    optionMenu.addSeparator();
    optionMenu.add(insertItem);
    optionMenu.add(overtypeItem);

    editMenu.addSeparator();
    editMenu.add(optionMenu);

    // demonstrate mnemonics

    JMenu helpMenu = new JMenu("Help");
    helpMenu.setMnemonic('H');

    JMenuItem indexItem = new JMenuItem("Index");
    indexItem.setMnemonic('I');
    helpMenu.add(indexItem);

    // you can also add the mnemonic key to an action
    Action aboutAction = new TestAction("About");
    aboutAction.putValue(Action.MNEMONIC_KEY, new Integer('A'));
    helpMenu.add(aboutAction);

    // add all top-level menus to menu bar

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    menuBar.add(helpMenu);

    // demonstrate pop-ups

    popup = new JPopupMenu();
    popup.add(cutAction);
    popup.add(copyAction);
    popup.add(pasteAction);

    JPanel panel = new JPanel();
    panel.setComponentPopupMenu(popup);
    add(panel);

    // the following line is a workaround for bug 4966109
    panel.addMouseListener(new MouseAdapter() {
    });
}

From source file:com.devoteam.srit.xmlloader.core.report.derived.DerivedCounter.java

public void addMouseListenerForGraph(JPanel panel) {
    if (/*this instanceof StatAverage || this instanceof StatFlow*/ true) {
        panel.addMouseListener(new MouseListener() {

            java.awt.Color lastColor;

            public void mouseClicked(MouseEvent e) {
            }//from   w w w.  j a v  a  2s. c o  m

            public void mousePressed(MouseEvent e) {
                JFrameRTStats.instance().getJPanelBottom().removeAll();

                JPanel longpanel = generateLongRTStats();
                longpanel.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT);
                longpanel.setAlignmentY(java.awt.Component.TOP_ALIGNMENT);
                JFrameRTStats.instance().getJPanelBottom().add(longpanel);

                // We add graphics to jPanelBottom
                JFrameRTStats.instance().getJPanelBottom().add(getChartPanel());
                // We save the StatKey selected
                ModelTreeRTStats.instance().setStatSelected(id);
                // We update the panel for display the graph
                JFrameRTStats.instance().updatePanel();
            }

            public void mouseReleased(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseEntered(MouseEvent e) {
                lastColor = e.getComponent().getBackground();
                e.getComponent().setBackground(ModelTreeRTStats.instance().getColorByString("selectForGraph"));
            }

            public void mouseExited(MouseEvent e) {
                e.getComponent().setBackground(lastColor);
            }
        });
    }
}

From source file:com.floreantpos.jasperreport.swing.JRViewerPanel.java

protected void createHyperlinks(List elements, int offsetX, int offsetY) {
    if (elements != null && elements.size() > 0) {
        for (Iterator it = elements.iterator(); it.hasNext();) {
            JRPrintElement element = (JRPrintElement) it.next();

            JRImageMapRenderer imageMap = null;
            if (element instanceof JRPrintImage) {
                JRRenderable renderer = ((JRPrintImage) element).getRenderer();
                if (renderer instanceof JRImageMapRenderer) {
                    imageMap = (JRImageMapRenderer) renderer;
                    if (!imageMap.hasImageAreaHyperlinks()) {
                        imageMap = null;
                    }/*from   ww  w.  jav  a 2 s.  c om*/
                }
            }
            boolean hasImageMap = imageMap != null;

            JRPrintHyperlink hyperlink = null;
            if (element instanceof JRPrintHyperlink) {
                hyperlink = (JRPrintHyperlink) element;
            }
            boolean hasHyperlink = !hasImageMap && hyperlink != null
                    && hyperlink.getHyperlinkTypeValue() != HyperlinkTypeEnum.NONE;
            boolean hasTooltip = hyperlink != null && hyperlink.getHyperlinkTooltip() != null;

            if (hasHyperlink || hasImageMap || hasTooltip) {
                JPanel link;
                if (hasImageMap) {
                    Rectangle renderingArea = new Rectangle(0, 0, element.getWidth(), element.getHeight());
                    link = new ImageMapPanel(renderingArea, imageMap);
                } else //hasImageMap
                {
                    link = new JPanel();
                    if (hasHyperlink) {
                        link.addMouseListener(mouseListener);
                    }
                }

                if (hasHyperlink) {
                    link.setCursor(new Cursor(Cursor.HAND_CURSOR));
                }

                link.setLocation((int) ((element.getX() + offsetX) * realZoom),
                        (int) ((element.getY() + offsetY) * realZoom));
                link.setSize((int) (element.getWidth() * realZoom), (int) (element.getHeight() * realZoom));
                link.setOpaque(false);

                String toolTip = getHyperlinkTooltip(hyperlink);
                if (toolTip == null && hasImageMap) {
                    toolTip = "";//not null to register the panel as having a tool tip
                }
                link.setToolTipText(toolTip);

                pnlLinks.add(link);
                linksMap.put(link, element);
            }

            if (element instanceof JRPrintFrame) {
                JRPrintFrame frame = (JRPrintFrame) element;
                int frameOffsetX = offsetX + frame.getX() + frame.getLineBox().getLeftPadding().intValue();
                int frameOffsetY = offsetY + frame.getY() + frame.getLineBox().getTopPadding().intValue();
                createHyperlinks(frame.getElements(), frameOffsetX, frameOffsetY);
            }
        }
    }
}

From source file:net.sf.jasperreports.swing.JRViewerPanel.java

protected void createHyperlinks(List<JRPrintElement> elements, int offsetX, int offsetY) {
    if (elements != null && elements.size() > 0) {
        for (Iterator<JRPrintElement> it = elements.iterator(); it.hasNext();) {
            JRPrintElement element = it.next();

            AreaHyperlinksRenderable imageMap = null;
            if (element instanceof JRPrintImage) {
                Renderable renderer = ((JRPrintImage) element).getRenderer();
                if (renderer instanceof AreaHyperlinksRenderable) {
                    imageMap = (AreaHyperlinksRenderable) renderer;
                    if (!imageMap.hasImageAreaHyperlinks()) {
                        imageMap = null;
                    }// ww w  .j av a  2 s  .c  o m
                }
            }
            boolean hasImageMap = imageMap != null;

            JRPrintHyperlink hyperlink = null;
            if (element instanceof JRPrintHyperlink) {
                hyperlink = (JRPrintHyperlink) element;
            }
            boolean hasHyperlink = !hasImageMap && hyperlink != null
                    && hyperlink.getHyperlinkTypeValue() != HyperlinkTypeEnum.NONE;
            boolean hasTooltip = hyperlink != null && hyperlink.getHyperlinkTooltip() != null;

            if (hasHyperlink || hasImageMap || hasTooltip) {
                JPanel link;
                if (hasImageMap) {
                    Rectangle renderingArea = new Rectangle(0, 0, element.getWidth(), element.getHeight());
                    link = new ImageMapPanel(renderingArea, imageMap);
                } else //hasImageMap
                {
                    link = new JPanel();
                    if (hasHyperlink) {
                        link.addMouseListener(mouseListener);
                    }
                }

                if (hasHyperlink) {
                    link.setCursor(new Cursor(Cursor.HAND_CURSOR));
                }

                link.setLocation((int) ((element.getX() + offsetX) * realZoom),
                        (int) ((element.getY() + offsetY) * realZoom));
                link.setSize((int) (element.getWidth() * realZoom), (int) (element.getHeight() * realZoom));
                link.setOpaque(false);

                String toolTip = getHyperlinkTooltip(hyperlink);
                if (toolTip == null && hasImageMap) {
                    toolTip = "";//not null to register the panel as having a tool tip
                }
                link.setToolTipText(toolTip);

                pnlLinks.add(link);
                linksMap.put(link, hyperlink);
            }

            if (element instanceof JRPrintFrame) {
                JRPrintFrame frame = (JRPrintFrame) element;
                int frameOffsetX = offsetX + frame.getX() + frame.getLineBox().getLeftPadding();
                int frameOffsetY = offsetY + frame.getY() + frame.getLineBox().getTopPadding();
                createHyperlinks(frame.getElements(), frameOffsetX, frameOffsetY);
            }
        }
    }
}

From source file:userinterface.properties.GUIGraphHandler.java

public int addGraph(JPanel m, String tabName) {
    // add the model to the list of models
    models.add(m);//from ww  w  .  ja  v a  2  s.  co  m

    // make the graph appear as a tab
    theTabs.add(m);

    options.add(new GraphOptions(plug, m, plug.getGUI(), "Options for graph " + tabName));

    if (m instanceof ChartPanel)
        // anything that happens to the graph should propagate
        m.addMouseListener(this);
    else if (m instanceof Graph3D)
        ((Graph3D) m).addMouseListener(this);

    // get the index of this model in the model list
    int index = models.indexOf(m);

    // increase the graph count and title the tab
    theTabs.setTitleAt(index, tabName);

    //set the tabclosepanel component so we see the close button
    TabClosePanel closePanel = new TabClosePanel(tabName);
    closePanel.addMouseListener(this);
    theTabs.setTabComponentAt(index, closePanel);

    // make this new tab the default selection
    theTabs.setSelectedIndex(theTabs.indexOfComponent(m));

    // return the index of the component
    return index;
}

From source file:com.openbravo.pos.util.JRViewer411.java

protected void createHyperlinks(List<JRPrintElement> elements, int offsetX, int offsetY) {
    if (elements != null && elements.size() > 0) {
        for (Iterator<JRPrintElement> it = elements.iterator(); it.hasNext();) {
            JRPrintElement element = it.next();

            JRImageMapRenderer imageMap = null;
            if (element instanceof JRPrintImage) {
                JRRenderable renderer = ((JRPrintImage) element).getRenderer();
                if (renderer instanceof JRImageMapRenderer) {
                    imageMap = (JRImageMapRenderer) renderer;
                    if (!imageMap.hasImageAreaHyperlinks()) {
                        imageMap = null;
                    }/*from   w ww .  j a v a  2 s  .com*/
                }
            }
            boolean hasImageMap = imageMap != null;

            JRPrintHyperlink hyperlink = null;
            if (element instanceof JRPrintHyperlink) {
                hyperlink = (JRPrintHyperlink) element;
            }
            boolean hasHyperlink = !hasImageMap && hyperlink != null
                    && hyperlink.getHyperlinkTypeValue() != HyperlinkTypeEnum.NONE;
            boolean hasTooltip = hyperlink != null && hyperlink.getHyperlinkTooltip() != null;

            if (hasHyperlink || hasImageMap || hasTooltip) {
                JPanel link;
                if (hasImageMap) {
                    Rectangle renderingArea = new Rectangle(0, 0, element.getWidth(), element.getHeight());
                    link = new ImageMapPanel(renderingArea, imageMap);
                } else //hasImageMap
                {
                    link = new JPanel();
                    if (hasHyperlink) {
                        link.addMouseListener(mouseListener);
                    }
                }

                if (hasHyperlink) {
                    link.setCursor(new Cursor(Cursor.HAND_CURSOR));
                }

                link.setLocation((int) ((element.getX() + offsetX) * realZoom),
                        (int) ((element.getY() + offsetY) * realZoom));
                link.setSize((int) (element.getWidth() * realZoom), (int) (element.getHeight() * realZoom));
                link.setOpaque(false);

                String toolTip = getHyperlinkTooltip(hyperlink);
                if (toolTip == null && hasImageMap) {
                    toolTip = "";//not null to register the panel as having a tool tip
                }
                link.setToolTipText(toolTip);

                pnlLinks.add(link);
                linksMap.put(link, hyperlink);
            }

            if (element instanceof JRPrintFrame) {
                JRPrintFrame frame = (JRPrintFrame) element;
                int frameOffsetX = offsetX + frame.getX() + frame.getLineBox().getLeftPadding().intValue();
                int frameOffsetY = offsetY + frame.getY() + frame.getLineBox().getTopPadding().intValue();
                createHyperlinks(frame.getElements(), frameOffsetX, frameOffsetY);
            }
        }
    }
}

From source file:neg.JRViewer.java

protected void createHyperlinks(List elements, int offsetX, int offsetY) {
    if (elements != null && elements.size() > 0) {
        for (Iterator it = elements.iterator(); it.hasNext();) {
            JRPrintElement element = (JRPrintElement) it.next();

            JRImageMapRenderer imageMap = null;
            if (element instanceof JRPrintImage) {
                JRRenderable renderer = ((JRPrintImage) element).getRenderer();
                if (renderer instanceof JRImageMapRenderer) {
                    imageMap = (JRImageMapRenderer) renderer;
                    if (!imageMap.hasImageAreaHyperlinks()) {
                        imageMap = null;
                    }//  w w  w .  j a v a2 s  . c o  m
                }
            }
            boolean hasImageMap = imageMap != null;

            JRPrintHyperlink hyperlink = null;
            if (element instanceof JRPrintHyperlink) {
                hyperlink = (JRPrintHyperlink) element;
            }
            boolean hasHyperlink = !hasImageMap && hyperlink != null
                    && hyperlink.getHyperlinkType() != JRHyperlink.HYPERLINK_TYPE_NONE;
            boolean hasTooltip = hyperlink != null && hyperlink.getHyperlinkTooltip() != null;

            if (hasHyperlink || hasImageMap || hasTooltip) {
                JPanel link;
                if (hasImageMap) {
                    Rectangle renderingArea = new Rectangle(0, 0, element.getWidth(), element.getHeight());
                    link = new ImageMapPanel(renderingArea, imageMap);
                } else //hasImageMap
                {
                    link = new JPanel();
                    if (hasHyperlink) {
                        link.addMouseListener(mouseListener);
                    }
                }

                if (hasHyperlink) {
                    link.setCursor(new Cursor(Cursor.HAND_CURSOR));
                }

                link.setLocation((int) ((element.getX() + offsetX) * realZoom),
                        (int) ((element.getY() + offsetY) * realZoom));
                link.setSize((int) (element.getWidth() * realZoom), (int) (element.getHeight() * realZoom));
                link.setOpaque(false);

                String toolTip = getHyperlinkTooltip(hyperlink);
                if (toolTip == null && hasImageMap) {
                    toolTip = "";//not null to register the panel as having a tool tip
                }
                link.setToolTipText(toolTip);

                pnlLinks.add(link);
                linksMap.put(link, element);
            }

            if (element instanceof JRPrintFrame) {
                JRPrintFrame frame = (JRPrintFrame) element;
                int frameOffsetX = offsetX + frame.getX() + frame.getLineBox().getLeftPadding().intValue();
                int frameOffsetY = offsetY + frame.getY() + frame.getLineBox().getTopPadding().intValue();
                createHyperlinks(frame.getElements(), frameOffsetX, frameOffsetY);
            }
        }
    }
}

From source file:cn.pholance.datamanager.common.components.JRViewer.java

protected void createHyperlinks(List<JRPrintElement> elements, int offsetX, int offsetY) {
    if (elements != null && elements.size() > 0) {
        for (Iterator<JRPrintElement> it = elements.iterator(); it.hasNext();) {
            JRPrintElement element = it.next();

            ImageMapRenderable imageMap = null;
            if (element instanceof JRPrintImage) {
                Renderable renderer = ((JRPrintImage) element).getRenderable();
                if (renderer instanceof ImageMapRenderable) {
                    imageMap = (ImageMapRenderable) renderer;
                    if (!imageMap.hasImageAreaHyperlinks()) {
                        imageMap = null;
                    }//from   w  w w .  ja va2s .  c  o  m
                }
            }
            boolean hasImageMap = imageMap != null;

            JRPrintHyperlink hyperlink = null;
            if (element instanceof JRPrintHyperlink) {
                hyperlink = (JRPrintHyperlink) element;
            }
            boolean hasHyperlink = !hasImageMap && hyperlink != null
                    && hyperlink.getHyperlinkTypeValue() != HyperlinkTypeEnum.NONE;
            boolean hasTooltip = hyperlink != null && hyperlink.getHyperlinkTooltip() != null;

            if (hasHyperlink || hasImageMap || hasTooltip) {
                JPanel link;
                if (hasImageMap) {
                    Rectangle renderingArea = new Rectangle(0, 0, element.getWidth(), element.getHeight());
                    link = new ImageMapPanel(renderingArea, imageMap);
                } else //hasImageMap
                {
                    link = new JPanel();
                    if (hasHyperlink) {
                        link.addMouseListener(mouseListener);
                    }
                }

                if (hasHyperlink) {
                    link.setCursor(new Cursor(Cursor.HAND_CURSOR));
                }

                link.setLocation((int) ((element.getX() + offsetX) * realZoom),
                        (int) ((element.getY() + offsetY) * realZoom));
                link.setSize((int) (element.getWidth() * realZoom), (int) (element.getHeight() * realZoom));
                link.setOpaque(false);

                String toolTip = getHyperlinkTooltip(hyperlink);
                if (toolTip == null && hasImageMap) {
                    toolTip = "";//not null to register the panel as having a tool tip
                }
                link.setToolTipText(toolTip);

                pnlLinks.add(link);
                linksMap.put(link, hyperlink);
            }

            if (element instanceof JRPrintFrame) {
                JRPrintFrame frame = (JRPrintFrame) element;
                int frameOffsetX = offsetX + frame.getX() + frame.getLineBox().getLeftPadding().intValue();
                int frameOffsetY = offsetY + frame.getY() + frame.getLineBox().getTopPadding().intValue();
                createHyperlinks(frame.getElements(), frameOffsetX, frameOffsetY);
            }
        }
    }
}