How to make a suttering window in JavaFx? - javafx

I need a window that can be move up down in my main window. Mainly IDE(IntelliJ, CodeBlock, Netbeans etc) output window feature.
I use TitledPane. By using this I can give a height that is expanded when I click the pane, but I can't expand the pane in any height.
see this - not expanded:
and this- expanded:
My code is here

Try using SplitPane. I guess this is what you want. You can learn more from the following link
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/SplitPane.html
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
SplitPane splitPane = new SplitPane(new Pane(), new Pane());
splitPane.setDividerPositions(0.5);
splitPane.setOrientation(Orientation.VERTICAL);
primaryStage.setScene(new Scene(splitPane,400,400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Related

Javafx does not accept other fonts or languages in textArea of Text Field

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.control.*;
public class textareasss extends Application {
#Override
public void start(Stage stage) throws Exception {
GridPane p = new GridPane();
TextArea tx= new TextArea();
p.add(tx,0,0);
Scene sce = new Scene(p,600,600);
stage.setScene(sce);
stage.show();
}
}
Link to the screen shot
Everytime i paste text in javafx Text Area or TextField, the text is not displayed but rather symbols like this:????.
I ran your code and it works, nothing wrong with JavaFX not accepting fonts or languages
this is what i used :
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage stage) throws Exception{
GridPane gridpane = new GridPane();
TextArea text= new TextArea();
gridpane.add(text,0,0);
Scene scene = new Scene(gridpane,600,600);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
and this is the output i got :
TextArea containing text in many languages

JavaFX: set an overlapping text on a button when clicked

I'm creating a simple lottery game where I can chose numbers from 1 to 42. I would like to mark the chosen number with a red "X" (instead of using an effect) but the user should still see the chosen number beneath the "X". When the user clicks the marked number again, the "X" should disappear.
The GUI looks like this:
enter image description here
How can I display a second text on a button in the way that the first text is still visible?
Thanks for your help
kind regards
Joel
You can set the Graphic of a button to a StackPane and then add an element to the StackPane. Since the children of a StackPane are all displayed on top of each other, this yields the effect that you want. My example just shows the concept, I would probably write a new class that extends Button in your use scenario :)
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
public class NewFXMain extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Pane pane = new Pane();
StackPane buttonPane = new StackPane();
Label label = new Label("42");
buttonPane.getChildren().add(label);
Button button = new Button();
button.setGraphic(buttonPane);
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
if(buttonPane.getChildren().size() == 1){
Label labelX = new Label("X");
labelX.setStyle("-fx-text-fill: red;");
buttonPane.getChildren().add(labelX);
}
else
buttonPane.getChildren().remove(1);
}
});
pane.getChildren().addAll(button);
primaryStage.setScene(new Scene(pane, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Simple way to create a vertical ButtonBar using JavaFX Scene Builder/Gluon

How do I make a vertical ButtonBar in JavaFX 8?
I want exactly the same concepts of the ButtonBar component, but ButtonBar is horizontal, I want vertical. All the buttons must be same size.
If I use a VBox I have to set the width of the buttons and of the VBox manually.
Is there a simpler way to do that without having to set widths?
You need two things: to set fillWidth to true on the VBox, and to set maxWidth on each button. The most convenient approach is probably a simple subclass of VBox:
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
public class VerticalButtonBar extends VBox {
public VerticalButtonBar() {
setFillWidth(true);
}
public void addButton(Button button) {
button.setMaxWidth(Double.MAX_VALUE);
getChildren().add(button);
}
}
and then you can do things like
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class VerticalButtonBarExample extends Application {
#Override
public void start(Stage primaryStage) {
VerticalButtonBar bar = new VerticalButtonBar();
bar.addButton(new Button("A"));
bar.addButton(new Button("Button"));
BorderPane root = new BorderPane();
root.setLeft(bar);
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
which looks like
See Adding a custom component to SceneBuilder 2.0 if you want to make the VerticalButtonBar visible to SceneBuilder.
As far as I can see, ButtonBar has no way to change this, so there is nothing SceneBuilder can do about it.
You might want to file a feature request for ButtonBar: http://bugs.java.com/

javafx fxml bring window to front form itself

I need to bring to front JavaFX FXML window from itself. Something like this:
procedure (boolean close)
{
if(close)
current_window.toFront();
}
How should I get this window(scene) ?
Try this
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage stage) {
Text text = new Text("!");
text.setFont(new Font(40));
VBox box = new VBox();
box.getChildren().add(text);
final Scene scene = new Scene(box,300, 250);
scene.setFill(null);
stage.setScene(scene);
stage.show();
stage.toFront();
}
public static void main(String[] args) {
launch(args);
}
}
If you have access to any of the nodes, you can use the following
((Stage)node.getScene().getWindow()).toFront();

JavaFX ListView and MouseEvent

I have a ListView and I want it to change the selected item when I click a different item. For example, I have:
result.getSelectionModel().select(0);
I want to change that .select(0) to .select(x), where x is the index on that ListView, from a mouse click. I'm not sure if I explained it correctly.
Hope you can help, thanks!
The default behaviour of a ListView is to select the item the user clicks. There is nothing you need to code to achieve this.
Just run the following sample and click on the list items and you will see the selection change.
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.stage.Stage;
public class ListViewViewer extends Application {
#Override
public void start(Stage stage) {
ListView<String> list = new ListView<>();
ObservableList<String> items = FXCollections.observableArrayList(
"Single", "Double", "Suite", "Family App");
list.setItems(items);
list.getSelectionModel().select(0);
stage.setScene(new Scene(list));
stage.show();
list.requestFocus();
}
public static void main(String[] args) {
launch(args);
}
}

Resources