Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to enlarge the clickable area of a JButton  RSS feed

 
Sascha Widmann
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey folks,

what I have already tried:

to set the margin of a button - didn´t work
to set anything like padding - didn´t found something for java
to set an empty border - didn´t work

I don´t know what to try next.

My main problem is to enlarge the clickable area without resizing the buttons image.

maybe you guys know council.

 
Campbell Ritchie
Sheriff
Posts: 53747
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please explain what went wrong. Do you want to enlarge the clickable area larger than the button?
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to set the margin of a button/quote]

Works for me.

Post your SSCCE that demonstrates the problem.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do this by placing the button in a JPanel with an EmptyBorder and using a MousListener added to the panel to set appropriate states on the button's model.

I'm curious -- what brought about this unusual requirement?
 
Sascha Widmann
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a touchscreen monitor running a java applet. Sometimes the operator have to click 2-3 times on the screen because some buttons are tiny. Now I have to find a solution which doesn´t change optics, but increase the hit rate of the buttons.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I were a user of your application, I wouldn't be thrilled to find that touching the screen outside of the button resulted in the button being clicked. Far better to increase the visual size of the button.

I second Rob Camick's suggestion to post an SSCCE so we can find why setMargin(...) didn't work for you.
 
Sascha Widmann
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I´m looking for is like TouchDelegate class from Android... ...Helper class to handle situations where you want a view to have a larger touch area than its actual view bounds...

It´ll be too much work to make a SSCCE, because I´m looking for a solution for JButton, JTextfield, JComboBox,....

What i´ve done is, I wrote my own button extending JButton. The only changes are affect backgroundcolor, font, some other simple changes.
If I do it like that -> buttons[i].setMargin(new Insets(10, 10, 10, 10));
it´s like the bounds are increased by 10 at each adge.
 
Sascha Widmann
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a provisionally solution !

extending my own button class with this code, the hit area of the buttons became bigger!


 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I do it like that -> buttons[i].setMargin(new Insets(10, 10, 10, 10));
it´s like the bounds are increased by 10 at each adge.


And that is exactly what should happen. If you increase the size of the button you increase the clickable area of the button.
 
Sascha Widmann
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But that is exactly what I didn´t want, the optics of my elemnts shouldn´t be changed. Only the area which is waiting for a click should be a little bit bigger.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sascha Widmann wrote:But that is exactly what I didn´t want, the optics of my elemnts shouldn´t be changed. Only the area which is waiting for a click should be a little bit bigger.

This is error prone. What if you have two adjacent buttons? Clearly defining the 'clickable' area visually is good usability
 
Sascha Widmann
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the problem you are talking about with my provisionally solution. Let´s assume, the buttons are horizontally arranged. The hit area of the firtst placed button overlaps the hit area of the second one. Means button 1 overlaps bottun 2, button 2 overlaps button 3, and so on.

Therefore I called it a provisionally solution ;-)
 
Campbell Ritchie
Sheriff
Posts: 53747
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sascha Widmann wrote: . . .
Therefore I called it a provisionally solution ;-)
But everybody else has called it a pitfall for users
 
Sascha Widmann
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but it does what it´s supposed to do, partially....
 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sascha,

another possibility is to put every button in a dedicated JPanel, and give both panel and
button the same actionlistener. By giving each panel a different background color,
they can all be easily distinguished.

Greetz,
Piet
 
Campbell Ritchie
Sheriff
Posts: 53747
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you add action listeners to a panel?
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only through input actions. JPanel has no addActionListener method.
 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sloppy formulation on my part.

But it does not take the IQ of a genius to figure out what I mean.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:You can do this by placing the button in a JPanel with an EmptyBorder and using a MousListener added to the panel to set appropriate states on the button's model.

Actually, it's easier to just add a button to a button. Proof of concept:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!