Results 1 to 6 of 6

Thread: Tooltip

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

    Default Tooltip

    I'm writing a program using swing applet.To the container i add the scrollPanle,to the i added panel above which i drawn my drawings...Now i want to add tooltip,when the mouse point goes over the drawing by showing its coordinates..Now i want to know how to add tooltip to the drawing?
    Please help me....

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    If you JPanel is mainPanel try it as follows.

    Java Code:
    mainPanel.setToolTipText("Just for testings those text");
    at the appropriate place of your code. Better to do it panel implementation.

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

    Default

    I tried in the way you said,but its not working out for me...Its not getting displayed....

    public class NewClass
    {
    public void init()
    {
    Container cont = getContentPane();
    cont.setLayout(new BorderLayout());
    setPreferredSize(new Dimension(300, 300));
    JScrollPane scroll = new JScrollPane();
    getContentPane().add(scroll, BorderLayout.CENTER);

    }
    private class Imagepanel extends JPanel implements MouseListener,MouseMotionListener
    {

    panel();
    addMouseMotionListener(this);
    addMouseListener(this);
    }
    public void panel()
    {
    StartDate obj = new StartDate();
    int w = obj.diff();
    int dimension = 0;
    int size = xmlreadingObj.getres_list().size();
    int height = 100 + size * 28;
    width = w;
    System.out.println("width"+width);

    setPreferredSize(new Dimension(width, height));
    setBorder(BorderFactory.createLineBorder(Color.BLA CK, border));
    l = new JLabel("HAI");
    add(l,BorderLayout.EAST);
    l.setVisible(true);
    l.setLocation(100,50);
    }

    protected void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
    RenderingHints.VALUE_ANTIALIAS_ON);
    g2.drawRect(100,200,15,20);
    g2.drawRect(200,200,15,20);

    }
    public void mousePressed(MouseEvent e)
    {
    Point p = e.getPoint();
    x_Pos = e.getX();
    y_Pos = e.getY();

    for(int j = 0; j < rects.length; j++)
    {
    if(rects[j].contains(p))
    {
    selectedindex = j;
    selectedRect = rects[j];
    dragging = true;
    break;
    }
    }
    }
    public void mouseMoved(MouseEvent e)
    {
    Imagepanel image = new Imagepanel();
    l = new JLabel();
    Point p = e.getPoint();
    if(rects[selectedindex].contains(p))
    {
    image.add(l);
    image.setToolTipText("Just to test");

    }
    else
    {
    candisplay = false;
    }
    }
    }

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    See how this simple widow works.

    Java Code:
    import java.awt.Dimension;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    /**
     *
     * @author Eranga Tennakoon
     */
    public class ToolTipDemo {
    
        JPanel pan = new JPanel();
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new ToolTipDemo().createAndShowGUI();
                }
            });
        }
        
        private void createAndShowGUI() {
            System.out.println("Created GUI on EDT? "+
                    SwingUtilities.isEventDispatchThread());        
            JFrame f = new JFrame("ToolTip Demo");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            pan.setToolTipText("This is a tool tip");
            pan.setPreferredSize(new Dimension(250, 250));
            f.add(pan);
            f.pack();
            f.setVisible(true);
        }
    
    }

Similar Threads

  1. Problem to ToolTip in Java 3d
    By roshithmca in forum AWT / Swing
    Replies: 1
    Last Post: 02-05-2008, 04:46 AM
  2. How to create ToolTip in Java 3d
    By roshithmca in forum AWT / Swing
    Replies: 0
    Last Post: 02-04-2008, 07:57 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
  •