Mars
Mars

Reputation: 917

Custom JButton with JTextArea component inside

I extended JButton so it would fit my needs. I have one JTextArea and two JLabel components inside of my new class. The problem is, that I cannot click through JTextArea. So, when mouse is in JTextArea bounds, the button is not responding. There is no problem with labels.

As on screen. There are four buttons. Each one is a separate yellowish rectangle. When mouse is over JTextArea's gray rectangle, I cannot press the button. I need JTextArea because it supports multiple lines. Is there any option to make it not intercept the mouse?

It would be ok if I could attach ActionListener to JTextArea, but I can't. It cannot have this kind of listener.

enter image description here

Upvotes: 0

Views: 395

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347334

Take a look at Concepts: Editors and Renderers to understand the difference between a renderer and an editor.

A renderer is a "rubber stamp" of the component, it is simply "painted" onto the surface of the JTable and is not a real life component

You would need to implement a custom editor, which could translate the trigger event (in your case the MouseEvent) into a local context.

Take a look at TableCellEditor#isCellEdtiable which is probably the closest you will get to the source of the event which might trigger the cell to become editable.

JButton could be seeded with a HTML String (`"This is some text"), which would be capable of supporting multiple lines and line wrapping as well

Having said all that, you might want to seriously reconsider your design...

Upvotes: 2

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

You look to be trying to use a JButton in a very non-button way, including having it hold a JTextArea, and not looking at all like a button. If you want a clickable area that is not an identifiable JButton, then consider using a MouseListener instead. You would likely have to add the same MouseListener to the container JPanel and the JTextArea.

Upvotes: 2

Related Questions