Keeping toolbar button pressed when displaying a popupmenu beneath it

I have written a custom widget - the drop down button. We can find examples of this widget all over the place - for eg. IE back button, Mail button, JBuilder run button etc.
The problem is this:
when the arrow button is pressed I display the popupmenu. Now, I need the button to hold its "pressed" state and appear accordingly while the popup menu is visible.
          arrowButton.addMouseListener(new MouseAdapter(){
               public void mousePressed(MouseEvent e)
                    showPopup();
Any ideas?

Here is your SSCE. Feel free to compile and run it. It will bring up a JFrame with toolbar buttons that show what I was talking about.
Note that the non popup buttons appear pressed when the mouse is kept pressed on them. Not so for the popup ones.
I cannot use an actionListener instead of mouse listener while displaying the popups because I need the popup to be visible with the first press of the mouse, not the first release (just like a combo-box)
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.UIManager;
public class Main3 {
     static JFrame frame = new JFrame("Test");
     static JToolBar toolBar = null;
     * @param args
     public static void main(String[] args)
          try
               UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
               toolBar = new JToolBar();
               toolBar.setFloatable(false);
               toolBar.setRollover(true);
               initToolBar();
               frame.getContentPane().add(toolBar, BorderLayout.NORTH);
               frame.pack();
               frame.setSize(800,600);
               frame.setVisible(true);
          catch (Exception ex)
               ex.printStackTrace();
     private static void initToolBar()
          final JPopupMenu popupMenu = new JPopupMenu();
          popupMenu.add(new JMenuItem("hello"));
          popupMenu.add(new JMenuItem("world"));
          final JButton button = new JButton("Popup JButton");
          button.addMouseListener(new MouseAdapter(){
               public void mousePressed(MouseEvent e)
                    popupMenu.show(button,0,button.getHeight());
          final JToggleButton toggleButton = new JToggleButton("Popup JToggleButton");
          toggleButton.addMouseListener(new MouseAdapter(){
               public void mousePressed(MouseEvent e)
                    popupMenu.show(toggleButton,0,toggleButton.getHeight());
          final JButton nonPopupButton = new JButton("No Popup JButton");
          nonPopupButton.addMouseListener(new MouseAdapter(){
               public void mousePressed(MouseEvent e)
                    System.out.println("Non popup button pressed");
          final JToggleButton nonPopupToggleButton = new JToggleButton("No Popup JToggleButton");
          nonPopupToggleButton.addMouseListener(new MouseAdapter(){
               public void mousePressed(MouseEvent e)
                    System.out.println("Non popup toggle button pressed");
          toolBar.add(button);
          toolBar.addSeparator();
          toolBar.add(toggleButton);
          toolBar.addSeparator();
          toolBar.add(nonPopupButton);
          toolBar.addSeparator();
          toolBar.add(nonPopupToggleButton);
}

Similar Messages

  • Power button pressed when ibook closed

    I have a iBook G4 14-inch. Its power button protrudes above the surface of the bottom panel. When the ibook is turned off and closed, the top bezel will press down into the button if the ibook is pressed together,such as being placed inside a case. This is my only ibook (have a PB alum and 2 pismos) so I cannot determine if the button should protrude above the surrounding panel. The ones on the pismos are recessed. When we pack the ibook, we have to remove the battery to keep it from booting up (and then have to reset the clock when it's up because there is no PRAM battery).
    I have taken this ibook apart several times to replace parts (including a complete new display) so I cannot be sure how the high thebutton should be. Thanks.
    PB G4 1.33 15 Aluminum    

    Thanks for the info. I assume if you press on the lid when it's closed, it does not power up.
    When I take off the top cover, I'll see if the heat shield or something else is keeping the button pushed upwards. It may be that the plastic case (top or bottom) is warped enought to make a difference. I replaced the entire display section with one from a 1Ghz ibook G4 and it may be causing a problem. I may have to swap back to the original screen bezel and case to make things work. Ugh.

  • Keep fixed resolution even when display not attached

    Is there any way to lock the display resolution on a Mac, even when there is no display attached? Or to change the set of display resolutions that are available when no display is attached?
    I connect a Mac to my A/V system via DVI -> HDMI. As long as the unit the Mac is connected to is turned on, the Mac gets reasonable EDID info and allows me to set it to 1920x1080, as desired. But after turning the A/V system off overnight, the Mac invariably reverts to some lower resolution, which wreaks havoc with window sizes and positions. I just want the Mac to stay at 1920x1080.
    I tried SwitchResX, which lets you add available resolutions and disable resolutions, but only when the display is attached, so it accomplishes nothing when the display is disconnected. I've confirmed the inability of SwitchResX to address the problem with its author.
    I purchased a Gefen DVI Detective Plus that theoretically learns the EDID information of a display, keeps that information available to the Mac at all times, and passes the video through it. This would be perfect, except A) it doesn't work correctly in the learning mode (I never did get an image up that way), and B) using a built-in EDID configuration it doesn't cleanly pass the video (it works sometimes, for a little while, but then sparklies start to appear and before long the HDMI connection is lost). Useless.
    I found information on the web about various .plist files, and I've edited most or all of them, all to no avail. I think I could give myself additional resolutions, while a display is connected, but, again, it's the behavior when there effectively is no display that is causing me problems.
    Unless someone here knows a way to lock the display resolution or to edit the set of resolutions available when a display is missing I fear I'll be moving and resizing windows for the rest of my days.
    (Note: This machine happens to still be on Tiger, but I'd be happy to upgrade it to Leopard if that would afford a solution.)

    Are you saying you already got it fixed but the problem is reaccuring??
    I am part of the unlucky ones who didnt't have applecare or the extended warranty. My iMac broke down 8 months ago. I no longer have any display. The strange thing was I left it at home because I went to uni, so there was a period of at least 10 months where it was not used. But still it failed within a few weeks of me getting back home. I was sucked in by their adverts making me believe they were a superior computer to any windows based. How wrong was I. It has now left me with a very bitter taste.
    Any of you guys know if it still will work if I connect an external monitor?

  • How to keep submit button script when distribute the form.

    I created a form with a submit button that generates an E-mail with a personalized subject and body message including some variables from the filled form.
    When I distribute the form, the Submit button changes with a default message and all my job is lost.
    Is ther any way to resolve my problem?

    Steps
    1. make a page with Submit Button and message Style Text item.
    2. Handle submit Button in Controller.
    3. Find current date and set for message Style Text item.
    Thanks

  • Keep button pressed?

    Hi, im coding asimple gui with a few buttons. The thing that has me stumped is that I cannot for the life of me keep a button "pressed" until it is clicked again (ie click once, it stays pressed, click it again or click a different button and it becomes "unpressed"). I have been changing the icon on the button to make it look pressed, and then setting a boolean flag so that when the button is clicked again, or a different button is pressed, the boolean flag is unset and the icon of the button reverts back to making it look "unpressed".
    Surely there is an easier way of doing this? Ie swing has method to do this built in?
    Thanks guys.

    Use javax.swing.JToggleButton.

  • Error -- No Such Trigger('when-button-pressed')

    Hi,
    I am getting the error ""No Such Trigger('when-button-pressed')"",
    when I am executing the code EXECUTE_TRIGGER( 'WHEN-BUTTON-PRESSED' );
    on some item(Key-Next-Item), although WHEN-BUTTON-PRESSED trigger exists.
    What could be the problem ?. It worked till yesterday, giving error from today, I havent
    changed any thing.
    Thanks in Advance
    Devender

    Steve's tip is the only good solution for this case.
    The problem in your case for example is:
    Let's say you have a block-trigger, which fires after your execute-trigger.
    Now a colleague of you, which don't know your code, create a WHEN-BUTTON-PRESSED on an item, then the new code is started and not the block-trigger-code.
    So, don't ever do this. Write your code in a package-function or -procedure and call it from the trigger. So you can re-user the functionality
    Gerd

  • When-button-pressed trigger save two records ?

    Hi all,
    I'm very new to Oracle form. I intend to make a very simple form where will allow user to enter and save record when finished.
    There I have build up the form and one push button where will called the when-button-presses when the user click.
    I have write the query as below :-
    begin
         if :device is null then
              message('Device no is null!!');
              raise form_trigger_failure;
         else
              insert into aic_std_cost_sell_price values ( :device, :asm_price, :tst_price);
              commit;
         end if;
    end;          
    however when save, two record had been inserted? and why ?
    rgds
    Lim

    When you create a block based on a database table, Forms builds and executes a SQL insert statement for you when you issue a commit statement. Therefore, yours is running, then Forms builds and executes one.
    Remove your insert statement. You don't need it.
    And change the COMMIT; to COMMIT_FORM; They do the same thing, but using commit_form will help you remember that lots of things are going on behind the scenes when you use it.

  • "duplicate button press" problem in jsf 1.1

    I think the problem come from "duplicate button press", when the users press the button several time, many request is generated, the sad is I don't have the source code of this page, I just have the source code which receive these request, what I want to do is just handle the first request from the button and ignore the other, is it possible?
    also, because I can't modify the problematic button page (I have not the source), so even I upgrade to jsf 1.2 is not useful, is it?
    thanks!!

    First, let me say that it is strange to try to solve this problem without being able to modify the source page. If this is a political or administrative restriction rather than a real technical limitation I suggest you push back on whomever has created this situation.
    The most effective method I can thing of would be to remove the state of the view from the session (assuming server side state saving) upon receiving the first request. This will cause the subsequent requests to fail. Although, this might not be the best due to timing issues. Also, keep in mind that the user will just see the error message, they will not get the results of the first request.
    So maybe a filter which intercepts the requests. It could perform duplicate detection by hashing all of the request parameters. You could even keep the responses around to resend. Oops, I've just re-invented HTTP caching; perhaps interjecting a product which already performs that role would be appropriate instead.

  • HELP: "Fitting" toolbar buttons not showing up in CS5

    Hi All,
    I can't figure out how to get this to work. The toolbar buttons in CS5 are a bit different than those that show up when I am in CS4 and I am not familiar with the changes.
    I like the "fit content to frame", "fit content proportionally", etc. as toolbar buttons.  I know there are keyboard shortcuts for these, which would be faster to learn... but I just don't work that way... I'm a visual person, I need to see and click on the buttons. But my CS4 buttons are gone in CS5.
    How do I get the fitting toolbar buttons back when I have selected an image that I want to modify?
    Thanks in advance!

    NVM... I answered my own question. The toolbar buttons that I was looking at were really there, but I just couldn't see them because too much space on my toolbar was taken up by stuff I don't use frequently.   Once I removed the extras that I didn't want, they appeared!

  • How can I change the width of bookmark toolbar buttons?

    I would simply like to alter the width of bookmark toolbar buttons to better display the bookmark title. I also have MultirowBookmarksToolbar addon installed.

    Add code to [http://kb.mozillazine.org/UserChrome.css userChrome.css] below the @namespace line.
    <pre><nowiki>@namespace url("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"); /* only needed once */
    </nowiki></pre><pre><nowiki>#personal-bookmarks toolbarbutton.bookmark-item { max-width: 20em; }</nowiki></pre>
    You can use this code for specific bookmarks (label name needs to be an exact, case sensitive, match)
    <pre><nowiki>#personal-bookmarks toolbarbutton.bookmark-item[label="Bookmark_Name"] { max-width: 20em; }</nowiki></pre>
    See http://kb.mozillazine.org/Editing_configuration

  • Touch pad buttons disabled when keyboard in use

    when a key on the built in keyboard is depressed the left and right cursor buttons are dissabled.
    the synaptics touchpad (7.5) menu allows "palm check enhanced" that stops acidental keypad touches whilst typing to be dissaled,but this did nt resolve the issue, this allows cursor movement when typing, but not left/right button press when typing
    the settings appear fragmented and the help section useful only if you know the correct term for the issue you are expreiancing, so any assistance woul be apreciated
    -will

    That normally happens when the person typing has inadvertently tapped the disable area on the touchpad.
    I do wish I could have been far more specific, but you only gave a general identification of your notebook instead of the complete product number or product name.
    Fn+Esc keys will show system information when you are logged into Windows. Post the complete product number here.
    ****Please click on Accept As Solution if a suggestion solves your problem. It helps others facing the same problem to find a solution easily****
    2015 Microsoft MVP - Windows Experience Consumer

  • I have reached out  to this community as my Iphone display gone black and got the solution to keep home and power button pressed. It worked but I would like to know why it went black

    I have reached out  to this community as my Iphone display gone black and got the solution to keep home and power button pressed. It worked but I would like to know
    1. Why it went black and is there a permanent fix for this
    2.Will the IOS gets automatically updated as I feel the IOS was 5 when I got the phone in 2012 december
    3.Do you suggest 7.1 for Iphone 4

    1. There is no way of knowing. These things happen.
    2. The iOS only gets updated when you want it to be.
    3. You should always update to the latest iOS your phone will support. Apple do not write the updates to make your experience worse.

  • I can't get the Bookmarks button (NOT the Display Your Bookmarks Button) to show in the Navigation Toolbar, except when I have the Customise window open.

    I think that covers it really. When I have the customise window open I can see the Bookmarks Button (like the Display Your Bookmarks Button but with a little arrow on the right hand side) but as soon as I click Done it vanishes. I've tried it in a number of positions on the Navigation toolbar but without success.

    My bookmarks button just moved itself out of the Navigation Toolbar and into the Bookmarks Toolbar. If there's a way to return it, let me know.
    It's using a whole new toolbar's worth of space (since the Bookmarks Toolbar is now double height) just for one dinky button...love the idea of maximizing real estate and this is messing that up.

  • How to display a message after button press ?

    Hi,
    I created a jspx page with a button.
    When that button is pressed, a lot of stuff is going to happen in my backing bean.
    This can take up to 30 seconds. (soap call etc ... )
    Meanwhile, the pressed button stays greyed out. When the action is finished, the button
    is available again.
    I want to display a message saying the action was succesfull or not, after that button is available again.
    Can anyone help me how I can make this ?
    Greetz.

    Why not opening a popup from your backing bean?
    Add a popup to your page:
    <af:popup id="yourID" bindings="#{yourBean.popup}">
    </af:popup>Open this in your backing bean:
        public void OpenPopup(String popupID) {
          FacesContext facesContext = FacesContext.getCurrentInstance();
          ExtendedRenderKitService service = Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);
          service.addScript(facesContext,"AdfPage.PAGE.findComponent('"+this.getPopup().getClientId(facesContext) + "').show();");
        }This way you can add anything you want in your popup. forms, messages, images,...

  • [Solved] Keep the last button pressed with a custom style

    Hello,
    I have a VBox with 20 buttons and I have this style applied (menu.css) (only for VBox wrapper):
    .button:focused {
        -fx-background-color: #0768A9;
        -fx-text-fill: #FFFFFF;
    But when I pressed other button outside the VBox the style disappears, because the other button get the focus.
    I'm afraid I'm taking the wrong way.
    How I can do to keep the last button pressed with a custom style? (Only for buttons containing the VBox).
    Best regards

    I created a simple method to change background colors in buttons:
        private void setSelectedBtnStyle(Button bboat) {
      // Change style oldSelectedBoat
        selectedBoat.setStyle("-fx-background-color: white;-fx-text-fill: black;");
        // oldSelectedBoat var change to newSelectedBoat
        this.selectedBoat = bboat;
      // Change style newSelectedBoat
        selectedBoat.setStyle("-fx-background-color: #0768A9;-fx-text-fill: #FFFFFF;");;
    Surely not the best solution, but it works for me.
    Best regards.

Maybe you are looking for