How do I switch the image files I want to see in a JavaFXML Button? - css

I am trying to toggle a button between two states. One state is to display an
image of a red circle, the other state being an image of a blue circle. When the user clicks the button it should toggle between the states. I have set
style in my css to accommodate the button:
#button-debit {
-fx-background-image: url("images/redButton.jpg");
}
#button-credit {
-fx-background-image: url("images/blueButton.png");
}
To see if this works I have tried just to transform from red to blue by doing this:
#FXML
private void handledborcrBtn() {
dborcrBtn.setId("button-credit");
}
However the result is not as expected. The red button image stays in place with the blue one underneath it, but the blue image is split up into several parts.
I think I remember a 'repaint' method from javax Swing but there seems to be more complicated with JavaFXML and I'm having trouble getting this to work properly.

Try it with the ToogleBotton
.toggle-button {
-fx-graphic: url('icons.jpg');
}
.toggle-button:selected {
-fx-graphic: url('othericon.png');
}

Related

JavaFX - Set different hover colors on toggle button based on toggle state

I have a toggle button in my program that starts/stops a script. I would like for this button to be green and say "START" when the button is not selected, and red and say "STOP" when it is selected. More importantly, I would like the unselected hover color to be a slightly darker version of the original green, and the selected hover color to be a slightly darker version of the red color. My current CSS for this button looks like this:
#startStopButton {
-fx-border-color:#d4d4d4;
-fx-background-color:#85eca5;
-fx-background-image: url("startButton.png");
-fx-background-size: 50px;
-fx-background-repeat: no-repeat;
-fx-background-position: 80% 50%;
-fx-alignment: CENTER_LEFT;
-fx-effect: dropshadow(three-pass-box, #e7e7e7, 15, 0, 0, 0);
}
#startStopButton:hover {
-fx-background-color:#80dc9c;
}
#startStopButton:selected{
-fx-background-color: #ff6060;
-fx-text:"STOP";
}
#startStopButton:selected:focused{
-fx-background-color: #ff6060;
-fx-text:"STOP";
}
Currently, this will work fine, except for when the button turns red. In this case, there is no hover effect. Within my FXML controller, there is a function that is activated every time this button is clicked:
private void startStopClick()
{
if(startStopButton.isSelected())
{
startStopButton.setText(" STOP");
// startStopButton.setStyle()
}
else {
startStopButton.setText(" START");
}
}
Is there any way to 1) set the button text within CSS so that I can leave that out of my controller?
2) Get the current toggle button state in CSS, so that I can have multiple hover effects. For example, something like this:
#startStopButton:unselected{
-fx-background-color: #ff6060;
-fx-text:"STOP";
}
If there is no way to do this in CSS, can I set the hover styles in the Java code in the FXML controller?
CSS properties are only available for the look of nodes. With a few exceptions the basic JavaFX nodes don't allow you to specify content via CSS. The text property of buttons is no exception; it cannot be set using CSS.
As for the colors: The rules occuring last override values assigned by rules with the same precedence occuring before them. This means the background color assigned by the rules for #startStopButton:selected and #startStopButton:selected:focused always override the color #startStopButton:hover assigns.
Since in both cases you want a darker color when hovering, the derive function and a lookedup color may work for you.
Example
#Override
public void start(Stage primaryStage) {
ToggleButton btn = new ToggleButton();
btn.getStyleClass().add("start-stop");
btn.textProperty().bind(Bindings.when(btn.selectedProperty()).then(" STOP").otherwise(" START"));
Pane p = new Pane(btn);
Scene scene = new Scene(p);
scene.getStylesheets().add("style.css");
primaryStage.setScene(scene);
primaryStage.show();
}
style.css
.start-stop.toggle-button {
base-color: #85eca5;
-fx-background-color: base-color;
}
.start-stop.toggle-button:selected {
base-color: #ff6060;
}
.start-stop.toggle-button:hover {
-fx-background-color: derive(base-color, -20%);
}
If you cannot use derive since you need to specify different colors for all 4 states you could still rely on looked-up colors to avoid relying on the rule ordering:
.start-stop.toggle-button {
unselected-color: blue;
selected-color: yellow;
-fx-background-color: unselected-color;
}
.start-stop.toggle-button:hover {
unselected-color: red;
selected-color: green;
}
.start-stop.toggle-button:selected {
-fx-background-color: selected-color;
}

How to create a transparent Button with JavaFX without decreasing the hitbox?

As soon as I create a new Button in JavaFX and set the background transparent with: myButton.setBackground(Background.EMPTY); or myButton.setStyle("-fx-background-color: transparent;"),
the hitbox will only consist of the text in the button when catching the ActionEvent via :
myButton.setOnAction(newjavafx.event.EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
//handle UI input
}
});
So I have to aim on a letter and click it, which is annoying, especially when having changing text and/ or small text.
How can I keep my hitbox the same while having a transparent background?
Use
myButton.setPickOnBounds(true);
which means that the layout bounds of the button will be used to determine mouse hits on it, rather than its set of non-transparent pixels.

Getting rid of pressed effect in JavaFx toggle Button

How can I get rid of the pressed effect of javafx toggle button? Essentially, I want the look and feel of the button to be the same except the background image.
In your CSS file, you can do
.toggle-button:armed {
-fx-color: -fx-base ;
}
You might also want to remove the hover effect:
.toggle-button:armed, .toggle-button:hover {
-fx-color: -fx-base ;
}

QPushButton focus

I have a pushbutton which its background image gets changed regularly according to the packets ui receives from serial. When the user clicks the button, it becomes red with borders.
pb_Jack1Up ->setStyleSheet("QPushButton {border-image: url(:/ArrowKey/Up_Default.jpg); } QPushButton:focus {border-width: 1px;border-style: solid; border-radius: 4px;}");
Now, what I want is to give the button some effects, to make the user understand that the button is clicked. like make the button go inside when clicked, and when user releases the button border shall no longer be set anymore. However, in my case the button border remains. I cannot also set style sheet with pressed and released slots, because the background image depends on what we receive from serial.
Could anyone help me please?
You can use:
QPushButton { ... }
QPushButton:disabled { ... }
QPushButton:pressed { ... }
QPushButton:focus { ... }
QPushButton:hover { ... }
or take a look at this blog entry.
The button keepsfocus after you release the mouse. Thats why it still has the red border.
You can check that if you activate another widget of the form, clicking, or tabbing, red border dissapears.
Try using
QPushButton:pressed
instead of focus

show border on rollover and select the thumb on click and unselect pre selected thumb

I have some small canvas, and i have to show border around them, i did that using rollover and rollout evenets, (these canvas hold product images), here rollover and rollout are working perfectly fine, but when a user clicks on some canvas, it has to be selected, means it has show the border around it, and rest canvas should work as normal. but when i select another canvas the previously selected canvas should get unselected and new clicked canvas gets the selection,
but the problem which is coming the rollOut event which is applied on canvas, on a click the canvas get selected, but when rollOut takes place it unselect the canvas, i even removed the rollOut listner on the click of a canvas, but in that case, the clicked canvas will not get unselected , when other canvas will be clicked
can.addEventListener(MouseEvent.ROLL_OVER,onRollOverThumb);
can.addEventListener(MouseEvent.ROLL_OUT,onRollOutThumb);
//can.addEventListener(MouseEvent.CLICK,onRollOverThumb);
private function onRollOverThumb(event:MouseEvent):void
{
event.target.setStyle('borderColor','0x000000');
event.target.setStyle('borderThickness','3');
event.target.setStyle('borderStyle','solid');
}
private function onRollOutThumb(event:MouseEvent):void
{
event.target.setStyle('borderColor','0xCCCCCC');
event.target.setStyle('borderThickness','1');
event.target.setStyle('borderStyle','solid');
}
i hope some thing are clear in this, does n e one has worked on this, please reply
Thanks in advance
Ankur sharma
What about implementing a "flag" variable that is set to true when the click occurs. Then, when the ROLL_OUT occurs, check if the flag is true or false. If true, don't do anything, if false, remove/change the border.
private function onRollOverThumb(event:MouseEvent):void
{
if(event.type=='click')
{
for(var j:int=0;j<viewparent.numChildren;j++)
{
viewparent.getChildAt(j).name="false";
}
event.currentTarget.name="true";
for(var i:int=0;i<viewparent.numChildren;i++)
{
if(viewparent.getChildAt(i).name=="true")
{
Canvas(viewparent.getChildAt(i)).setStyle('borderColor','0x000000');
Canvas(viewparent.getChildAt(i)).setStyle('borderThickness','3');
Canvas(viewparent.getChildAt(i)).setStyle('borderStyle','solid');
}
else
{
Canvas(viewparent.getChildAt(i)).setStyle('borderColor','0xCCCCCC');
Canvas(viewparent.getChildAt(i)).setStyle('borderThickness','1');
Canvas(viewparent.getChildAt(i)).setStyle('borderStyle','solid');
}
}
}
else
{
event.currentTarget.setStyle('borderColor','0x000000');
event.currentTarget.setStyle('borderThickness','3');
event.currentTarget.setStyle('borderStyle','solid');
}
}
private function onRollOutThumb(event:MouseEvent):void
{
if(event.currentTarget.name=="false")
{
event.currentTarget.setStyle('borderColor','0xCCCCCC');
event.currentTarget.setStyle('borderThickness','1');
event.currentTarget.setStyle('borderStyle','solid');
}
}
i modified my own code, added one name property to the canvases
can.name="false"
and it's now working,
can n e one tell me, how to put some select and unselect(kind of fade effect) on the border, when the black selection get removed, it shld be removed in some fade manner, can we apply fade effect on border?

Resources