I need to make a GUI in javafx where the buttons shape is custom made with a stylesheet
I use -fx-shape: "SVG Path"; to make the shape of my button
And sceneBuilder to make the GUI and if you need to know i use netbeans as my ide
Now when I make the custom form i want to only be able to click on that and not around it where the button is invisible
I have tried to use padding but i dont think that is the way
So if you look at this picture which is in scenebuilder
https://imgur.com/a/11dRR2G
you can see the white form which is what i want to be clickable
not the whole rectangle that you can see is lighted up
Ok this is not the direct answer to your question but a workaround on your Problem.
In you case i would just take the image which looks like that button and add a setOnMouseClickedEventListener
like this-
ImageView img = new ImageView("https://imgur.com/a/11dRR2G");
img.setPickOnBounds(false); // disables click on transparent areas
img.setOnMouseClicked((MouseEvent e) -> {
//do something
});
Related
I am currently developing the front end of an app,using WindowsBuilder plugin on eclipse, in which i have a "toolbar" on the top of the frame
with different butttons a user can interact with. I want the buttons to be circles, with a custom icon that i will add.When "mouse enters" i want the hovered over button to enlarge and when "mouse exits" i want the button to go back to normal. The problems I have encountered so far are the following:
Cannot make circular button.
How to enlarge/reduce them(apart from set.width() + something , setHeight() + somethingElse
I want something like this:
I have already tried the jtattoo L&F for circle button but the 1.3 version throws error and the 1.6 does not throw one but does not change the button shape.
Note:The reason i ask these 2 questiong together is that they can be solved(i think) with a single L&F theme if someone has developed/knows one.
Some time ago I wrote an OvalButton class that can handle oval, circular and capsule-like shaped JButtons.
You can extend this class and override getBackgroundImage() method to return the image you want to set it as the background of the button. Then add listeners and text as usually. Only a click on the oval/circular area triggers the action.
When you get this ready I believe you should be able to import a new component.
I have a ToggleGroup of ToggleButtons that should have the same style when unselected but each a different style when selected or hovered. I already found how this works with colors:
.my-toggle-button{
-fx-background-color: grey;
}
.my-toggle-button:hover{
-fx-background-color: -fx-mycolor;
}
.my-toggle-button:selected{
-fx-background-color: -fx-mycolor;
}
In my program, I can now set -fx-mycolor dynamically:
button.setStyle("-fx-mycolor: "+color);
where button is one of many ToggleButtons and color is a dynamically generated String.
This works just as expected: Each button has its own color when hovered over and keeps it when selected. If a different button gets selected, the previously selected button in the ToggleGroup snaps back to grey.
Now what I want is to do this with background images. I tried using
.toggle-button-fret-nonzero:selected{
-fx-background-image: url(-fx-myimage);
}
and then set it in Java via setStyle("-fx-myimage: "+image) but this doesn't work (CSS tries to load an image named -fx-myimage).
How can I achieve the behaviour demonstrated with colors for images?
I already tried to use setStyle("-fx-background-image: url("+image+");") directly on certain mouse-events like hovering or clicking but that's not what I want because then the button does not snap back to grey when another button is selected.
I would also like to avoid to define CSS-classes for every possible button since they are many.
EDIT: I solved the issue by using setStyle("-fx-background-image: url("+image+");") method and manually iterating over every button on every click, resetting the background-image accordingly. This works fine now, it's just that it feels like a hack since it's basically reimplementing the functionality of a ToggleButton...
I have a certain problem: I have created a quick clicker game, where two players try to click quicker than the other one. A special button freezes the opponent's main button. The frozen button currently only becomes unclickable and its text color becomes gray.
To add some "style", I would like to literally freeze it. I don't really know how it would look like, but I thought about a light blue background, and some light blue spikes around it or something like that. I haven't found any style that would do that, and any image to replace the button with, because "Frozen" finds other images ;)...
I'm not a designer so telling me that tools that can design buttons exist won't help me.
Also, just an image won't probably be enough, because the button can be frozen at any moment, with any text and any text color.
Thanks for your help.
You can create button look (drawable) and set it programmatically.
Example
myButton = (Button) findViewById(R.id.myButton);
myButton.setBackgroundResource(R.drawable.frozenDrawable);
To do some simple looks for buttons i recommend you
Android Button Maker
.
Example 2
For more advanced look you can create some png
ImageButton myButton = (ImageButton)findViewById(R.id.myButton);
myButton.setImageResource(R.drawable.frozenResource);
I don't have code, but here is what I would experiment with, if I had to create create the appearance of a frozen button while keeping its text and general appearance:
To ice the button:
Using Google Image search, find the image of a slab of ice
Create an imageView with that "slab of ice" image as the src, and make it about the same size as your button (or slightly larger if you want the ice to leak out)
Place that icy imageView directly over your button.
Make that imageView slightly transparent by setting the "alpha" property to 0.5 (or whatever looks good, keeping in mind that 0 = invisible and 1=fully visible).
To unfreeze the button:
Make the imageView invisible.
Re-enable the button
I have a dynamically created ImageButton. Here is my code -
ImageButton bar = new ImageButton(this);
... //some code
parentView.addView(bar);
Ripple effect appears when I click the ImageButton. But when I add the following line -
bar.setBackgroundColor(Color.RED);
The ripple effect doesn't appear anymore. Is there any way to make the ripple effect appear on a colored ImageButton?
By ripple effect, I mean this effect - click here (I'm sorry but I cannot add the image here as gifs are not supported)
Maybe try something like this:
bar.getBackground().setColorFilter(Color.RED, Mode.MULTIPLY);
You need to check what MODE would be apopriate, because i don't have big experience with this.
When i try to add background in Jbutton in java by going to its properties and then icon, it then sets the background of the button but the text which i have written on the button,it moves right(outside the button area).What is the solution for this?
If you wanted to add an Image to your JButton with NetBeans follow these steps :
Right Click Source Packages, under Projects and Select New -> Other -> (Under Categories) Select Other -> (Under File Types) Select Folder.
Click Next, and provide a name to the folder. For Example resources, do check that for Parent Folder, src is written inside the field. Now Click Finish.
Now manually go to this location on your Computer and create a New Folder, say images, and then paste the IMAGE inside this folder.
Now Under Design Mode, select your JButton, and on the Right Side go to this JButton 's properties. Just under foreground you will see icon is written, click the Eclipse Button associated with it to open the window as shown in Figure below :
Do check, this FIGURE 1, to fill in your values and then Press OK
You are done adding image to your JButton.
If you want to use the image I used, here it is .
Yeah, I forgot to mention, for this, I had set horizontalTextPosition = CENTER and veritcalTextPosition = BOTTOM under Other Properties, inside Properties.
And Here is the output of the whole thing :
As far as I understand you are not setting background but you are setting Icon to button by this. And that is why you see Image on right side and text on left side. To add image as background to button you need to override paintComponent(g) method of JButon and draw your image in it.
What is the solution for this?
One 'solution'1 it to write the text on the image. This is not a very good solution though, since:
The text would need to be written on each icon used for the button (e.g. normal, roll-over, selected etc.).
It would then become your responsibility to choose an appropriate font face & size.
The text would not automatically change font or size when the PLAF changes.
Things like mnemonics and accelerators will not work with the image-text button.
It was only when writing the points that it became more clear just how quirky it would be to work with.