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:
|
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
|
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.
|
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();
} |