java - How to detect if a cursor is over a textfield of a button and then move it to the next one? -


i need able detect whether or not mouse hovering on a button , when press enter needs move next button in grid , cycle way around button grid(the buttons in own grid layout).

the program 5000 lines of code, din't include here, if needs can send you.

enter image description here

you can either make use of this api or can add mouselistener along mousemotionlistener this:

import java.awt.*; import java.awt.event.*; import javax.swing.*;  public class texthovercomponent extends jpanel {     private static final long serialversionuid = 1;      private static final color default_text_color = color.white;      private static final color hover_text_color = color.red;      private font font = new font("arial", font.bold, 16);      private string text = "exit";      private color textcolor = default_text_color;      private point textlocation = new point(650, 50);      public texthovercomponent() {         addmouselistener(new mouseadapter() {             @override             public void mouseentered(mouseevent event) {                 checkforhover(event);             }              @override             public void mouseexited(mouseevent event) {                 checkforhover(event);             }         });          addmousemotionlistener(new mousemotionlistener() {             @override             public void mousemoved(mouseevent event) {                 checkforhover(event);             }              @override             public void mousedragged(mouseevent event) {                 checkforhover(event);             }         });     }      void checkforhover(mouseevent event) {         fontmetrics metrics = getfontmetrics(font);          graphics g = getgraphics();         rectangle textbounds = metrics.getstringbounds(text, g).getbounds();         g.dispose();          textbounds.translate(textlocation.x, textlocation.y);          if (textbounds.contains(event.getpoint())) {             textcolor = hover_text_color;         } else {             textcolor = default_text_color;         }         repaint(textbounds);     }      @override     protected void paintcomponent(graphics g) {         super.paintcomponent(g);          g.setfont(font);         g.setcolor(textcolor);         g.drawstring(text, textlocation.x, textlocation.y);     } } 

the answer other part of question don't know of now, try edit answer , put answer too

update: moving mouse position need use robot class. here example you:

import java.awt.awtexception;  import java.awt.dimension;  import java.awt.robot;  import java.awt.toolkit;  import java.awt.event.mouseevent;    public class main   {        public static void main(string[] args) throws awtexception, interruptedexception       {          robot robot = new robot();            dimension screensize = toolkit.getdefaulttoolkit().getscreensize();            robot.mousemove(0, (int) screensize.getheight()-20);            robot.mousepress(mouseevent.button1_mask);          robot.mouserelease(mouseevent.button1_mask);        }    } 

Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

python - RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 800 -