Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Perform -> key press Event on GUI programmatically

Perform -> key press Event on GUI programmatically

Scheduled Pinned Locked Moved Java
questionjava
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Sachin k Rajput

    How can I perform arrow key event for a GUI in my Java Program? Thanks!

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    Use a KeyListener[^].

    Use the best guess

    T 1 Reply Last reply
    0
    • L Lost User

      Use a KeyListener[^].

      Use the best guess

      T Offline
      T Offline
      TorstenH
      wrote on last edited by
      #3

      +5 - This is how to get this done[^]

      regards Torsten When I'm not working

      L 1 Reply Last reply
      0
      • T TorstenH

        +5 - This is how to get this done[^]

        regards Torsten When I'm not working

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        Thanks, all I did was go to the 'K' section in the documentation index. :-\

        Use the best guess

        S 1 Reply Last reply
        0
        • L Lost User

          Thanks, all I did was go to the 'K' section in the documentation index. :-\

          Use the best guess

          S Offline
          S Offline
          Sachin k Rajput
          wrote on last edited by
          #5

          Thanks for your suggestions.. My problem is I want to change the item in a Jlist Programmatically. That's why I wanted to use this but Robot(AWT key Press Event)not working. My code is:

          listener = new ListSelectionListener() {
          @Override
          public void valueChanged(ListSelectionEvent e) {
          Object o = imageList.getSelectedValue();
          if (o instanceof BufferedImage) {
          imageView.setIcon(new ImageIcon((BufferedImage)o));

                      }
          

          }
          }
          imageList.addListSelectionListener(listener);

          Well the code is working fine but I want to change the next image using a button or something else How can I do this? Please help me in this?

          L S T 3 Replies Last reply
          0
          • S Sachin k Rajput

            Thanks for your suggestions.. My problem is I want to change the item in a Jlist Programmatically. That's why I wanted to use this but Robot(AWT key Press Event)not working. My code is:

            listener = new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
            Object o = imageList.getSelectedValue();
            if (o instanceof BufferedImage) {
            imageView.setIcon(new ImageIcon((BufferedImage)o));

                        }
            

            }
            }
            imageList.addListSelectionListener(listener);

            Well the code is working fine but I want to change the next image using a button or something else How can I do this? Please help me in this?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            Sorry, I don't understand the problem. Maybe you need to look at some of the GUI tutorials[^].

            Use the best guess

            1 Reply Last reply
            0
            • S Sachin k Rajput

              Thanks for your suggestions.. My problem is I want to change the item in a Jlist Programmatically. That's why I wanted to use this but Robot(AWT key Press Event)not working. My code is:

              listener = new ListSelectionListener() {
              @Override
              public void valueChanged(ListSelectionEvent e) {
              Object o = imageList.getSelectedValue();
              if (o instanceof BufferedImage) {
              imageView.setIcon(new ImageIcon((BufferedImage)o));

                          }
              

              }
              }
              imageList.addListSelectionListener(listener);

              Well the code is working fine but I want to change the next image using a button or something else How can I do this? Please help me in this?

              S Offline
              S Offline
              Shubhashish_Mandal
              wrote on last edited by
              #7

              As "Rechard" suggest, add KeyListener to your imageList and put your logic on KeyPress event to move the next item on the list on up/down arrow key.

              Regards Shubhashish

              1 Reply Last reply
              0
              • S Sachin k Rajput

                Thanks for your suggestions.. My problem is I want to change the item in a Jlist Programmatically. That's why I wanted to use this but Robot(AWT key Press Event)not working. My code is:

                listener = new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                Object o = imageList.getSelectedValue();
                if (o instanceof BufferedImage) {
                imageView.setIcon(new ImageIcon((BufferedImage)o));

                            }
                

                }
                }
                imageList.addListSelectionListener(listener);

                Well the code is working fine but I want to change the next image using a button or something else How can I do this? Please help me in this?

                T Offline
                T Offline
                TorstenH
                wrote on last edited by
                #8

                Read the tutorial. and maybe make a little "Area51"-project where you try and test things aside from normal coding.

                regards Torsten When I'm not working

                S 1 Reply Last reply
                0
                • T TorstenH

                  Read the tutorial. and maybe make a little "Area51"-project where you try and test things aside from normal coding.

                  regards Torsten When I'm not working

                  S Offline
                  S Offline
                  Sachin k Rajput
                  wrote on last edited by
                  #9

                  Thanks for your suggestions!!! If I'm not concerning in my project then the main task of mine is to change the item in a list on an event(Button or String Pass). ...I can do it on press of right key so I thought to read the right key programmatically. I think you can understand what I want to say.. Thanks!

                  L 1 Reply Last reply
                  0
                  • S Sachin k Rajput

                    Thanks for your suggestions!!! If I'm not concerning in my project then the main task of mine is to change the item in a list on an event(Button or String Pass). ...I can do it on press of right key so I thought to read the right key programmatically. I think you can understand what I want to say.. Thanks!

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #10

                    Indian Coder1989 wrote:

                    I thought to read the right key programmatically.

                    Yes, and you do this with a KeyListener, as we have said.

                    Use the best guess

                    1 Reply Last reply
                    0
                    • S Sachin k Rajput

                      How can I perform arrow key event for a GUI in my Java Program? Thanks!

                      M Offline
                      M Offline
                      MarlBermudoNights
                      wrote on last edited by
                      #11

                      KeyListener + passing the focus properly:

                      @Override
                      public void keyTyped(KeyEvent ke) {
                      if(ke.getKeyChar() == KeyEvent.VK_LEFT){
                      //do something
                      }else if(ke.getKeyChar() == KeyEvent.VK_RIGHT){
                      //do something else
                      }
                      }

                      @Override
                      public void keyPressed(KeyEvent e) {
                      keyTyped(e);
                      }

                      also by input mapping:

                      private void inputMap(){

                          this.getInputMap().put(KeyStroke.getKeyStroke(
                              KeyEvent.VK\_LEFT, 0), "left");
                          this.getActionMap().put("left", new AbstractAction() {
                              @Override
                              public void actionPerformed(ActionEvent ae) {
                                  //do something
                              }
                          });
                      }
                      
                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups