I thought I remembered seeing this widget in one of my books, but maybe
I'm starting to remember things for lack of sleep (and too much Dew).
It seems like I can create a simple button that will trigger a popup
element with a list in it (or whatever element I need). Is the route I
am going to need to take?
Many thanks, as always!
Nick
--
My software has no bugs. It just develops random features.
reply to ni...@DROPME.rightstep.org
... but then want me to subscribe before getting the code. I'm going to
keep digging around since I know I was not hallucinating and the widget
is possible. :)
Nicholas Pappas wrote:
> I found an example of what I am trying to do:
> http://www.esus.com/docs/GetQuestionPage.jsp?uid=1046
--
> I was wondering if there is an element, or a built in mutation of
> JComboBox (perhaps), that provided a drop down button like some
> programs have for their Undo button.
Sun's Look & Feel guidelines specify such a toolbar button. However,
there is no implmentation in Swing. All examples in the L&F guidelines
use a drop-down menu, but don't provide code. There are example images
(maybe done with Photoshop ...), but the guidelines are not even clear
if a click (button down + button up), or already a button down should
activate the menu.
I have never coded such a thingy, but if I would have to, I would start
with a JMenu.
I use it my a few of my apps.
-Jason
"Nicholas Pappas" <righ...@hotmail.com> wrote in message
news:3D7231EE...@hotmail.com...
There is no magic involved, just use a button and a normal popup menu,
so that the popup behaves as adrop-down menu.
Here is some code taken from one of my applications, and simplified
(maybe to much, might not compile :-)):
//
// Create an example toolbar menu with two menu items
//
JPopupMenu ToolbarMenu = new JPopupMenu();
JMenuItem ToolbarMenuItem1 = new JMenuItem();
ToolbarMenuItem1.setText("Item 1");
ToolbarMenu.add(ToolbarMenuItem1);
JMenuItem ToolbarMenuItem2 = new JMenuItem();
ToolbarMenuItem2.setText("Item 2");
ToolbarMenu.add(ToolbarMenuItem2);
//
// Create the toolbar with one toolbar button
//
JToolBar Toolbar = new JToolBar();
// Use a text for demo purposes. In a real application an
// icon with an L&F guidelines compliant marker should be used
JButton ToolbarButton = new JButton();
ToolbarButton.setText("Toolbar Button");
//
// The event handling is not L&F guidelines compliant because
// (a) the guidelines are rather vague regarding what should
// trigger the popup menu, and
// (b) the guidelines don't allow for a default behavior, which
// is, however, rather common.
//
// The following implements:
// Right-Mouse + platform popup trigger: Pop-up the menu
// Left-Click: Default action
//
ToolbarButton.addMouseListener(new MouseAdapter() {
void postToolbarMenu(MouseEvent e) {
if(e.isPopupTrigger()) {
Component c = (Component)e.getSource();
ToolbarMenu.show(c, 0, c.getHeight());
}
}
public void mousePressed(MouseEvent e) {
postToolbarMenu(e);
}
public void mouseReleased(MouseEvent e) {
postToolbarMenu(e);
}
});
ToolbarButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Default action");
}
});
Toolbar.add(ToolbarButton);
JPanel cp = (JPanel) this.getContentPane();
cp.setLayout(new BorderLayout());
cp.add(Toolbar, BorderLayout.NORTH);
/Thomas