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

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.

Related

How to replace down arrow with text in a combobox in JavaFX

I'm trying to remove the down arrow from a combobox. All the solutions I have found just make the arrow disappear, for example this one.
Is there a way to remove completely the space where the arrow appears and fill the box just with the text of the selected choice?
If you want to completely elimnate the arrow & arrow button space, you can try with the below custom ComboBox.
The below code is setting the arrow button and arrow nodes size to 0 and asking to rerender the comboBox. The null check is to let this changes apply only once.
public class MyComboBox<T> extends ComboBox<T>{
Region arrowBtn ;
#Override
protected void layoutChildren() {
super.layoutChildren();
if(arrowBtn==null){
arrowBtn= (Region)lookup(".arrow-button");
arrowBtn.setMaxSize(0,0);
arrowBtn.setMinSize(0,0);
arrowBtn.setPadding(new Insets(0));
Region arrow= (Region)lookup(".arrow");
arrow.setMaxSize(0,0);
arrow.setMinSize(0,0);
arrow.setPadding(new Insets(0));
// Call again the super method to relayout with the new bounds.
super.layoutChildren();
}
}
}
UPDATE :
Based on the suggestion of #kleopatra, we can get the same behaviour using css as well (without the need to create a new class for ComboBox).
.combo-box .arrow-button,
.combo-box .arrow{
-fx-max-width:0px;
-fx-min-width:0px;
-fx-padding:0px;
}
The below image will tell you the difference of a normal combox box and this custom combo box. The left one is the normal comboBox, you can see the list cell when inspecting with ScenicView. The right one is the custom one. The list cell is completely occupied suppressing the arrow space.

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

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');
}

Javafx Jfoenix Drawer is blocking the nodes behind the Overlay

I am new to Javafx and after some readings on the subject I decided to start building my first application.
My application has a main page and an hamburgerDrawer on left side panel. The trouble I am having is that when I load the application, the drawer overlay is blocking the nodes beneath it. I tried to use the method SetDefaultDrawerSize() to 0 when I close the drawer, and to a specific size, say SetDefaultDrawerSize(900), with no success, the drawer is still blocking the content beneath.
#Override
public void initialize(URL url, ResourceBundle rb) {
try {
AnchorPane drawerContent = FXMLLoader.load(getClass().getResource("/materialitemtracker/view/AnchorPaneHamburgerDrawerView.fxml"));
hamburgerDrawer.setSidePane(drawerContent);
hamburgerDrawer.setOverLayVisible(true);
hamburgerDrawer.setResizableOnDrag(true);
HamburgerBackArrowBasicTransition burgerTask = new HamburgerBackArrowBasicTransition(hamburger);
burgerTask.setRate(-1);
hamburger.addEventHandler(MouseEvent.MOUSE_PRESSED, (e) -> {
burgerTask.setRate(burgerTask.getRate() * -1);
burgerTask.play();
if (hamburgerDrawer.isShown()) {
hamburgerDrawer.close();
// hamburgerDrawer.setDefaultDrawerSize(0);
} else {
hamburgerDrawer.open();
// hamburgerDrawer.setDefaultDrawerSize(900);
}
});
} catch (IOException ex) {
Logger.getLogger(AnchorPaneMainController.class.getName()).log(Level.SEVERE, null, ex);
}
Sample images:
Application Main Page
Drawer content
I had a similar issue to the one you describe. However in my case it wasn't the overlay but the drawer itself. The overlay when visible would encompass the entire UI when visible so I quickly determined that this wasn't my issue since it was only the buttons on the left of the UI which were not receiving events. Instead it seemed the area affected was limited the default drawer size I had set.
There are two solutions i could see:
Move the buttons above the drawer in the UI structure. This didn't work for me since the buttons would then overlap the expanded drawer in my UI.
Give the drawer a negative offset and change the constraints depending on the drawer state at runtime. My drawer resides on the left of the UI inside an AnchorPane so set a negative left anchor value to begin with (-255 for this example). Then depending on the state I do as follows.
drawer.setOnDrawerOpening(event ->
{
AnchorPane.setRightAnchor(drawer, 0.0);
AnchorPane.setLeftAnchor(drawer, 0.0);
AnchorPane.setTopAnchor(drawer, 0.0);
AnchorPane.setBottomAnchor(drawer, 0.0);
});
drawer.setOnDrawerClosed(event ->
{
AnchorPane.clearConstraints(drawer);
AnchorPane.setLeftAnchor(drawer, -255.0);
AnchorPane.setTopAnchor(drawer, 0.0);
AnchorPane.setBottomAnchor(drawer, 0.0);
});
Try the following code:
if (drawer.isOpened()) {
drawer.close();
drawer.setVisible(false);
} else {
drawer.setVisible(true);
drawer.open();
}

Resize a Button JavaFX

i have a question about a JavaFx Button.
In the following code i add a Button into a HBox.
ivTriangleImg.setFitHeight(16);
ivTriangleImg.setFitWidth(16);
ivTriangleImg.setRotate(iRotateCoord1);
btnTriangle.setGraphic(ivTriangleImg);
btnTriangle.setStyle("-fx-background-color:green;");
addComponentToBox(btnTriangle);
The Button gets a Graphic -> The Graphic is a transparent triangle with the Size of 16x16 pixels.
The Problem is, that the Button doesn't have the Size 16x16 it is so much more. How can i get the Button smaller and the Pictur must have the same size?
For JavaFX 8 use
btnTriangle.setPadding(Insets.EMPTY);
For JavaFX 2 use
btnTriangle.setStyle("-fx-padding: 0;");
However you can directly put the image view to the scene rather than setting it to the button's graphic, and add listeners you want:
ivTriangleImg.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
// do something
}
});
I added your Code, it works very fine. I made it a little bit different:
btnTriangle.setStyle("-fx-background-color:transparent;-fx-padding:0;-fx-background-size:0;");

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