JavaFX background Setting - css

I am making a simple software to Reserve Stadium Seats. I have a main page wish is the Login Page. I have a simple design. With a transparent background just to give it a form. As you can see in the image below.
As you can see the code can bee seen behind the form. When i login i have a simple logout button that takes me again to the same fxml to load the form. but this time the background is no longer transparent: . As you see here it is green. it is by default white but i was trying to make it transparent again.
this is the code i tried to do:
public void logoutButton (ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow();
Scene scene = new Scene(root);
root.setStyle("-fx-background-color: #20825170;");
stage.setScene(scene);
}
even changing the style to transparent wont work i tried it. any help ?

You need to set the fill of the scene to transparent when a user logs out
scene.setFill(Color.TRANSPARENT);
Edit
As #jewelsea stated in the comments, You don't have to set the scene's fill to TRANSPARENT every time if you don't change the scene, you can keep the same scene and only set the fill to transparent the first time (in the Application's start method will work)

Related

JavaFX - Combobox Expands Downward Off Screen (Post 8u60)

I thought this bug was supposed to be fixed a long time ago... but when I place a ComboBox near the bottom of a window it expands downward and off the screen. I think the reason it does this is because there are other controls above it, so rather than "bump" into those it just expands downward instead. Why doesn't the combo box expand over the the rest of the layout? All I know is that when I add some padding above it, it will expand upward instead of downward.
Example:
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
ObservableList<String> choiceList = FXCollections.observableArrayList();
choiceList.addAll("Choice1", "Choice2", "Choice3", "Choice4");
ComboBox<String> choices = new ComboBox<>(choiceList);
choices.setMinWidth(100);
Button button1 = new Button("First Button");
button1.setMinWidth(150);
Button button2 = new Button("Second Button");
button2.setMinWidth(150);
VBox layout = new VBox(10);
layout.setPadding(new Insets(10, 10, 10, 10));
layout.getChildren().addAll(button1, button2, choices);
Scene scene = new Scene(layout);
primaryStage.setScene(scene);
primaryStage.setTitle("Example");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Image of window displayed by the above code
Anybody got a fix for this?
Note, if in your case, you are moving your main application window to the bottom of the screen and the combo box is still dropping down and being cut off so that some contents of the popup are not visible, then it may well be a bug. But my guess is, that if that is what is happing, that it is a system specific issue as I cannot replicate such an issue on OS X. If that is the case, try a Java 9 early access release and see if the issue still replicates, and if it does you could file a bug report.
This is not bug, it is by design.
The ComboBox popup is internally implemented as a PopupControl. A PopupControl is a new window, which isn't constrained to lie within the bounds of its parent window (your main stage). JavaFX has logic to ensure that popups such as the ContextMenus and ComboBox popups are shown within the visible screen area, not the owning stage area.
If you move your main application towards the top of the screen, then open the combo box popup, the popup will drop down because there is room on the screen to display it. But if you move the main application towards the bottom of the screen, then the open the combo box popup, the popup will drop up (I don't know a better way to explain that concept ;-) because there is no room to display the popup if it were to drop down.
Drop up sample. Created by running your example code on OS X, moving the main application window near the bottom of the screen and opening the combo box popup.

Open a javafx fxml file in separate window on a button click without duplication

I use the below code to open a javafx fxml file in a different window on a button click event and it works fine. But if I click the same button again while the window opened it will create a duplicate window. Is there a possibly solutions to overcome this problem? Thanks in advance.
Parent parent = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Stage stage = new Stage(StageStyle.DECORATED);
stage.setTitle("Title");
stage.setScene(new Scene(parent));
stage.show();
I just stumbled upon this old question and figured I might answer it for anyone new to JavaFX or coding in general (I'm bored..).
In the provided code (see below) a new Stage is created every time, meaning that if this is run inside of a method you actually create a new Stage variable every time that the code is run:
Stage stage = new Stage(StageStyle.DECORATED);
What you could do instead create your Stage variable outside the method so that you either 1. just overwrite it every time or 2. have some "is showing" or a nullcheck or similar to see if a new stage should be created or if the existing one just needs to be shown.
For example:
private Stage stage;
private void onOpenNewStageBtnClicked(){
if(stage == null){
Parent parent = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
stage = new Stage(StageStyle.DECORATED);
stage.setTitle("Title");
stage.setScene(new Scene(parent));
}
stage.show();
}
Also, what I usually do is I create a Stage factory to avoid a lot of duplicate code and so that I can break out the creation of the stages and the loading of fxmls to other classes than my controller.

How to creat a button programmatically with JavaFX?

I want to make a button on an AnchorPane without drag it from the library in the FXML file I want to do it progammatically: if the search button clicked, should show a new button not existed in the AnchorPane before I did this code but I don't know what is wrong with it:
private void searchButton(ActionEvent evt) {
Button tab = new Button();
tab.setLayoutX(147);
tab.setLayoutY(102);
tab.setText("Tab1");
tab.setPrefHeight(27);
tab.setPrefWidth(69);
WebView wb = new WebView();
wb.setLayoutX(-1);
wb.setLayoutY(128);
wb.setPrefWidth(1604);
wb.setPrefWidth(700);
}
I am assuming that you searchButton method is in controller attached to some FXML. Then all you need to do is this:
yourAnchorPane.getChildren().add(tab);
If you don't have already published reference to anchorPane in your controller, then add this into your controller
#FXML
AnchorPane yourAnchorPane;
And in SceneBuilder select your anchorPane, go to code tab and enter "yourAnchorPane" as fx:id.
Further info on working with anchorpane is javadoc.
You probably also want to set some constraints on the tab to locate it at a position within the AnchorPane. For instance, the following code will locate your button tab relative to the top left corner of the AnchorPane: Ten pixels down and fifteen pixels to the right.
AnchorPane.setTopAnchor(tab, 10.0);
AnchorPane.setLeftAnchor(tab, 15.0);

Be careful your GUI work(click or drag to photo) in javaFX when using transparent PNG file

(English expression is very hard to me. Please correct my writing if anyone fine strange expression.)
Be careful, if you have a mind to use PNG file when make draggable or click action.
Sometimes transparent PNG will disturb dragging and click action.
So, if you want to use PNG on your dragging or click action work, use opaque PNG.
Last week, I tried to make a draggable imageview on tilepane.
It looks like photo tile puzzle.
I applied clicking and dragging action.
Then it sometimes worked and sometimes didn't workd.
I wondered why it is.
So I tried test over and over again.
Finally, I could fine a clue.
One transparent PNG has a opaque part and transparent part.
Thus, the task works when click opaque part and doesn't work when click transparent part. Lets see details below my answer.
Both result is different. One thing point to imageview, and another thing point to hbox(parent of imageview).
1.When click opaque part of PNG
#FXML
private void tile1MouseClicked(MouseEvent event) {
System.out.println("result : " + event.getTarget());
}
result : ImageView#7afa16c0
2.When click transparent part of PNG
#FXML
private void tile1MouseClicked(MouseEvent event) {
System.out.println("result : " + event.getTarget());
}
result : HBox#3f30efe6
String[] imageName = {"slide1.png", "slide2.png", "slide3.png", "slide4.png",
"slide5.png", "slide6.png", "slide7.png", "slide8.png"};
Image img = null;
for (int i = 0; i < imageName.length; i++) {
try {
img = new Image(getClass().getResourceAsStream(imageName[i]));
} catch (Exception e) {
System.out.println("exception : " + e);
}
ImageView imageview = new ImageView(img);
imageview.setFitWidth(20);
imageview.setFitHeight(20);
HBox hbox = new HBox();
hbox.getChildren().add(imageview);
tile1.getChildren().add(hbox);
}

Stage Icon is not visible

I am creating a sample code to show the Stage designed in Javafx, It should not have Minimize and Maximize Button only Close ('X') button required.
For that we are using following code.
Stage stage = new Stage();
// Here we have load it using JFXML
stage.initModality(Modality.WINDOW_MODAL);
stage.initStyle(StageStyle.UTILITY);
stage.setResizable(true);
if (title != null && !title.trim().isEmpty()) {
stage.setTitle(title);
}
stage.setWidth(w);
stage.setHeight(h);
stage.getIcons().add(new Image(Dialog.class.getResourceAsStream("/image/myicon.png")));
stage.showAndWait();
Now the icon I set on the stage is not visible.
What I am missing ?
I assume Windows as the OS (in MacOSX the icon is shown). Under Windows, StageStyle.UTILITY leads to using WS_EX_TOOLWINDOW in which case the icon is not shown. You probably need to use another StageStyle.
"PRB: WS_EX_TOOLWINDOW Windows Do Not Show System Menu Icon": http://support.microsoft.com/kb/179376

Resources