Results 1 to 5 of 5
Thread: Tooltip not getting displayed...
- 07-22-2008, 01:16 PM #1
Member
- Join Date
- Jan 2008
- Posts
- 83
- Rep Power
- 0
Tooltip not getting displayed...
I have want to add tooltip went the mouse moves over the rectangle.so,I added a label to a panel and the panel to another panel.(I didn't use windows/JFrame,since I have to bring this applet in the HTML.)when the mouse comes over the rectangle,panel should visible or else it should not.But now,for me the tooltip is not displaying when the mouse moves over the rectangle.
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package javaapplication2; import java.awt.event.MouseEvent; import javax.swing.JApplet; import java.awt.*; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.*; import java.util.*; /** * * @author 8563 */ public class CheckApplet extends JApplet { static JPanel toolTip ; @Override public void init() { Container con = getContentPane(); con.setLayout(new BorderLayout()); setPreferredSize(new Dimension(300, 300)); JScrollPane scroll = new JScrollPane(); getContentPane().add(scroll, BorderLayout.CENTER); //JFrame f = new JFrame(); scroll.setViewportView(new ImagePanel()); } private static class ImagePanel extends JPanel implements MouseListener,MouseMotionListener{ ImagePanel image; Rectangle rext = new Rectangle(40, 60, 30, 15); int[] x = {75, 95, 115, 130}; int[] width = {15, 15, 15, 15}; boolean selected1 = false; int count1; Rectangle [] rects; Rectangle [] rectangles; ArrayList<Rectangle> dlprect_list; Rectangle [] childRects ; ArrayList <Rectangle> Rects = new ArrayList <Rectangle>(); boolean canDraw = false; int child_count ; boolean [] visible ; ArrayList child = new ArrayList(); boolean selected2 = false; ArrayList<String> dlp; String HLP; ArrayList <Integer> countList ; Integer [] childList ; boolean canRedraw = false; ArrayList <Integer>list ; JLabel label; public ImagePanel() { prepareData(); addMouseListener(this); addMouseMotionListener(this); toolTip = new JPanel(); label = new JLabel(); //this.add(toolTip); toolTip.setPreferredSize(new Dimension(50,50)); label.setHorizontalAlignment(JLabel.CENTER); label.setOpaque(true); label.setBackground(UIManager.getColor("ToolTip.background")); label.setBorder(UIManager.getBorder("ToolTip.border")); toolTip.add(label); setOpaque(true); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.draw(rext); drawRect(g2); } public void prepareData() { int x = 75; int y = 85; int w = 20; int h = 15; TestReadXML obj = new TestReadXML(); obj.parse(); dlp = obj.getDlpDescr(); dlprect_list = new ArrayList<Rectangle>(); HLP = obj.hlp; countList = obj.dlp_activity; childList = countList.toArray(new Integer[countList.size()]); for (int i = 0; i < dlp.size(); i++) { dlprect_list.add(new Rectangle(x, y, w, h)); y += 25; } rectangles = dlprect_list.toArray(new Rectangle[13]); visible = new boolean[dlprect_list.size()]; } public void drawRect(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; for (Rectangle r : rectangles) { g2.draw(r); repaint(); } } public void mouseClicked(MouseEvent e){} public void mousePressed(MouseEvent e) { Point pres_point = e.getPoint(); //visible = new boolean[hlp.size()]; for (int i=0;i<rectangles.length;i++) { if(rectangles[i].contains(pres_point)) { selected1 = true; selected2=false; //visibilty(i); visible[i] = true; count1 = i; break; } } } public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e) {} public boolean isToolTipShowing() { return toolTip.isShowing(); } public void showToolTip(int index, Point p) { //mlabel.setPreferredSize(new Dimension(50,50)); label.setText("TOOLTIP"); //toolTip.pack(); toolTip.setLocation(p); toolTip.setVisible(true); } public void hideToolTip() { toolTip.setVisible(false); //toolTip.dispose(); } public void mouseDragged(MouseEvent e) {} public void mouseMoved(MouseEvent e) { Point p = e.getPoint(); boolean traversing = false; Rectangle[] r = dlprect_list.toArray(new Rectangle[dlprect_list.size()]); for(int j = 0; j < r.length; j++) { Rectangle r1 = r[j]; if(r1.contains(p)) { if(isToolTipShowing()) { SwingUtilities.convertPointToScreen(p, this); showToolTip(j, p); } traversing = true; break; } } if(!traversing && isToolTipShowing()) hideToolTip(); } } }
- 07-22-2008, 09:58 PM #2
I am unable to compile your applet because of a missing class: TestReadXML.
It looks complicated so I'll wait.
- 07-23-2008, 12:06 PM #3
Member
- Join Date
- Jan 2008
- Posts
- 83
- Rep Power
- 0
I have just drawn a rectangle and want to display tooltip for the applet(for this applet structure,which i want to show in html,so i dnt use main class or jframe)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Tree;
import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JApplet;
import javax.swing.JPanel;
import java.util.*;
/**
*
* @author 8563
*/
public class Tooltip extends JApplet {
/**
* Initialization method that will be called after the applet is loaded
* into the browser.
*/
@Override
public void init()
{
Container con = getContentPane();
con.setLayout(new BorderLayout());
setPreferredSize(new Dimension(300, 100));
JScrollPane scroll = new JScrollPane();
getContentPane().add(scroll, BorderLayout.CENTER);
setBackground(Color.WHITE);
scroll.setViewportView(new ImagePanel());
}
private class ImagePanel extends JPanel implements MouseListener,MouseMotionListener
{
ArrayList<Rectangle> rect_list;
ArrayList <String> rect_descr;
JPanel toolTip;
JLabel label;
public ImagePanel()
{
prepareData();
addMouseListener(this);
addMouseMotionListener(this);
toolTip = new JPanel();
label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
label.setOpaque(true);
label.setBackground(UIManager.getColor("ToolTip.ba ckground"));
label.setBorder(UIManager.getBorder("ToolTip.borde r"));
toolTip.setPreferredSize(new Dimension(50,50));
//toolTip.add(label);
//setOpaque(true);
add(toolTip);
}
public void prepareData()
{
int x=40;
int y=50;
int w=15;
int ht=10;
rect_list = new ArrayList<Rectangle>();
rect_descr = new ArrayList <String> ();
for(int i=0;i<5;i++)
{
rect_list.add(new Rectangle(x,y,w,ht));
y +=20;
x+=25;
}
for(int j=0;j<10;j++)
{
rect_descr.add("Base"+j);
}
}
@Override
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
for(Rectangle r :rect_list)
{
g2.draw(r);
}
}
public void setTooltip(int index,Point p)
{
label.setText(rect_descr.get(index));
toolTip.setLocation(p);
toolTip.setVisible(true);
}
public boolean isToolTipShowing()
{
return toolTip.isVisible();
}
@Override
public Dimension getPreferredSize()
{
return new Dimension(500,500);
}
public void hideToolTip()
{
toolTip.setVisible(false);
}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e)
{
Point p = e.getPoint();
boolean traversing = false;
Rectangle[] r = rect_list.toArray(new Rectangle[rect_list.size()]);
for(int j = 0; j < r.length; j++)
{
Rectangle r1 = r[j];
if(r1.contains(p))
{
if(isToolTipShowing())
{
SwingUtilities.convertPointToScreen(p, this);
setTooltip(j, p);
}
traversing = true;
break;
}
}
if(!traversing && isToolTipShowing())
hideToolTip();
}
}
}
- 07-23-2008, 05:38 PM #4
Two problems:
1 — this test
in mousePressed was mising the not (!) operator.Java Code:if(!isToolTipShowing())
2 — your ImagePanel had artifacts because the background was not filled for each paint cycle.
An easy way to fill in the background is by adding a call to super:
Java Code:protected void paintComponent(Graphics g) { super.paintComponent(g);Java Code:// <applet code="TooltipRx" width="400" height="400"></applet> import java.awt.*; import javax.swing.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.JApplet; import javax.swing.JPanel; import java.util.*; public class TooltipRx extends JApplet { @Override public void init() { Container con = getContentPane(); con.setLayout(new BorderLayout()); resize(new Dimension(300, 100)); JScrollPane scroll = new JScrollPane(); getContentPane().add(scroll, BorderLayout.CENTER); // setBackground(Color.WHITE); scroll.setViewportView(new ImagePanel()); } private class ImagePanel extends JPanel implements MouseListener, MouseMotionListener { ArrayList<Rectangle> rect_list; ArrayList <String> rect_descr; JPanel toolTip; JLabel label; public ImagePanel() { prepareData(); addMouseListener(this); addMouseMotionListener(this); label = new JLabel(); label.setHorizontalAlignment(JLabel.CENTER); label.setBorder(UIManager.getBorder("ToolTip.border")); toolTip = new JPanel(new BorderLayout()); toolTip.setOpaque(true); toolTip.setBackground(UIManager.getColor("ToolTip.background")); toolTip.add(label); setLayout(null); add(toolTip); } public void prepareData() { int x=40; int y=50; int w=15; int ht=10; rect_list = new ArrayList<Rectangle>(); rect_descr = new ArrayList <String> (); for(int i=0;i<5;i++) { rect_list.add(new Rectangle(x,y,w,ht)); y +=20; x+=25; } for(int j=0;j<10;j++) { rect_descr.add("Base"+j); } } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; for(Rectangle r : rect_list) { g2.draw(r); } } public void setTooltip(int index, Point p) { label.setText(rect_descr.get(index)); Dimension d = label.getPreferredSize(); toolTip.setBounds(p.x, p.y, d.width, d.height); toolTip.setVisible(true); toolTip.repaint(); } public boolean isToolTipShowing() { return toolTip.isVisible(); } @Override public Dimension getPreferredSize() { return new Dimension(500,500); } public void hideToolTip() { toolTip.setVisible(false); toolTip.repaint(); } public void mouseClicked(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseDragged(MouseEvent e) {} public void mouseMoved(MouseEvent e) { Point p = e.getPoint(); boolean traversing = false; Rectangle[] r = rect_list.toArray(new Rectangle[rect_list.size()]); for(int j = 0; j < r.length; j++) { Rectangle r1 = r[j]; if(r1.contains(p)) { if(!isToolTipShowing()) { // Offset toolTip from r1. p.x = r1.x + r1.width + 10; p.y = r1.y - 5; setTooltip(j, p); } traversing = true; break; } } if(!traversing && isToolTipShowing()) hideToolTip(); } } }
- 07-31-2008, 10:00 AM #5
Member
- Join Date
- Jul 2008
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
How to dispose tooltip.
By Preethi in forum New To JavaReplies: 1Last Post: 07-22-2008, 07:06 AM -
Tooltip
By Preethi in forum New To JavaReplies: 5Last Post: 06-11-2008, 11:37 AM -
displayed the rownumbers
By geeta_ravikanti in forum JDBCReplies: 1Last Post: 04-22-2008, 02:30 AM -
Problem to ToolTip in Java 3d
By roshithmca in forum AWT / SwingReplies: 1Last Post: 02-05-2008, 03:46 AM -
Java Components are not displayed(sometimes)
By archanajathan in forum AWT / SwingReplies: 3Last Post: 11-05-2007, 08:34 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks