Results 1 to 4 of 4
  1. #1
    shawleigh17 is offline Member
    Join Date
    Jul 2009
    Posts
    9
    Rep Power
    0

    Default Java Popup .hide() method is leaving a grey box in the shape of the JTextArea I use.

    Hi, I am trying to have a comment box show up when I move into an area on a canvas, and then hide when I move out of this area. I have a short piece of code that kind of shows the problem I am having... In the example, the popup is having problems with the hiding. In my version (which is much longer and is reliant on 5 or more other long classes ), I can get the popup to show up where I want it to, and I can get all of the text to hide, but I can't get the little gray box to go away. If I use a timer, though, I have no problems. Does any one know why this is happening?

    Here is the executable code:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.MouseInputAdapter;
    
    public class newClass extends JPanel {
        Rectangle rect = new Rectangle(100,100,150,75);
        MouseoverDetails comments;
        JTextArea popupLabel;
        
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setPaint(Color.blue);
            g2.draw(rect);
        }
    
        public void setRect(int x, int y) {
            rect.setLocation(x, y);
            repaint();
        }
    
        public static void main(String[] args) {
            newClass test = new newClass();
            new GraphicDragController(test);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.setSize(400,400);
            f.setLocation(100,100);
            f.setVisible(true);
        }
    }
    
    class GraphicDragController extends MouseInputAdapter {
        newClass component;
        Point offset = new Point();
        boolean dragging = false;
    
        public GraphicDragController(newClass gdad) {
            component = gdad;
            component.addMouseListener(this);
            component.addMouseMotionListener(this);
        }
    
        public void mousePressed(MouseEvent e) {
            Point p = e.getPoint();
            Rectangle r = component.rect;
            if(r.contains(p)) {
                offset.x = p.x - r.x;
                offset.y = p.y - r.y;
                dragging = true;
            }
        }
    
    
        public void mouseReleased(MouseEvent e) {
            dragging = false;
        }
        
        public void mouseMoved(MouseEvent e) 
        {
        	boolean inside = false;
       
        	if(component.rect.contains(e.getPoint()))
        	{
    		  inside = true;
    		  component.popupLabel = new JTextArea("Test");
    		  component.comments = new MouseoverDetails(component,e.getPoint(), component.popupLabel);
    	    }
       
     	   if (inside == false)
     	   {	   
     		   try{
     			   component.comments.popup.hide();
     		   }
     		   catch(Exception ex){}
     	   }	   
        }
    
        public void mouseDragged(MouseEvent e) {
            if(dragging) {
                int x = e.getX() - offset.x;
                int y = e.getY() - offset.y;
                component.setRect(x, y);
            }
        }
    }
    
    class MouseoverDetails 
    {
    	PopupFactory factory;
    	Popup popup;
    	JTextArea popupLabel; 
    	
    	public MouseoverDetails(newClass test, Point p, JTextArea popupLabel)
    	{
    		factory = PopupFactory.getSharedInstance();
    		popup = factory.getPopup(test, popupLabel, test.rect.x+30, test.rect.y - 30);
    		popup.show();
    	}	 	
    }
    and here are the code segments that I am using in my code:
    This is the mouseMoved method
    Java Code:
    public void mouseMoved(MouseEvent e)
       {
    	   boolean insidePallet = false;
    	   for(int i=0;i<usedPalletsList.size();++i)
    	   {
    		  PalletType pt = usedPalletsList.get(i);
    		  
    		  if(pt.contains(e.getPoint()))
    		  {
    			  insidePallet = true;
    			  comments = new MouseoverDetails(usedPalletsList, e.getPoint(), component, component.addPM.popupLbl);
    		  }
    	   }
    	   
    	   if (insidePallet == false)
    	   {	   
    		   try{
    			   comments.popup.hide();
    		   }
    		   catch(Exception ex){}
    		   
    	   }	   
       }

    And this is part of the class where I implement the popup.
    Java Code:
    if(pt.GetType(pt).equals("SinglePallet")&& pt.contains(p) && !r.contains(p) && !pt.text.equals("xxx"))
    			{
    			    popupLabel.setText("Location: " + currentLocation + "\nDescription: " + skuDescp + "\nVelocity: " + 
    					   z + " Pallets Per Week" + "\nBulk Percent: " + bpD 
    					   + "%\nOther Percent: " + opD + "%\nBrand: " + brand + 
    					   "\nSize: " + group + "\nPlanogram: " + planogram);
    			     
    			     factory = PopupFactory.getSharedInstance();	
    			     popX = pt.x +30;
    			     popY = pt.y;
    			     popup = factory.getPopup(dndTest, popupLabel, popX, popY);
    		    	 popup.show();
    		}

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Just a quick thought. When you hide the text box, the component under it may not be aware that it needs to repaint itself. After you hide the text box, try revalidate() on the component below...

  3. #3
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    There are a number of problems in the sample program.

    - Every time the mouse moves while it is inside rect, a new popup is popped up.
    All these popups are put in the same place, so there multiplicity is not apparent
    - hide() is only called when the mouse is outside the rectangle
    and then it only hides the last of the popups that were created
    - the popups are placed relative to the screen origin
    and not relative to the window or the rect

    The sample program does not demonstrate the alleged problem in the full code.

    (and revalidate() is concerned with screen placement, so I don't think it will help with repaint problems)

    Please try again.

  4. #4
    shawleigh17 is offline Member
    Join Date
    Jul 2009
    Posts
    9
    Rep Power
    0

Similar Threads

  1. how to clear the session when leaving a page.
    By shivakumari in forum Java Servlet
    Replies: 3
    Last Post: 03-25-2009, 06:25 AM
  2. Grey Box
    By mja229 in forum New To Java
    Replies: 2
    Last Post: 12-24-2008, 07:24 PM
  3. how we get popup window in java
    By baserohit in forum Advanced Java
    Replies: 1
    Last Post: 03-22-2008, 04:39 AM
  4. java : how to hide application icon from a task bar
    By yogeshagashe in forum Advanced Java
    Replies: 0
    Last Post: 03-12-2008, 01:05 PM
  5. Popup in Java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •