wiki-export/wiki-content/DevFaqDropdownMenuAddToolbar.xml (182 lines of code) (raw):

<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --><mediawiki xmlns="http://www.mediawiki.org/xml/export-0.3/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="0.3" xml:lang="en" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.3/ http://www.mediawiki.org/xml/export-0.3.xsd"> <siteinfo> <sitename>NetBeans Wiki</sitename> <base>http://wiki.netbeans.org/Main_Page</base> <generator>MediaWiki 1.15.1</generator> <case>first-letter</case> <namespaces> <namespace key="-2">Media</namespace> <namespace key="-1">Special</namespace> <namespace key="0"/> <namespace key="1">Talk</namespace> <namespace key="2">User</namespace> <namespace key="3">User talk</namespace> <namespace key="4">NetBeans Wiki</namespace> <namespace key="5">NetBeans Wiki talk</namespace> <namespace key="6">File</namespace> <namespace key="7">File talk</namespace> <namespace key="8">MediaWiki</namespace> <namespace key="9">MediaWiki talk</namespace> <namespace key="10">Template</namespace> <namespace key="11">Template talk</namespace> <namespace key="12">Help</namespace> <namespace key="13">Help talk</namespace> <namespace key="14">Category</namespace> <namespace key="15">Category talk</namespace> </namespaces> </siteinfo> <page> <title>DevFaqDropdownMenuAddToolbar</title> <id>7250</id> <revision> <id>40190</id> <timestamp>2010-07-24T20:33:44Z</timestamp> <contributor> <username>Jtulach</username> <id>526</id> </contributor> <text xml:space="preserve">__NOTOC__ __NOTOC__ ===How do I add a drop-down menu to a toolbar button?=== To add a drop-down menu to a component in a toolbar, you can either extend &lt;tt&gt;CallableSystemAction&lt;/tt&gt; and override &lt;tt&gt;public Component getToolbarPresenter()&lt;/tt&gt;, or implement &lt;tt&gt;javax.swing.Action&lt;/tt&gt; or any subclass thereof, and implement &lt;tt&gt;[http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/actions/Presenter.Toolbar.html Presenter.Toolbar]&lt;/tt&gt; which defines that method. You might want to create a &lt;tt&gt;JToggleButton&lt;/tt&gt;, and when the button is pressed, show a &lt;tt&gt;JPopupMenu&lt;/tt&gt;. (Also try &lt;tt&gt;org.openide.awt.DropDownButtonFactory&lt;/tt&gt;.) Example: &lt;source lang="java"&gt; public class PickDrawingLineAction extends CallableSystemAction { private static JToggleButton toggleButton; private static ButtonGroup buttonGroup; private static JPopupMenu popup; private MyMenuItemListener menuItemListener; List handledCharts; public void performAction() { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { toggleButton.setSelected(true); } }); } public String getName() { return "Pick Drawing Line"; } public HelpCtx getHelpCtx() { return HelpCtx.DEFAULT_HELP; } protected boolean asynchronous() { return false; } public Component getToolbarPresenter() { Image iconImage = Utilities.loadImage( "org/blogtrader/platform/core/netbeans/resources/drawingLine.png"); ImageIcon icon = new ImageIcon(iconImage); toggleButton = new JToggleButton(); toggleButton.setIcon(icon); toggleButton.setToolTipText("Pick Drawing Line"); popup = new JPopupMenu(); menuItemListener = new MyMenuItemListener(); handledCharts = PersistenceManager.getDefalut() .getAllAvailableHandledChart(); buttonGroup = new ButtonGroup(); for (AbstractHandledChart handledChart : handledCharts) { JRadioButtonMenuItem item = new JRadioButtonMenuItem(handledChart.toString()); item.addActionListener(menuItemListener); buttonGroup.add(item); popup.add(item); } toggleButton.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { int state = e.getStateChange(); if (state == ItemEvent.SELECTED) { /** show popup menu on toggleButton at position: (0, height) */ popup.show(toggleButton, 0, toggleButton.getHeight()); } } }); popup.addPopupMenuListener(new PopupMenuListener() { public void popupMenuCanceled(PopupMenuEvent e) { toggleButton.setSelected(false); } public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { toggleButton.setSelected(false); } public void popupMenuWillBecomeVisible(PopupMenuEvent e) { } }); return toggleButton; } private class MyMenuItemListener implements ActionListener { public void actionPerformed(ActionEvent ev) { JMenuItem item = (JMenuItem)ev.getSource(); String selectedStr = item.getText(); AnalysisChartTopComponent analysisTc = AnalysisChartTopComponent.getSelected(); if (analysisTc == null) { return; } AbstractChartViewContainer viewContainer = analysisTc.getSelectedViewContainer(); AbstractChartView masterView = viewContainer.getMasterView(); if (!(masterView instanceof WithDrawingPart)) { return; } DrawingPart drawingPart = ((WithDrawingPart)masterView).getCurrentDrawing(); if (drawingPart == null) { JOptionPane.showMessageDialog( WindowManager.getDefault().getMainWindow(), "Please add a layer firstly to pick line type", "Pick line type", JOptionPane.OK_OPTION, null); return; } AbstractHandledChart selectedHandledChart = null; for (AbstractHandledChart handledChart : handledCharts) { if (handledChart.toString().equalsIgnoreCase(selectedStr)) { selectedHandledChart = handledChart; break; } } if (selectedHandledChart == null) { return; } AbstractHandledChart handledChart = selectedHandledChart.createNewInstance(); handledChart.setPart(drawingPart); drawingPart.setHandledChart(handledChart); Series masterSeries = viewContainer.getMasterSeries(); DrawingDescriptor description = viewContainer.getDescriptors().findDrawingDescriptor( drawingPart.getLayerName(), masterSeries.getUnit(), masterSeries.getNUnits()); if (description != null) { Node stockNode = analysisTc.getActivatedNodes()[0]; Node node = stockNode.getChildren() .findChild(DescriptorGroupNode.DRAWINGS) .getChildren().findChild(description.getDisplayName()); if (node != null) { ViewAction action = (ViewAction)node.getLookup().lookup(ViewAction.class); assert action != null : "view action of this layer's node is null!"; action.view(); } } else { /** best effort, should not happen */ viewContainer.setCursorCrossVisible(false); drawingPart.setActived(true); SwitchHideShowDrawingLineAction.updateToolbar(viewContainer); } } } } &lt;/source&gt;</text> </revision> </page> </mediawiki>