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 8 of 8

Thread: counting mouse button clicks

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default counting mouse button clicks

    This is my program so far:
    It complies an runs, but the textfield ends up being as big as the entire frame, and when I click the button, nothing happens.

    import java.awt.event.*;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
    public class ButtonStart extends Frame
    {
    private int mouseclicked=0;
    TextField objTextField;
     
    public static void main(String args[])
    {
    ButtonStart BS= new ButtonStart();
    }
     
    public ButtonStart()
    {
    Frame objFrame;
    Button objButton;
    TextField objTextField;
    objFrame=new Frame("Clicking Buttons");
    objButton= new Button("Click me!");
    objTextField= new TextField("0");
    objTextField.setBounds(90, 50, 100, 100);
    objButton.setBounds(10, 30, 60, 60);
     
     
    objFrame.addMouseListener(new MyMouseListener());
     
    objFrame.setSize(300,300);
    objFrame.setVisible(true);
    objFrame.add(objButton);
    objFrame.add(objTextField);
    objFrame.addWindowListener(new WindowAdapter()
     
    {
    public void windowClosing(WindowEvent we)
    {
    System.exit(0);
    }
    });
     
    }
     
    public class MyMouseListener extends MouseAdapter
    {
    public void mouseClicked(MouseEvent me)
    {
    int mouseclicked= me.getClickCount();
     
    objTextField.setText("Mouse clicked this many times:" + mouseclicked );
    }
     
    }
    }

    Anyone have any idea of how I could make this program work? Thanks!
    Last edited by KevinWorkman; June 6th, 2011 at 10:05 AM. Reason: added highlight tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: counting mouse button clicks

    When posting code, make sure you use the highlight tags to preserve formatting. I added them for you this time. Also, do you really not use any indentations?

    The problem with the click count is that you're misunderstanding what MouseEvent.getClickCount() returns. If you want to keep track of the total number of times the mouse has been clicked, you'll have to increment a variable yourself.

    As for your layout issues, here are some questions:
    Why are you using Frame instead of JFrame, and TextField instead of JTextField?
    Why are you extending Frame?
    What layout are you using?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: counting mouse button clicks

    You could just add 1 to an integer variable every time the mousePressed method is called.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: counting mouse button clicks

    Quote Originally Posted by Shazer2 View Post
    You could just add 1 to an integer variable every time the mousePressed method is called.
    That's a bit misleading, IMHO. Adding one to the integer he's using currently will simply be 1 + getClickCount(), which is usually very small. What he wants to do is increment a variable that has scope outside of the method.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: counting mouse button clicks

    when I click the button, nothing happens
    Another possible explanation for nothing happening is that you are NOT overriding the method. Since Adapters provide all the methods in the interface, if you mistype the method name, the compiler is happy to add a new method.
    Add an @Override statement before any methods you add to an Adapter to let the compiler check it for you

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: counting mouse button clicks

    Yeah, now that I'm looking at it a little more closely, you've got a few wonky things going on before you even get to your logic error.

    I really recommend switching to Swing instead of AWT, if only for the sanity of people who might answer your questions.

    I'd also recommend using a layout- setting the bounds explicitly is usually more trouble than its worth.

    Finally, you might want to make your requirements a little more exact- what exactly do you want to count? The number of times the user clicks the button? Or anywhere in the frame? Or something else?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: counting mouse button clicks

    use flowlayout to provide buttons size accordingly as frame uses border layout by default

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: counting mouse button clicks

    This post is over 3 years old. Please don't resurrect old threads like this. I'm closing this one.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How to get Mouse Position even if it is not within our application?
    By Freaky Chris in forum Java Programming Tutorials
    Replies: 2
    Last Post: January 4th, 2012, 10:57 AM
  2. MouseListener accumulating mouse clicks
    By mycallsevern in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 19th, 2011, 02:15 PM
  3. Mouse Listeners
    By newbie in forum AWT / Java Swing
    Replies: 2
    Last Post: November 27th, 2010, 11:08 PM
  4. Counting cells
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2010, 05:04 PM
  5. Move button with mouse pressed
    By Stavros in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 10th, 2010, 08:45 AM

Tags for this Thread