Reputation: 384
I want to add a mouse listener to a text. Is it posiible? Thanks
BufferStrategy bs=this.getBufferStrategy();
if(bs==null){
createBufferStrategy(3);
return;
}
Graphics g=bs.getDrawGraphics();
g.fillRect(0, 0, 800, 400);
g.setFont(new Font("Verdana",0 ,50));
g.setColor(Color.WHITE);
g.drawString("Play", 600, 60);
Upvotes: 0
Views: 187
Reputation: 1053
As your Graphics
object does not know about the individual "items" on it, you can't add an listener to the text "Play" per se.
Approach one would be to not use one "big" Graphics
objects but to have an individual component for the "Play" text. You could attach a listener to this smaller component.
Another approach would be to add an listener to the "big" component and query the click event for the mouse coordinates.
Upvotes: 1
Reputation: 8357
you can try something like this.
final JTextField textField = new JTextField("Text goes here");
textField.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
//take some action here
}
});
Upvotes: 0