0

I am trying to make a program that uses a JButton, but the regular button isn't working, so I want to make it so that when I press the JLabel it will activate a function.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 4
    Why not think - "how can I get my button working?"? – g00se Jul 16 '21 at 18:10
  • 1
    JButton isn't working so you implement its funcionality in a JLabel and it makes everything right? I wonder what's the reason for button not working in a first place. – zubergu Jul 16 '21 at 18:11
  • Like it says in the question the button has presets, so I need to get rid of those presets, but I don't know how, so I want to start from scratch with a JLabel – user15350339 Jul 17 '21 at 23:03
  • 1
    Tip: Add @g00se (or whoever, the `@` is important) to *notify* the person of a new comment. *"Like it says in the question the button has presets"* The word 'presets' is nowhere in the question, not that I even understand what that means or how it is supposed to negate using one. You have selected the wrong answer this time, and the question is of little value to future visitors, so down voting to warn them. – Andrew Thompson Jul 18 '21 at 16:17
  • @zubergu *"I wonder what's the reason for button not working in a first place."* As do I. Note the OP made a reply that makes no sense to me, maybe it does to you. – Andrew Thompson Jul 18 '21 at 16:19
  • @AndrewThompson I think this question could help new Java developers who can't figure out how to get the presets off of the button. – TheComputerKing Jul 19 '21 at 15:20
  • @TheComputerKing *"I think this question could help new Java developers who can't figure out how to get the presets off of the button."* Huh .. there's no accounting for what people think. Down vote well earned. – Andrew Thompson Jul 19 '21 at 16:59
  • @Andrew Thompson half of life is what people think – user15350339 Jul 20 '21 at 18:01

1 Answers1

1

All that you have to do is this:

label.addMouseListener(new MouseListener())
  • 2
    A `JLabel` with a `MouseListener` won't respond to keyboard input the way a `JButton` with an `ActionListener` can. The button with action listener will also respond to mouse input, has inbuilt functionality to change icons of mouse over, pressed etc., shows when it's focused & can be made to look like a label. So for this purpose, it is superior in every way. Minus one. See also [Is “Don't do it” a valid answer?](https://meta.stackexchange.com/questions/8891/is-dont-do-it-a-valid-answer) – Andrew Thompson Jul 18 '21 at 16:14