Example usage for javax.swing JLabel setComponentPopupMenu

List of usage examples for javax.swing JLabel setComponentPopupMenu

Introduction

In this page you can find the example usage for javax.swing JLabel setComponentPopupMenu.

Prototype

@BeanProperty(preferred = true, description = "Popup to show")
public void setComponentPopupMenu(JPopupMenu popup) 

Source Link

Document

Sets the JPopupMenu for this JComponent.

Usage

From source file:Main.java

public Main() {
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setSize(400, 300);/*from   w w w . j a v  a 2s.c o m*/
    jPopupMenu1.add(jMenuItem1);
    jTabbedPane1.addTab(null, jLabel1);
    jTabbedPane1.addTab(null, jLabel2);
    getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
    int tabCount = jTabbedPane1.getTabCount();
    for (int i = 0; i < tabCount; i++) {
        JLabel jLabel = new JLabel("Testing the tab" + (i + 1));
        jTabbedPane1.setTabComponentAt(i, jLabel);
        jLabel.setName(String.valueOf(i));
        jLabel.setComponentPopupMenu(jPopupMenu1);
    }
    jPopupMenu1.addPopupMenuListener(new PopupMenuListener() {

        @Override
        public void popupMenuCanceled(final PopupMenuEvent evt) {
        }

        @Override
        public void popupMenuWillBecomeInvisible(final PopupMenuEvent evt) {
        }

        @Override
        public void popupMenuWillBecomeVisible(final PopupMenuEvent evt) {
            JPopupMenu source = (JPopupMenu) evt.getSource();
            JLabel invoker = (JLabel) source.getInvoker();
            JLabel component = (JLabel) jTabbedPane1.getComponentAt(Integer.parseInt(invoker.getName()));
            jMenuItem1.setText(invoker.getText() + ":  " + component.getText());
        }
    });
}

From source file:org.thelq.stackexchange.dbimport.gui.GUI.java

/**
 * Update the list of locations// ww w .ja v  a 2 s . co  m
 */
protected void updateLocations() {
    locationsBuilder.getPanel().removeAll();
    for (final DumpContainer curContainer : controller.getDumpContainers()) {
        //Initialize components
        if (curContainer.getGuiHeader() == null) {
            String longLocation = Utils.getLongLocation(curContainer);
            JLabel headerLabel = new JLabel(longLocation);
            headerLabel.setToolTipText(longLocation);
            headerLabel.setIcon(UIManager.getIcon("Tree.collapsedIcon"));
            JPopupMenu popupMenu = new JPopupMenu();
            JMenuItem deleteItem = new JMenuItem("Delete");
            deleteItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    log.info("Removing " + curContainer.getType() + " " + curContainer.getLocation());
                    controller.getDumpContainers().remove(curContainer);
                    updateLocations();
                }
            });
            popupMenu.add(deleteItem);
            headerLabel.setComponentPopupMenu(popupMenu);
            curContainer.setGuiHeader(headerLabel);
            //Handlers
            headerLabel.addMouseListener(new MouseAdapter() {
                boolean visible = true;

                @Override
                public void mouseClicked(MouseEvent e) {
                    //Update labels
                    visible = !visible;
                    for (DumpEntry curEntry : curContainer.getEntries()) {
                        curEntry.getGuiName().setVisible(visible);
                        curEntry.getGuiSize().setVisible(visible);
                        curEntry.getGuiLog().setVisible(visible);
                        if (curEntry.getGuiSeparator() != null)
                            curEntry.getGuiSeparator().setVisible(visible);
                    }

                    //Change icon
                    if (visible)
                        curContainer.getGuiHeader().setIcon(UIManager.getIcon("Tree.expandedIcon"));
                    else
                        curContainer.getGuiHeader().setIcon(UIManager.getIcon("Tree.collapsedIcon"));
                    locationsPane.revalidate();
                }
            });
        }
        if (curContainer.getGuiTablePrefix() == null) {
            JTextField headerPrefix = new JTextField(6);
            curContainer.setGuiTablePrefix(headerPrefix);
            headerPrefix.setText(Utils.genTablePrefix(curContainer.getName()));
            if (StringUtils.isBlank(headerPrefix.getText()))
                log.warn("Unable to generate a table prefix for {}", curContainer.getLocation());
        }

        //Start adding to panel
        locationsBuilder.leadingColumnOffset(0);
        locationsBuilder.append(curContainer.getGuiHeader(), 7);
        locationsBuilder.append(curContainer.getGuiTablePrefix());
        locationsBuilder.nextLine();
        locationsBuilder.leadingColumnOffset(2);

        Iterator<DumpEntry> entriesItr = curContainer.getEntries().iterator();
        while (entriesItr.hasNext()) {
            DumpEntry curEntry = entriesItr.next();
            if (curEntry.getGuiName() == null)
                curEntry.setGuiName(new JLabel(curEntry.getName()));
            locationsBuilder.append(curEntry.getGuiName());
            if (curEntry.getGuiSize() == null)
                curEntry.setGuiSize(new JLabel(sizeInMegabytes(curEntry.getSizeBytes())));
            locationsBuilder.append(curEntry.getGuiSize());
            if (curEntry.getGuiLog() == null)
                curEntry.setGuiLog(new JLabel("Waiting..."));
            locationsBuilder.append(curEntry.getGuiLog(), 3);
            locationsBuilder.nextLine();
            if (entriesItr.hasNext()) {
                if (curEntry.getGuiSeparator() == null)
                    curEntry.setGuiSeparator(new JSeparator());
                locationsBuilder.append(curEntry.getGuiSeparator(), 7);
                locationsBuilder.nextLine();
            }
        }
    }

    locationsPane.validate();
}