Results 1 to 5 of 5
  1. #1
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

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

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I am unable to compile your applet because of a missing class: TestReadXML.
    It looks complicated so I'll wait.

  3. #3
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

    Default

    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();

    }
    }
    }

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Two problems:
    1 this test
    Java Code:
    if(!isToolTipShowing())
    in mousePressed was mising the not (!) operator.

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

  5. #5
    suman is offline Member
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

Similar Threads

  1. How to dispose tooltip.
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 07-22-2008, 07:06 AM
  2. Tooltip
    By Preethi in forum New To Java
    Replies: 5
    Last Post: 06-11-2008, 11:37 AM
  3. displayed the rownumbers
    By geeta_ravikanti in forum JDBC
    Replies: 1
    Last Post: 04-22-2008, 02:30 AM
  4. Problem to ToolTip in Java 3d
    By roshithmca in forum AWT / Swing
    Replies: 1
    Last Post: 02-05-2008, 03:46 AM
  5. Java Components are not displayed(sometimes)
    By archanajathan in forum AWT / Swing
    Replies: 3
    Last Post: 11-05-2007, 08:34 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
  •