[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Usering MouseInputAdapter question

Posted on 2006-05-26
7
Medium Priority
?
314 Views
Last Modified: 2010-03-31
With the following code I want the string from JButton("Place on TextField.") to be placed in the textarea txt when the cursor is in the JButton's focus.  Does anyone know what I did incorrectly?

public class ButtonQ extends JApplet {
 
      private static final long serialVersionUID = 1L;
      private JButton bq = new JButton("Place on TextField.");
        private JTextField txt = new JTextField(10);

    MouseInputAdapter ma = new MouseInputAdapter() {
      public void mouseMoved(MouseEvent e) {
        String name = ((JButton)e.getSource()).getText();
        txt.setText(name);
      }
    };
   

  public void init() {
   bq.addMouseListener(ma);
    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    cp.add(bq);
    cp.add(txt);
  }
  public static void main(String[] args) {
    Console.run(new ButtonQ(), 350, 200);
  }
} ///end ButtonQ.java
0
Comment
Question by:kellyputty
  • 4
  • 3
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16773341
0
 

Author Comment

by:kellyputty
ID: 16773545
Thank you.
Now I think what I want to do may not be possible.  I want the button's label to go into the text area when the mouse cursor enters the button, no clicks involved.
Is that possible?
0
 
LVL 92

Expert Comment

by:objects
ID: 16773624
yes you'd use a mousemotionlistener for that, and set the text in mouseEntered()

http://java.sun.com/docs/books/tutorial/uiswing/events/mousemotionlistener.html
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:kellyputty
ID: 16773662
I implemented the following, and everything looks correct, however, when run it and enter the button--it gets focus,however, the text isn't transfered to the textarea.

I don't understand what I did wrong?


public class ButtonQ extends JApplet {
private static final long serialVersionUID = 1L;
private JButton bq = new JButton("Place on TextField.");
private JTextField txt = new JTextField(10);
   
   
    MouseMotionListener ma = new MouseMotionListener() {
      public void mouseEntered(MouseEvent e) {
        String name = ((JButton)e.getSource()).getText();
        txt.setText(name);
      }
      public void mouseMoved(MouseEvent e) {};
      public void mouseDragged(MouseEvent e) {};
    };
   

  public void init() {
   bq.addMouseMotionListener(ma);
    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    cp.add(bq);
    cp.add(txt);
  }
  public static void main(String[] args) {
    Console.run(new ButtonQ(), 350, 200);
  }
} ///end ButtonQ.java
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 16773706
Sorry, by bad. Should be a MouseListener, not a MouseMotionListener.
0
 

Author Comment

by:kellyputty
ID: 16773708
just figured that out. thanx
0
 

Author Comment

by:kellyputty
ID: 16773716
Here is the proper result for future inquiries:

public class ButtonQ extends JApplet {
 
      private static final long serialVersionUID = 1L;
      private JButton bq = new JButton("Place on TextField.");
    private JTextField txt = new JTextField(10);
   
   
    MouseListener ma = new MouseListener() {
     
      public void mouseEntered(MouseEvent e) {
        String name = ((JButton)e.getSource()).getText();
        txt.setText(name);
      }
      public void mouseClicked(MouseEvent e) {};
      public void mousePressed(MouseEvent e) {};  
      public void mouseExited(MouseEvent e) {};
      public void mouseReleased(MouseEvent e) {};
    };
   

  public void init() {
      bq.addMouseListener(ma);
    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    cp.add(bq);
    cp.add(txt);
  }
  public static void main(String[] args) {
    Console.run(new ButtonQ(), 350, 200);
  }
} ///end ButtonQ.java
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month18 days, 11 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question