aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes tooltip and mouse event Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "tooltip and mouse event" Watch "tooltip and mouse event" New topic
Author

tooltip and mouse event

shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
This program takes, (double[] data = { 20.2, 12.7, 19.0, 35.75, 16.2, 48.9, 31.0, 10.2 } this as an input and calcutatin x & y co-ordintes..Can anyone modify this so,that it takes both X&Y as input


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.text.NumberFormat;
import javax.swing.*;
public class GraphicOption extends JPanel
{
double[] data = { 20.2, 12.7, 19.0, 35.75, 16.2, 48.9, 31.0, 10.2 };
final int PAD = 20;
final int PROX_DIST = 10;
Point[] dataLocs;
NumberFormat nf;
GraphicOption()
{
nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(1);
nf.setMaximumFractionDigits(2);
dataLocs = new Point[data.length];
for(int j = 0; j < dataLocs.length; j++)
dataLocs[j] = new Point();
}
/** * The JComponent method summary section for this method* explains how to do this. See api for the details. */
public String getToolTipText(MouseEvent e)
{
Point p = e.getPoint();
for(int j = 0; j < dataLocs.length; j++)
{
if(p.distance(dataLocs[j]) < PROX_DIST)
{
return nf.format(data[j]);
}
}
return null;
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
// Draw axes.
g2.drawLine(PAD, PAD, PAD, h-PAD);
g2.drawLine(PAD, h-PAD, w-PAD, h-PAD);
double xInc = (w - 2*PAD)/(data.length-1);
double scale = getScale(h);
// Origin location is (PAD, h-PAD).
// So y == zero at:
double y0 = h-PAD;
g2.setPaint(Color.red);
// Plot data.
for(int j = 0; j < data.length; j++)
{
double x = PAD + j*xInc;
double y = y0 - scale*data[j];
// Set location for each point.
dataLocs[j].setLocation(x, y);
g2.fill(new Ellipse2D.Double(x-1.5, y-1.5, 4, 4));
}
}
private double getScale(int h)
{
double max = -Double.MAX_VALUE;
for(int j = 0; j < data.length; j++)
{
if(data[j] > max)
max = data[j];
}
return (h - 2*PAD)/max;
}
public static void main(String[] args)
{
JFrame f = new JFrame();
GraphicOption graphicOption = new GraphicOption();
DataSurveyor surveyor = new DataSurveyor(graphicOption, f);
graphicOption.addMouseMotionListener(surveyor);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(graphicOption);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
class DataSurveyor extends MouseMotionAdapter
{
GraphicOption component;
JWindow toolTip; JLabel label;
Point loc;
NumberFormat nf;
DataSurveyor(GraphicOption go, JFrame f)
{
component = go;
label = new JLabel();
label.setBorder(UIManager.getBorder("ToolTip.border"));
toolTip = new JWindow(f);
toolTip.getContentPane().setBackground(UIManager.getColor("ToolTip.background"));
toolTip.getContentPane().add(label);
}
public void mouseMoved(MouseEvent e)
{
String text = component.getToolTipText(e);
boolean hover = false;
if(text != null)
{
if(loc == null)
{
Point p = e.getPoint();
SwingUtilities.convertPointToScreen(p, component);
label.setText(text);
int height = label.getPreferredSize().height;
loc = new Point(p.x, p.y-height);
toolTip.setLocation(loc);
toolTip.pack();
toolTip.setVisible(true);
}
hover = true;
}
if(!hover && toolTip.isVisible())
{
toolTip.dispose();
loc = null;
}
}
}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
why don't you ask the author of the code:
http://www.coderanch.com/t/345294/GUI/java/mouse-event

multiple posts of the same problem (generally) confuses/annoys those with a solution
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: tooltip and mouse event