How to give a button a custom shape in Java Swing? - java

I'm new to Java Swing, I searched a lot about this question on stackoverflow.com and other websites, but couldn't find a concrete answer, so I decided to ask it myself. I want to create a JButton of a custom shape - of the map of the USA. I have a png image with a transparent background. I know that
usaButton.setOpaque(false);
usaButton.setContentAreaFilled(false);
will get rid of border of the button and remove the background color, but I want it to create a border around the map itself whenever I hover my mouse over it, not a rectangular border around the button. This is the map just for the reference:

Related

Jlabel Extra Clickable Space

I create buttons using jlabels, so I can make a image into sort of a button. The only problem is, is that jlabels are square, therfore if i click somewhere within the square where the picture is not contained, it still runs the jlabel.MouseClickEvent. Is there any fix for this, or another component that i could use?
Ex. If i click this on the corner where the circle is not showing, but the square is still there, then the event fires.
Any fixes/different components to use? Thanks!
If you are just using simple Shapes for the images then you might be able to use the Shape Component found in Playing With Shapes.
The ShapeComponent will only respond to mouse events within the bounds of the Shape.
Otherwise the solution is to override the contains(...) method of your JLabel to check if the mouse point is in the bounds of your image, or in your case if the pixel at that location is not transparent.

Java Swing : best way to set background image clickable

I try to find the best way to make an image clickable with different action depending on the area you click. For now, my window display an EFTPOS (image found on google image) using :
getContentPane().add(new JPanelBackground("./img/background.png", "./img/eftposAboveBackground.png", this.getWidth(), this.getHeight()));
Now, I would like to click on every EFTPOS's buttons. I was thinking of create some transparent buttons at the buttons's positions but :
First, I don't want to use an absolute layer
Second, the Swing GUI design editor (Netbeans) doesn't display my background image since I used the code above.
I'm pretty sure there is another way than setting my eftpos on the background, maybe using the ImageIcon class..
Any help would be greatly appreciated!
You can to create an ArrayList of Rectangles (or Shapes) to represent the area of each button on the image.
You need to add a MouseListener to the image.
In the mousePressed() event you need to iterate through all the Rectangle in the ArrayList using the Rectangle.contains(mouse point) method to see if the user clicked in a Rectangle.
When you find the Rectangle that was clicked you execute your custom code.

Howto paint a popup with image beside a component being positioned in a relative layout manager?

A have a list with small thumbnails and want to show a fancy tooltip with a larger version of the image of the thumbnail left to the thumbnail if one hovers with the mouse over the thumbnail. If one drags the mouse down to the next list element the old tooltip should disappear and a new tooltip next to the new cell should appear.
Unlike the existing text tooltip javax.swing.JComponent.setToolTipText(String) my fancy tooltip should always be displayed left of the current column. The problem is, that this is a very complex relative layout I cannot change for this. The fancy tooltip should appear above all other components like the real tooltip does too, i.e. I cannot reserve some free space for it.
I took a look in the implemenation of the real tooltip and think about something like getting the absolute coordinates of the mouse mouseEvent.getX() and subtracting the position inside the thumbnail to get the right corner of my fancy tooltip. Is it possible to get the coordinates of the mouse event inside the component where the listener is registered? Any better ideas for my needs?
I thought java.awt.event.MouseEvent.getX() is the absolute point, but thats java.awt.event.MouseEvent.getXOnScreen(), i.e. this is already the answer...

How do I make a Swing JComponent a bigger mouse target?

I have a JPanel that contains a bunch of Swing JComponents, including some JSeparators that may be only one or two pixels wide. I want to let my users drag the items around, but it can be rather difficult to hit a one or two pixel wide line. Is there a way that I can give those JSeparators a wider "target" region for mouse clicks? The only thing I've been able to think of is to have my mouse handler listen for clicks on the JPanel, and if it gets any, run through the list of JSeparators, looking to see if any of them are within a couple of pixels of the mouse click.
Should that work? Is there a better way?
Add a fat EmptyBorder to the component.
If it already has a border, you can set a compound border using the current border then the empty border, or simpler, add the empty border (and listener) to a panel that contains the component. The latter will work better for components such as JButton, which have borders that change according to state and focus.

Java GUI, creating piano keys using JButton

I'm trying to create a simple piano application using Java, and I'm stuck at creating the keys.
So what I have is a bunch of (Black) JButtons on top of (White) JButtons, but whenever I click the white buttons, it covers the black buttons.
So my question is; how do I implement this so the black JButtons would stay on top of the white ones.
I'm not sure how you're laying out your buttons in the first place. I suggest you use JLayeredPane for the layout. With it, you specify z-index of each component - and it takes care of the rest. Have a look here: http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html#depth

Categories

Resources