• We just launched and are currently in beta. Join us as we build and grow the community.

Focus when Mouse Hover in Java

TRAPPIN

Cloud Misconfiguration Auditor
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
60
Likes
128
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
This tutorial will teach you how to create a program that when hovering a mouse it will focus on the button using java.

So, now let's start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of focusSample.java.

2. Import the following package library:
  1. import

    java.awt.*

    ;

    //used to access a Component and GridLayout class
  2. import

    java.awt.event.*

    ;

    //used to access MouseAdapter,MouseEvent, and MouseListener class
  3. import

    javax.swing.*

    ;

    //used to access JButton and JFrame class

3. Create a class name MouseHover that will extend a MouseAdapter to access a component so that it can request focus. We will use the JComponent class so that it will call the component to be focused.
  1. class

    MouseHover extends

    MouseAdapter

    {
  2. public

    void

    mouseEntered(

    MouseEvent

    mouseEvent)

    {
  3. Component

    component =

    mouseEvent.getComponent

    (

    )

    ;
  4. if

    (

    !

    component.hasFocus

    (

    )

    )

    {
  5. component.requestFocusInWindow

    (

    )

    ;
  6. }
  7. }
  8. }

4. We will initialize variables in our Main, variable frame as JFrame, and mouseListener as the MouseHover class that we have declared above.
  1. JFrame

    frame =

    new

    JFrame

    (

    "Focus Sample"

    )

    ;
  2. MouseListener

    mouseListener =

    new

    MouseHover(

    )

    ;

Now, we will create 6 buttons and will apply the Focus that we have declared the MouseHover class. We will use the setFocusable method of the buttons so that it can be focused.
  1. for

    (

    int

    i =

    1

    ;

    i <=

    6

    ;

    i++

    )

    {
  2. JButton

    button =

    new

    JButton

    (

    Integer

    .toString

    (

    i)

    )

    ;
  3. button.addMouseListener

    (

    mouseListener)

    ;
  4. button.setFocusable

    (

    true

    )

    ;
  5. frame.getContentPane

    (

    )

    .add

    (

    button)

    ;
  6. }

5. Lastly, set the size and visibility to true, and close its operation. Have this code below:
  1. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  2. frame.getContentPane

    (

    )

    .setLayout

    (

    new

    GridLayout

    (

    2

    , 3

    )

    )

    ;
  3. frame.setSize

    (

    300

    , 200

    )

    ;
  4. frame.setVisible

    (

    true

    )

    ;


Output:

focussample.png


Here's the full code of this tutorial:

  1. import

    java.awt.*

    ;

    //used to access a Component and GridLayout class
  2. import

    java.awt.event.*

    ;

    //used to access MouseAdapter,MouseEvent, and MouseListener class
  3. import

    javax.swing.*

    ;

    //used to access JButton and JFrame class


  4. class

    MouseHover extends

    MouseAdapter

    {
  5. public

    void

    mouseEntered(

    MouseEvent

    mouseEvent)

    {
  6. Component

    component =

    mouseEvent.getComponent

    (

    )

    ;
  7. if

    (

    !

    component.hasFocus

    (

    )

    )

    {
  8. component.requestFocusInWindow

    (

    )

    ;
  9. }
  10. }
  11. }
  12. public

    class

    focusSample{
  13. public

    static

    void

    main(

    String

    args[

    ]

    )

    {
  14. JFrame

    frame =

    new

    JFrame

    (

    "Focus Sample"

    )

    ;
  15. MouseListener

    mouseListener =

    new

    MouseHover(

    )

    ;

  16. for

    (

    int

    i =

    1

    ;

    i <=

    6

    ;

    i++

    )

    {
  17. JButton

    button =

    new

    JButton

    (

    Integer

    .toString

    (

    i)

    )

    ;
  18. button.addMouseListener

    (

    mouseListener)

    ;
  19. button.setFocusable

    (

    true

    )

    ;
  20. frame.getContentPane

    (

    )

    .add

    (

    button)

    ;
  21. }


  22. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  23. frame.getContentPane

    (

    )

    .setLayout

    (

    new

    GridLayout

    (

    2

    , 3

    )

    )

    ;
  24. frame.setSize

    (

    300

    , 200

    )

    ;
  25. frame.setVisible

    (

    true

    )

    ;
  26. }
  27. }

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

440,010

316,559

316,568

Top