JList where mouse click acts like ctrl-mouse click

I'd like to create a JList where I can select multiple items as if the control key were down, but without having the control key down. I thought that perhaps I could do this by extending JList and overriding its processMouseEvent method, setting the MouseEvent object to think that control is pressed and then calling the super method like so, but it doesn't work (I still need to press the control key to get my desired effect):
import java.awt.Component;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class Ctrl_Down_JList {
  private static void createAndShowUI() {
    String[] items = {"Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"};
    JList myJList = new JList(items) {
      @Override
      protected void processMouseEvent(MouseEvent e) {
        // change the modifiers to believe that control key is down
        int modifiers = e.getModifiers() | InputEvent.CTRL_DOWN_MASK;
        // can I use this anywhere?  I don't see how to change the
        // modifiersEx of the MouseEvent
        int modifiersEx = e.getModifiersEx() | InputEvent.CTRL_DOWN_MASK;
        MouseEvent myME = new MouseEvent(
            (Component) e.getSource(),
            e.getID(),
            e.getWhen(),
            modifiers, // my changed modifier
            e.getX(),
            e.getY(),
            e.getXOnScreen(),
            e.getYOnScreen(),
            e.getClickCount(),
            e.isPopupTrigger(),
            e.getButton());
        super.processMouseEvent(myME);
    JFrame frame = new JFrame("Ctrl_Down_JList");
    frame.getContentPane().add(new JScrollPane(myJList));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        createAndShowUI();
}Any ideas would be much appreciated!

Any ideas would be much appreciated!Here is a hack, idea is same as yours, but using Robot:
public static void createAndShowUI2() {
     String[] items = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
     final JList myJList = new JList(items);
     myJList.addMouseListener(new MouseAdapter() {
          Robot robot;
               try {
                    robot = new Robot();
               } catch (AWTException ex) {
                    ex.printStackTrace();
          @Override
          public void mouseEntered(MouseEvent e) {
               if (robot != null)
                    robot.keyPress(KeyEvent.VK_CONTROL);
          @Override
          public void mouseExited(MouseEvent e) {
               if (robot != null)
                    robot.keyRelease(KeyEvent.VK_CONTROL);
     JFrame frame = new JFrame("Ctrl_Down_JList");
     frame.getContentPane().add(new JScrollPane(myJList));
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.pack();
     frame.setLocationRelativeTo(null);
     frame.setVisible(true);
}It does work, but there has to be some cleaner way of doing this, perhaps using SelectionModel but I don't know about it.
Thanks!
Edit: I found that if Control key is pressed manually, then this hack obviously, to handle this, it has to made dirtier:
myJList.addMouseListener(new MouseAdapter() {
     Robot robot;
          try {
               robot = new Robot();
          } catch (AWTException ex) {
               ex.printStackTrace();
     Timer timer = new Timer(20, new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
               if (robot != null)
                    robot.keyPress(KeyEvent.VK_CONTROL);
        @Override
     public void mouseEntered(MouseEvent e) {
          if (robot != null) {
               robot.keyPress(KeyEvent.VK_CONTROL);
               timer.start();
        @Override
     public void mouseExited(MouseEvent e) {
          if (robot != null) {
               robot.keyRelease(KeyEvent.VK_CONTROL);
               timer.stop();
});Edited by: T.B.M on Mar 13, 2010 10:06 PM

Similar Messages

  • Shift key in a web page is acting like ctrl+f search key?????

    i installed some addons today after a while i noticed .problem with my shift key
    it is fine while using on text boxes but shift +any alphabet leads to search box similar to ctrl+f box.

    Hello gandhi.amar2,
    Thank you for contacting Mozilla Support. '''Try Firefox Safe Mode''' to see if the problem goes away. [[Troubleshoot Firefox issues using Safe Mode|Firefox Safe Mode]] is a troubleshooting mode that turns off some settings and disables most add-ons (extensions and themes).
    ''(If you're using an added theme, switch to the Default theme.)''
    If Firefox is open, you can restart in Firefox Safe Mode from the Help menu by clicking on the '''Restart with Add-ons Disabled...''' menu item:<br>
    [[Image:FirefoxSafeMode|width=520]]<br><br>
    If Firefox is not running, you can start Firefox in Safe Mode as follows:
    * On Windows: Hold the '''Shift''' key when you open the Firefox desktop or Start menu shortcut.
    * On Mac: Hold the '''option''' key while starting Firefox.
    * On Linux: Quit Firefox, go to your Terminal and run ''firefox -safe-mode'' <br>(you may need to specify the Firefox installation path e.g. /usr/lib/firefox)
    ''Once you get the pop-up, just select "'Start in Safe Mode"''
    [[Image:Safe Mode Fx 15 - Win]]
    '''''If the issue is not present in Firefox Safe Mode''''', your problem is probably caused by an extension, and you need to figure out which one. Please follow the [[Troubleshoot extensions, themes and hardware acceleration issues to solve common Firefox problems]] article to find the cause.
    ''To exit Firefox Safe Mode, just close Firefox and wait a few seconds before opening Firefox for normal use again.''
    When you figure out what's causing your issues, please let us know. It might help others with the same problem.
    Cheers,
    Patrick

  • Hi, my mac has recently been acting strange. First started about a week or so ago, where it was acting like it would get hung up; but would eventually fix itself. Now, I am having to restart it constantly just to entere a web address. Help please?

    Can someone please help me to see if I can figure out what is wrong? I looked to see if there was any updates that I overlooked, however, it stated that all updates had been made. I am completely lost. Thank You.

    You are in a situation where it may not be a big deal, or your hard drive could be failing and everything on your computer could be at risk. That means everything gets erased if you don't have a copy of it. You may want to make your first priority getting an external hard drive and making a copy of all your music, pics, movies, ect. while you still can, if you still can. It may turn out to not be a big deal, a directory issue or your'e out of space or something, but better to have a back up and not need it, than to lose everything that's important to you.
    If you value your stuff, stop trying to use the computer and back it up, before you do anything else.
    It sounds like you should then take it to an Apple Service Provider like an Apple Store.

  • Can't type in form fields, acts like right mouse click.

    Using the mouse pad or magic mouse - anytime I single click in a fillable field it acts like a right or two finger click and will not let me type in the fields. It won't even let me type in URLs.

    I have this problem too. I've disabled all add ons except Foxtab 1.4.9 to no avail. The problem goes away when Foxtab is disabled. But I love Foxtab.
    Interesting discovery: everything is fine - I can type merrily along like I am here - until I click my mouse. Then the problem occurs. I have to go to another text box, type some stuff then come back. Both my Apple computers are experiencing the same problem. Both have wireless mac mice. Both started the same problem about 3 months ago. Not using my mouse gets round the problem but it's a pain because I am often cutting and pasting text into a text box. Any ideas?

  • Hi, how can I stop my screen from freezing up.Its acts like a scratch card, I use the mouse cursor to rub away to find the password box?

    HI,how can I stop my screen from freezing, It acts like a scratch card,I run over it with the mouse to scratch away at the grey screen to find the password box
    underneath?

    OS X: Login window partially appears, cursor movement redraws screen

  • Mouse left button acting like right button

    I have a MacBook black with snow Leopard. My mouse is acting up. On both the laptop and a usb mouse the left button is acting like athe write button by bringing up action boxes instead of just doing the deffault action. for example I go to the task bar and to open a program and the box "open/ options" open up. what's the solution?

    Is the Control key stuck down? Try another keyboard.

  • Adobe Stamps Don't Place Where Mouse Clicks

    Hello,
    My firm is experiencing some issues regarding stamps.  We've created a library of stamps for the employees to use in their documentation.  However, some users are noticing that the stamps don't place where they click.  For instance, a user will select a checkmark stamp, hover the mouse over a box on the pdf file, and click on the box to place the stamp.  The user wants the checkmark to appear inside the box.  However, for some odd reason the stamp appears 20 or so pixels above or below where the user clicks.  It's quite random.  And this occurs on more than one PC type (three different kinds of Toshiba laptops).  It also occurs in both XP and in Windows 7.  Has anyone noticed this?  Does anyone know how to fix this?  I have updated both Adobe and the mouse drivers to see if it makes a difference - it didn't.  Any suggestions would be appreciated!
    Thanks,
    Teresa

    Acrobat is kind of quirky about pasting into the file. Pastes tend to be done at either the top left or the middle of the page, depending on the version of Acrobat. You then have to use the object touchup tool to move the paste to where you want it.

  • Type anywhere in JTextArea where mouse clicks

    Is it possible to start typing anywhere where the mouse clicks with a JtextArea ? I have an application that needs to allow users to start typing at the position where mouse clicks. Be default, JtextArea starts at (0,0).

    Well, this might not be the cleanest way, but you could create a mouselistener on the TextArea (or subclass it), and
    1) check to see if the clicked position refers to a valid offset in the textArea. If so, you don't really have to do anything, since the basic functionality of the thing should drop the caret in just where you want it. ModelToView() will help you figure out where in the document the mouse clicked.
    2) if not, figure out how many lines down/ tabs over need to be appended to the document to get the cursor where you want it, then insert them
    3) move the cursor to the end of the document.
    this might not be all that clean, especially since fonts vary in width. there are a number of ways you can find out how wide a string is in a given font, though.
    hope that helps

  • Intuos3 pen tip acts like right click in Photohop CS4

    I have a Intuos3 5 wide. It works great in various applications under Windows Vista 32bit and the latest drivers.
    I'm using a normal Logitech mouse which is set to left-handed because I use the mouse with my left hand. The Intuos pen is set to the default right handed.
    So far no problems except for Photoshop CS4.
    In CS4 the pen tip acts like a right click. I can only draw or click when I keep de lower button on the pen pressed down. The eraser does exactly the same, thus showing the context menu).
    I tried to reverse the pen functions and the mouse handedness for Photoshop only but this didn't work the right way. I can assign a right click to the tip so I can draw and click as it should be. But I cannot do this for the eraser so I'm not satisfied with this workaround.
    I have this problem in Photoshop CS4 only. Not in Lightroom or Bridge (or other non Adobe programs). Anyone who can help me with this problem?

    Hey there,
    I definitely hear you about Split Motion. There is an enhancement request for supporting multiple layers, and adding it as a keyboard shortcut. The first one is tricky, but the second one is not. I think there was hope that splitting motions wouldn't be necessary much (especially since you have to be careful when doing so when eases are involved), which is why the eventual need for more functionality related to split motion was realized a bit late in the schedule.
    Now regarding other operations, such as you mentioned F5 to insert frames - that and other operations (inserting certain types of property keyframes, F6, and so on) work just fine.
    You can apply a shortcut to Create Motion Tween in the custom keyboard shortcut dialog.  Not all operations in the program are available though (most context menu items aren't), which is why unfortunately why we don't see Split Motion yet.
    It might be possible to avoid Split Motion though with another workflow. What are you using the feature for?  Just curious in case I can help.
    Jen.

  • Left click acting like both left and right click

    Left Click button is acting like both left and right click button HP 2000 Notebook PC

    Could you try to use another mouse? If the other mouse works perfect, then your mouse pad must be damaged and you should look to get it fixed or just use a mouse instead. If not, then the settings on how the clicks act must have been altered. 
    Try to find a mouse to try first before looking at the settings for your mouse pad. You could maybe borrow one if anyone else has a computer with a mouse in your home.
    While you do that, I'll work on searching if this is common and if their is a solution if it's the software.
    I'm a programmer and a computer expert in training.
    Please feel free to click the "Kudos!" on the left if I helped. Note* I do not work for HP

  • I have a Yahoo search bar acting like an App tab that I can't get rid of. There is no option to unpin when I right click on it.

    I have a yahoo search bar which was installed during a download and installation of SIW.exe a system information application from http://gtopala.com/. It was the freeware version which is add based software. However I didn't expect that the search function would be such a nusance and interfere with Firefox's functions.
    The search bar is acting like a App tab that I can't move or get rid of. Ther is no option to unpin the object when I right click on the bar or to change it in any way using the 'group your tab' function to the right of the tab tool bar.

    Reply: Cor-el > Excellent!! Thank You. In Tools, Add Ons, Extensions there was a Default Tab enntry which was the culprit. Am running W7 so the XP Tools, Options, Advanced , Use Hardware acceleration when available, wasn't of much help here, however I did fix a problem on another computer running XP with that same fix. Thank You very much! HD

  • I am having a problem. When I click on the Mobile Facebook icon it acts like it is going to start opening then it quits and goes back to the icon. So I can't use Facebook on my iPhone 4

    I am having a problem. When I click on the Mobile Facebook icon it acts like it is going to start opening then it quits and goes back to the icon. So I can't use Facebook on my iPhone 4

    Force quit the application and try again.
    Try a reset or restart of the device, trying the app after each step.

  • Selection Tool suddenly acting like Direct Selection Tool during click-and-drag

    Hello!
    This morning, my Selection Tool (the filled-in arrow) is acting like a Direct Selection Tool (the hollow arrow), but only when I click and drag on a group.  It ends up select the anchors within the group rather than the whole group itself.
    I've checked my preferences, quit and relaunched Illustrator a few times, and nothing seems to fix it.  The problem briefly went away a few minutes ago, but came back when I quit and relaunched for good measure.
    It's happening in Illustrator CS 5.1.  I have CS6, and that version works fine, but I'm waiting on some plugins to be updated for it, so I'd like to use CS 5.1.  I'm also running the latest Suitcase Fusion 4.
    Any input on this would be appreciated.  Thanks!

    Does the cursor appear correct, do you have any other keys depresessed?
    The CMD key will make selection tool into direct slection. What OS are you running, update as there was a modifier key problem through 10.6.5 - 10.6.7

  • My iphone 4 is acting like crap right i can't even restore it everytime i try to reboot it my iphone freezes at the apple logo it is ******* to the point where i just want to smash it how do i solve this problem please help?

    my iphone 4 is acting like crap right i can't even restore it everytime i try to reboot it my iphone freezes at the apple logo it is ******* me off to the point where i just want to smash it how do i solve this problem please help?

    Don't worry, just follow these steps to fix iPhone stuck on Apple logo
    => First of all Start your Computer and then connect with Internet connection, now Download the latest version of the iTunes application
    => Now install the iTunes application in your System and connect your Device with computer via Data cable
    => Now connect your Device with iTunes application and then Tab on summary option, see in the left side bar of iTunes
    => Now Select restore option from iTunes and then confirm the Restore Message for better results. After this unplug your Device and Restart it
    I hope that will surely
    Thank you...

  • Firefox running weird !! Whenever I click on something like bookmarks, it acts like it is going to open, blinks real fast, and then doesn't open.

    Whenever I click on something like bookmarks, it acts like it is going to open, blinks real fast, and then doesn't open.

    See [[Secure Connection Failed]]
    Create a new profile exclusively for the 4.0 beta version and create a desktop shortcut with -P "profile" appended to the target to launch that profile.
    * http://kb.mozillazine.org/Testing_pre-release_versions
    * http://kb.mozillazine.org/Creating_a_new_Firefox_profile_on_Windows
    * http://kb.mozillazine.org/Shortcut_to_a_specific_profile
    * http://kb.mozillazine.org/Using_multiple_profiles_-_Firefox

Maybe you are looking for

  • ICal crashes iTunes on iPod sync

    I bleed Apple juice. I trust Apple and own nothing but Apples. Still, I've come to the conclusion that iCal is just the worst program Apple has ever created and really shouldn't be offered until it's been fixed. I've written repeatedly about how slow

  • Error in scheduling a mapping with sqlloader ctl file

    Hi everyone, I have been trying to schedule a single mapping which generates sqlloader ctl file. but i get the error ORA-20001: Begin. initialize complete. workspace set. l_job_audit_execution_id= 20545. ORA-20001: Please check execution object is de

  • Purchase order longtext

    Hi All, How to fetch the purchase order longtext . Scenario when we enter material in MM03 and select the purchase order text view there we can find the long text . is there any table where this long text is getting updated or any specific procedure

  • Placing images on layers

    Hi not really sure what my question s is - or should be. I'm preparing av 1024*768 backgound for a\ web site On layer one I have a gradient in the clients colours I also have four images supplied by the client which I I modified by removing the backg

  • Problem in Payments

    Hi, I am running a Zprogram which gives Cleared Items Invoices for Vendor. Here we are using PAYR to retrieve Paymeents data based on LAUFD (Run Date). For particular vendor in PAYR we go to BSAK table for Invoices details. And when you rum FBL1N T.c