• Post Reply Bookmark Topic Watch Topic
  • New Topic

.doClick(); but for a JLabel?

 
Niklas Karlsson
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I use .doClick() "a lot", for Jbutton
but for a JLabel, what do i use??
and is is something to see if the label has a boarder (i set a boarder if i click the Jlabel).?

or do I use a Robot, and if so how can I make it to move the mouse to that position, from the current possition?

Best Regards! :-)
 
Campbell Ritchie
Marshal
Posts: 52516
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niklas Karlsson wrote:. . .
I use .doClick() "a lot", for Jbutton
. . .
Why not use a high‑level action, with an action listener?
Buttons are intended to be clicked, but panels aren't. You can add a mouse listener to a panel, but why? Unless you have some specific action related to the panel, why would you want to click it?
 
Niklas Karlsson
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Niklas Karlsson wrote:. . .
I use .doClick() "a lot", for Jbutton
. . .
Why not use a high‑level action, with an action listener?
Buttons are intended to be clicked, but panels aren't. You can add a mouse listener to a panel, but why? Unless you have some specific action related to the panel, why would you want to click it?



I whant to add a boarder om this..
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can make a button look like a JLabel:

 
Campbell Ritchie
Marshal
Posts: 52516
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changing the particular label's appearance does relate directly to that label, so maybe a mouse listener would be appropriate.You probably have to declare myLabel as final, except in Java8 where that rule has been slightly relaxed.
 
Niklas Karlsson
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:You can make a button look like a JLabel:



Yes but the doClickdosent work of I whant to toggle the boarders..


 
Campbell Ritchie
Marshal
Posts: 52516
119
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't add a mouse listener to a button. Use doClick or an action listener. Try

myComponent.setBorder(myComponent.getBorder() == null ? new XYZBorder() : null);
 
Niklas Karlsson
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don't add a mouse listener to a button. Use doClick or an action listener. Try

myComponent.setBorder(myComponent.getBorder() == null ? new XYZBorder() : null);


my goodnes thanks!!


 
Campbell Ritchie
Marshal
Posts: 52516
119
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I take that as meaning it worked?

You're welcome
 
Piet Souris
Rancher
Posts: 1625
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An alternative would be to use a JToggleButton, disguised as a JLabel,
like Rob suggested.

By the way: is there any specific reason why you use 'doClick()'?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!