Reputation: 27
I expected this to be very simple and straightforward, but tooltiptext is not showing upon hovering the mouse over. I tried printing the text and it prints correctly. Any comments what I'm doing wrong?
public class gui2 extends JFrame {
private JLabel item1;
public gui2() {
super("The title bar");
setLayout(new FlowLayout());
item1 = new JLabel("label 1");
item1.setToolTipText("This is a message");
String str = item1.getToolTipText();
System.out.println(str);
add(item1);
}
class gui {
public static void main(String[] args) {
gui2 g2 = new gui2();
g2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
g2.setSize(400, 200);
g2.setVisible(true);
}
} }
Upvotes: 0
Views: 9027
Reputation: 36423
As mentioned by @restricteur your code does not compile.
This is due to the fact that your class gui
which holds the main(..)
is nested within another class, hence no static
declaration of method is allowed unless the nested class is marked static
. ( I simply moved/un-nested Gui
out from Gui2
)
Besides that your code does work, I think you are being hasty - hold the mouse over JLabel
for like 3-4 seconds and you should see the ToolTip
appear:
(using your code with no compilation error of course):
Suggestions on code:
1) Please watch java naming conventions i.e class names should begin with a capital letter and each new word thereafter should also i.e gui
becomes Gui
or GUI
but I prefer the former.
2) Dont call setSize
on JFrame
use and= appropriate LayoutManager
and call pack()
on JFrame
before setting it visible (but after components have been added).
3) Dont extend JFrame
unnecessarily simply create an instance and use that.
4) Always create and manipulate Swing Components on Event Dispatch Thread via SwingUtilities.invokeLater(Runnable r)
block.
5) Opt for setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
unless using Timer
s as this will allow main(..)
to continue regardless if GUI is exited.
Here is code with above fixes:
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
class Gui2 {
private JLabel item1;
private JFrame frame;
public Gui2() {
frame = new JFrame("The title bar");
frame.setLayout(new FlowLayout());
item1 = new JLabel("label 1");
item1.setToolTipText("This is a message");
String str = item1.getToolTipText();
System.out.println(str);
frame.add(item1);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
class Gui {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Gui2();
}
});
}
}
Upvotes: 2
Reputation: 312
Your code does not compile even if you add the imports. Here is your code corrected and working :
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Gui {
public static void main(String[] args) {
Window window = new Window();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 200);
window.setVisible(true);
}
}
class Window extends JFrame {
private static final long serialVersionUID = 1L;
private JLabel jlabel;
public Window() {
super("The title bar");
setLayout(new FlowLayout());
jlabel = new JLabel("label 1");
jlabel.setToolTipText("This is a message");
String str = jlabel.getToolTipText();
System.out.println(str);
add(jlabel);
}
}
Upvotes: 3