• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

actionListener for JLabel

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to add actionListener to JLabel? I want to click and select Jlabels for further processing.
Thanks.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No you can't. You can add a MouseListener though.

There is a trick that can give you a "label" that acts like a button. This is based on the notion that a radio button looks very much like a label with a button in front of it:

Now keep in mind that this is just quick & dirty. A better way would be to create your own subclass of AbstractButton, then use a ButtonUI to do the painting. You can use JRadioButton as an example, but strip the specific radio button behaviour.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:
Now keep in mind that this is just quick & dirty. A better way would be to create your own subclass of AbstractButton, then use a ButtonUI to do the painting. You can use JRadioButton as an example, but strip the specific radio button behaviour.



Actually, there's no need to mess with ButtonUI. JButton has methods such as setBorderPainted() and setContentAreaFilled() for a reason.

You may want to take a look at this related older thread.
[ March 31, 2008: Message edited by: Brian Cole ]
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, I missed setContentAreaFilled.

I tried something like this once but only called setBorderPainted. The result was quite horrible.
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your advice. I have created a label and if the user wants to remove it from the panel, he should be allowed to do that. I am trying to design like this. If the user right clicks, "Remove" should appear and if the user clicks on it, a dialog box should appear and if the user clicks OK, then that label should be removed. It may not be able to process like this for JLabel. In that case, how do I display my "string" in the panel? Right now I am using JLabel to display the string. Thanks again.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding a mouse listener to the label can do just that:

Don't forget to revalidate() and/or repaint() your panel after removing the label.
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for you suggestions. I tried poptrigger but it didn't work. Since this is my first swing project, lot of confusion starting from component selection. I tried adding action listener to JLabel and I am able to see the JLabel getting underlined(code below) PropertyChangeListener also worked. My motivation is, when I press a button, I should be able to detect this change and remove that label from the JPanel. These labels are generated dynamically. I continued in this thread rather than in new thread as people in this forum suggested. Thanks.

My objective is when the user selects a label, that should be removed from the JPanel. SOrry for the confusion.



Or do I have change JLabel to any other component to achieve this? The user needs to select a component and remove them. Otherwise it is just for the display. That is why I started with JLabel.

[ April 11, 2008: Message edited by: Gopu Akraju ]
[ April 11, 2008: Message edited by: Gopu Akraju ]
 
Brian Cole
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gopu Akraju:
I tried adding action listener to JLabel...

Or do I have change JLabel to any other component to achieve this?



You can't add an ActionListener to a JLabel. If you really want
to add an ActionListener to something, I recommend a JButton.

It is possible for a JButton to act like a button but to look like
a label, if that's what you are going for. See this thread.

If you want to continue with your MouseListener approach, that's
ok too.
[ April 11, 2008: Message edited by: Brian Cole ]
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you want to continue with your MouseListener approach, that's
ok too.



How do I detect this change? Any particular method? I want to detect this change when I click a button?
Basically I want to click JLabel and when I click button, that label should get removed.

That is where I get struck. Thanks.
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to detect the mouse litener from a JLabel. For example, we add actionlistener to check box and detect the change with a method called (chbk.isSelected == trye) in order to check the status of checkboxe. I tried with label.isFocusable, but didn't work. Can any one suggest on how to check the status of JLabel whether it is nderlined or not?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Can any one suggest on how to check the status of JLabel whether it is underlined or not?

right click the label a few times

 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Michael,
I really appreciate your time and help. As I am confused A LOT, let me explain again please. When I click on JLabel, it gets underlined as I set the text of JLabel underlined under mousePressed listener. After this action, when I click on a button(REMOVE), I need to detect which label from the JPanel is underlined and then I should proceed from there. I could understand till the label gets underlined. Inside the actionPerformed function of REMOVE button, I need a way to find a label from the JPanel which gets underlined?
Is it possible to do? Do I have to getText of JLabels from the Panel and check whether it is underlined? Am I proceeding in the right path? Please let me know.
[ April 13, 2008: Message edited by: Gopu Akraju ]
 
Brian Cole
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gopu Akraju:
When I click on JLabel, it gets underlined as I set the text of JLabel underlined under mousePressed listener. After this action, when I click on a button(REMOVE), I need to detect which label from the JPanel is underlined and then I should proceed from there.



While it's probably possible to determine which JLabel is
the underlined by sifting through the JPanel's components,
the usual way this would be done is to just keep track of
it with a variable.

So at the top of your class you declare a field like this:
private JLabel theUnderlinedOne = null;

Then right after you call
label.setText("<html><b><u>" + text + "</u></b></html>");
you set the variable
theUnderlinedOne = label;
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Michael, it really works and served the purpose. I have one more doubt? Is tehre any way for me to allow the user to click only one label at a time? If the user clicks one label and clicks on another label, I want to underline only the latest click and the first label should be back to normal position, by this I can allow the user to remove only one label at a time.
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I could managed to do it inside the mousePressed method. I checked whther the html tags are added to label and removed them accordingly on the next mouse press.
 
reply
    Bookmark Topic Watch Topic
  • New Topic