Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Set JButton's mouse events to JTextField

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Set JButton's mouse events to JTextField

    So, i have a JButton and a JTextField, and i need a way to connect both elements mouse events and make them act like JButton's mouse events. I.e., if i click on text field, button is clicked too, if mouse enters text field, then button acts like mouse entered it too, if mouse releases text field, then button acts like mouse released it too.
    Now, what ways there is ? Is there something like button.setMouseState(entered), button.setMouseStateEntered(), or some other way to force mouse state for elements ? That way i could just set text field's mouse events to change button's mouse states.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: Set JButton's mouse events to JTextField

    Add a MouseListener to the appropriate Component, and for each type of event you wish to capture, dispatch a new event to the other component. As an example, below would be a partial implementation to dispatch the mouse entered event from the listening component to the other component (button).
    @Override
    public void mouseEntered(MouseEvent e){
       button.dispatchEvent(new MouseEvent (button, MouseEvent.MOUSE_ENTERED, System.currentTimeMillis(), e.getModifiers(), 1, 1, e.getClickCount(), false);//1, 1 is just a pseudo position, could be more complex depending upon needs
    }

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Set JButton's mouse events to JTextField

    Thanks, i wrote mouseEntered(), mouseExited(), mousePressed(), mouseReleased(), mouseClicked() methods and it works perfectly.

Similar Threads

  1. Replies: 8
    Last Post: April 21st, 2013, 08:20 AM
  2. Replies: 4
    Last Post: April 6th, 2013, 05:13 PM
  3. JButton events
    By pottsiex5 in forum AWT / Java Swing
    Replies: 9
    Last Post: September 27th, 2011, 12:57 PM
  4. Using jbutton to write to jtextfield values to database
    By Sociopath in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 23rd, 2011, 10:53 PM
  5. [SOLVED] how can i resize JTextField, JButton using GridBagLayout,... TNX
    By sny in forum AWT / Java Swing
    Replies: 0
    Last Post: March 4th, 2010, 08:57 AM