Blackchart
Blackchart

Reputation: 133

JPanel how to add a ToolTipText

I have a little problem, I need to add a ToolTipText to JPanel. How should I do this? I want to have a tooltip when I have mouse over the circle. This is part of my code.

JPanel component1 = new JPanel();
JPanel component11 = new JPanel();
okno.add(component1,"align left,cell 0 0, h 75!, grow,wrap");


component1.setLayout(new MigLayout("","[][grow][grow]", "[grow]"));
component1.add((okno.add(creLab("Kraj", i, czcionka, etykietki))),"left align, cell 0 0");
component1.add(t1,"cell 1 0,grow");
//component1.add(new circle1(),"right align, cell 2 0,h 50!, w 53!, gapleft 50, wrap");
component1.add(component11," right align, cell 2 0, h 30!, gapleft 300, wrap");
component11.setLayout(new MigLayout("","[]","[]"));
component11.add(new circle1(),"cell 0 0,h 50!, w 50!, dock north");
component11.setToolTipText("<html>W polu obok wpisz kraj pochodzenia towaru</html>");

I add also code of circle1:

class circle1 extends Applet{
    public void paint(Graphics g){
        setForeground(Color.yellow);
        g.drawOval(0, 0, 50, 50);
        g.fillOval(0, 0, 50, 50);
        g.setColor(Color.black);
        g.drawString("Jak", 14, 14);
        g.drawString("wpisac", 3, 28);
        g.setColor(Color.red);
        g.drawString("kraj?", 14, 42);


        //g.drawString(arg0, arg1, arg2)
    }
}

Upvotes: 2

Views: 2846

Answers (3)

camickr
camickr

Reputation: 324197

See Playing With Shapes. You can create a JLabel with a ShapeIcon. Then you just use the setToolTipText() method of the JLabel. You can then add the label to the panel like any other component.

Now that you can use a component to represent a Shape there is no need to do custom painting. Just create a panel add add components to the panel. You can also create JLabels for all your text strings.

Don't do custom painting, unless you have a good reason to do so.

Upvotes: 1

fGo
fGo

Reputation: 1146

The first thing is to identify when the mouse is inside the circle. To do that you could verify the mouse position on a mouseMotionlister according to the circle area

http://www.java2s.com/Code/JavaAPI/javax.swing/JPaneladdMouseMotionListenerMouseMotionListenerlis.htm

Once you identify this situation you could proceed to change the tooltip

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347334

Take a look at JComponent#getToolTipText(MouseEvent)

This will allow you to determine what text to return based on the location of the mouse.

It's difficult to determine for your code snippet, exactly where the circle is been drawen, but I would avoid drawing directly to the surface of the applet, but instead use a custom component (like a JPanel) instead (overriding its paintComponent method). This I would then either add to the applet or to the control panel.

This way your going to avoid issues with the mouse events been consumed

I would also take a look at Ellipse2D, which can be used to determine if the ellipse contains a given point

Upvotes: 2

Related Questions