0

I've looked around and viewed various threads on here. the one I found closest to what I'm attempting to do is this one:

Hovering over JButtons and displaying a message

Basically I'm trying to replace button.setToolTipText(""); with an ImageIcon. I'm using this as a preview of the next page you're about to visit inside the JFrame, to give to user an Idea or quick overview of the next page. (I have figured out the imaging, just not the ToolTip).

Here is what I tried using based on what I've seen in the various thread, but obviously it didn't work, hence I'm asking this question.

Code I used: Log.setToolTip(new ImageIcon(getIconLog));

Community
  • 1
  • 1
Ryan
  • 359
  • 2
  • 8
  • 15

1 Answers1

1

You can use MouseListener and JLabel act as a tooltip. Like this...

public static void main(String[] args){
     final JFrame f = new JFrame();
     f.setSize(500, 400);
     f.setVisible(true);
     f.setLayout(null);
     f.setDefaultCloseOperation(3);

     final JButton b = new JButton("ToolTip");
     b.setBounds(100, 100, 70, 70);
     f.add(b);

     final JLabel toolTip = new JLabel();
     b.addMouseListener(new MouseListener() {

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {
            f.remove(toolTip);
            f.repaint();
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            try {
                toolTip.setIcon(new ImageIcon(ImageIO.read(new File("your image"))));
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            toolTip.setBounds(b.getLocation().x+b.getWidth(), b.getLocation().y-b.getHeight(), 100, 50);
            f.add(toolTip);
            f.repaint();
        }

        @Override
        public void mouseClicked(MouseEvent e) {

        }
    });

}
subash
  • 3,116
  • 3
  • 18
  • 22
  • Great answer. Don't need to do it like that (you answered with more than I needed). But I can pick pieces from that & you answered another question I was actually stuck on ++. – Ryan Dec 05 '13 at 00:09