MousePressed/ MouseUp...

The partial code below runs

str="("+e.getX()+", "+e.getY()+")";

to show a message on the screen on mousePressed().

Question: How to code mouseUp() to make the message to disapear?

Thank you.
public MovableMessagePanel(String s){
	
            addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e){
                    
                    x=e.getX();
                    y=e.getY();  
                    str="("+e.getX()+", "+e.getY()+")";
                    repaint();
                }
                public void mouseUp(MouseEvent e){
                    
                    x=e.getX();
                    y=e.getY();  
//                    str="("+e.getX()+", "+e.getY()+")";
                    repaint();
                }                
            });

Open in new window

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
 
for_yanCommented:
Try this simple code - you press the mose on the window - it shows its position - you reklaese the mouse -
the position string disapperas


import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MousePressedUp extends JFrame implements MouseListener {
           MyMousePanel mmp;
    public MousePressedUp(){

         mmp = new MyMousePanel();
          mmp.addMouseListener(this);
        Container c = this.getContentPane();
        c.add(mmp);
        this.setSize(300,300);
        this.setVisible(true);

    }



  public void mouseClicked(MouseEvent me){}

      public void mousePressed(MouseEvent me){

                  mmp.setString(me.getPoint().getX() + "  "  +  me.getPoint().getY());
           repaint();
      }

      public void mouseReleased(MouseEvent me){
                mmp.setString("");
              repaint();


      }
      public void mouseEntered(MouseEvent me){}
      public void mouseExited(MouseEvent me){}

    public static void main(String[] args) {
        new MousePressedUp();
 }
}

class MyMousePanel extends JPanel {
    String s = "";

  public void setString(String s){
      this.s = s;
  }
    public void paint(Graphics g){

        g.drawString(s, 50,50 );

    }

    

}

Open in new window

0
 
CEHJCommented:
Do
str=null; //(Only paint if not empty or null)
repaint();

Open in new window

0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
How to know when Mouse is up to reset to str=" " ?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
imladrisCommented:
In general, in a GUI environment, one should not assume that what you put on the screen will persist. Your window can, for instance, be minimized or overlayed by another window. In addition the original designers of GUI systems wanted to allow for dynamic, real time, changes to windows.

So, the common idea is that there is central code, or a hierarchy of code, that is responsible for repainting the screen. This code can (and will) be called by the operating system at any time to "refresh" the image in the window. In Java this is even done directly and asynchronously, in a separate thread.

Therefore, in your case, the concept would be that the central paint method (or hierarchy) has access to a state variable (e.g. String mousepos;) that supplies the position to be displayed. Then when you get a Mouse Pressed event you set the mousepos String and force a repaint. For a Mouse Up event you set the mousepos String to null and force a repaint. The repaint will cause the paint code to be executed.
Meanwhile, in the paint code, you check if the string is null and, if it isn't, then you display it. Since a repaint normally starts by blanking the window this will cause the display to be cleared if the string is null, and to show (or change) the string if it is not.
0
 
CEHJCommented:
>>How to know when Mouse is up to reset to str=" " ?


It will be up when mouseUp is called
0
 
imladrisCommented:
You would set "mousepos" to null in the mouseUp listener method.
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Could you revise the submitted code?
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I am getting:

System.out.println("Down");

but not:

System.out.println("up");

in the code below.
public MovableMessagePanel(String s){
	
            addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e){
                    System.out.println("Down");
                    x=e.getX();
                    y=e.getY();  
                    str="("+e.getX()+", "+e.getY()+")";
                    repaint();
                }               
            });
            
            addMouseListener(new MouseAdapter(){
                public void mouseUp(MouseEvent e){
                    System.out.println("Up");
                    x=e.getX();
                    y=e.getY();  
//                    str="("+e.getX()+", "+e.getY()+")";
                    repaint();
                }                
            });

Open in new window

0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
re:> You would set "mousepos" to null in the mouseUp listener method.

How? Could you revise the code I have submitted above so it will do this?
0
 
CEHJCommented:
Try the following
import java.awt.*;
import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;


public class F extends JFrame {
    private void setGui() {
        try {
            setLocation(0, 100);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Container cp = getContentPane();
	    cp.add(new MovableMessagePanel("PAINT ME!"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        F f = new F();
                        f.setGui();
                        f.setSize(200, 200);
                        f.setVisible(true);
                    }
                });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class MovableMessagePanel extends JPanel {
        private int x;
        private int y;
        private String str;

        public MovableMessagePanel(String s) {
            addMouseListener(new MouseAdapter() {
                    public void mousePressed(MouseEvent e) {
                        System.out.println("Down");
                        x = e.getX();
                        y = e.getY();
                        str = "(" + e.getX() + ", " + e.getY() + ")";
                        repaint();
                    }
                });

            addMouseListener(new MouseAdapter() {
                    public void mouseUp(MouseEvent e) {
                        System.out.println("Up");
                        x = e.getX();
                        y = e.getY();
                        str = null;
                        repaint();
                    }
                });
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            if (str != null) {
                g.drawString(str, x, y);
            }
        }
    }
}

Open in new window

0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
CEHJ,

Thank you for the sample code.

System.out.println("Down"); is firing but
System.out.println("Up"); is not.

The same problem I had originally.
0
 
for_yanCommented:
Dont think there is method MouseUP
There is method mouseReleased()
0
 
for_yanCommented:
Try my code - it does what you want
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
0
 
CEHJCommented:
Sorry - that should have been
addMouseListener(new MouseAdapter() {
 56                     public void mouseReleased(MouseEvent e) {
 57
 58                         System.out.println("Up");
 59                         x = e.getX();
 60                         y = e.getY();
 61                         str = null;
 62                         repaint();
 63                     }
 64                 });

Open in new window

0
 
CEHJCommented:
Copy/paste error. That's why you should use @Override (following won't compile)
@Override
                    public void mouseUp(MouseEvent e) {

                        System.out.println("Up");
                        x = e.getX();
                        y = e.getY();
                        str = null;
                        repaint();
                    }
                });

Open in new window

0
 
CEHJCommented:
As for the accepted code:

a. in Swing, you should override paintComponent, not paint
b. don't make an unnecessary drawString call if the String doesn't need to be painted
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.