Ruchir Baronia Ruchir Baronia - 3 months ago 8
Java Question

Tool tip in gui not displaying?

I am using the .setToolTipText component so that the text "This will show up when you hover over it" shows up when the mouse is hovered over the window. Currently, everything else is working, except for the tooltip. Right now the tool tip only displays when hovered above the words, how do I make it display whenever I hover over the window itself? This is the code:

Here is the main class:

import javax.swing.JFrame;

class evenmoregui{
public static void main (String[ ] args){

moregui guiobject = new moregui();
guiobject.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Makes the program terminate when the x is clicked
guiobject.setSize(275,180); //This is the size
guiobject.setVisible(true); //Means it should show up, actually puts the window on screen
}


}


Here is the class with the actual .setToolTip part:

import java.awt.FlowLayout; //Make it so the windows don't overlap, it just formats the windows properly
import javax.swing.JFrame; //Gives you all the windows features in the window, like minimize
import javax.swing.JLabel; //Allows text and simple images on window

public class moregui extends JFrame { //JFrame gives you all the basic windows features.
private JLabel item1;

public moregui(){ //Constructor
super("This is the title bar"); //These are all imported methods


setLayout(new FlowLayout()); //Gives default layout


item1 = new JLabel("This is a sentence"); //Text on the screen


item1.setToolTipText("This will show up when you hover over it");


add(item1); //Adds item1 into window
}


}

Answer

This is a correct code. Try hovering over it longer.

JPanel panel = new JPanel();
        panel.setToolTipText("ToolTIp");
        contentPane.add(panel, BorderLayout.NORTH);

        JLabel lblLabel = new JLabel("Label");
        panel.add(lblLabel);

Let me know if you ran into any more problems.