I am searching for how to get the selected tab from a TabPane just by clicking on a button with JavaFX. I've tried to do it with the ChangeListener but it doesn't work yet.
So how can I do that?
As with many JavaFX controls there is a SelectionModel to get first.
tabPane.getSelectionModel().getSelectedItem(); To get the currently selected tab, or alternatively, getSelectedIndex() for the index of the selected tab.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TabSelect extends Application {
#Override
public void start(Stage primaryStage) {
TabPane tabPane = new TabPane();
tabPane.getTabs().addAll(new Tab("tab 1"),
new Tab("tab 2"),
new Tab("tab 3"));
Button btn = new Button();
btn.setText("Which tab?");
Label label = new Label();
btn.setOnAction((evt)-> {
label.setText(tabPane.getSelectionModel().getSelectedItem().getText());
});
VBox root = new VBox(10);
root.getChildren().addAll(tabPane, btn, label);
Scene scene = new Scene(root, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
//you can also watch the selectedItemProperty
tabPane.getSelectionModel().selectedItemProperty().addListener((obs,ov,nv)->{
primaryStage.setTitle(nv.getText());
});
}
public static void main(String[] args) { launch(args); }
}
tabPane.getSelectionModel().getSelectedIndex().
Related
The buttons dont give a console output. Two buttons are in a Vbox at the bottom of a BorderPane and should print "new" or "continue" when pressed.
I followed a tutorial and tried to extend it to one more button.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application implements EventHandler<ActionEvent> {
Button btn1, btn2;
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("title");
Button btn1 = new Button("new");
Button btn2 = new Button ("continue");
btn1.setOnAction(this);
btn2.setOnAction(this);
VBox vb = new VBox (btn1, btn2);
vb.setSpacing(10);
vb.setPadding(new Insets(20));
BorderPane root = new BorderPane();
root.setBottom(vb);
Scene scene = new Scene (root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
#Override
public void handle(ActionEvent event) {
if (event.getSource()==btn1) {
System.out.println("new!");
} else if (event.getSource()==btn2) {
System.out.println("continue!");
}
}
}
nothing happens, but there should be a output of "new or continue"
Remove class from buttons.
This way the code will use buttons from fields, instead of creating new ones.
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("title");
btn1 = new Button("new");
btn2 = new Button ("continue");
// the rest of the code will be the same
}
As stated in the title, I have fxml files, I have a UI that is set up with three labels/buttons up top and the lower half of the window has a pane. Every time a label/button is clicked, the pane must switch to that corresponding fxml file. So in other words, the pane must always be in the same position, kind of like a tabbed layout but without tabs.
I know I can achieve this with just loading a new instance of an fxml file but, I want to avoid that because when a user click on a tab he previously was on, he should be able to see his earlier input.
I have some main.java that starts the program. Some controller.java that controls the UI when it is first loaded, and some fxml file corresponding to that initial view. How can I go about implementing this transition functionality? P.S. I am very novice at JavaFX.
Here is a MCVE of how you can achieve it.
It can of course be implemented using FXML :
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class StageTest extends Application{
private Pane pane1, pane2, mainPane;
#Override
public void start(Stage stage) throws Exception {
stage.setTitle("Switch Panes");
Button button1 = new Button("Show Pane 1");
button1.setOnAction(e -> showPane1());
Button button2 = new Button("Show Pane 2");
button2.setOnAction(e -> showPane2());
HBox buttonsPane = new HBox(5.);
buttonsPane.getChildren().addAll(button1, button2);
pane1 = getPane("PANE ONE");
pane2 = getPane("PANE TWO");
mainPane = new StackPane(pane1);
BorderPane root = new BorderPane();
root.setTop(buttonsPane);
root.setCenter(mainPane);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
private void showPane1() {
mainPane.getChildren().clear();
mainPane.getChildren().add(pane1);
}
private void showPane2() {
mainPane.getChildren().clear();
mainPane.getChildren().add(pane2);
}
private Pane getPane(String txt) {
VBox pane = new VBox();
pane.getChildren().addAll(new TextArea(txt+" add text here: "));
return pane;
}
public static void main(String[] args) {
launch(args);
}
}
HiEveryone,
I'm new in JavaFx and my TextField shows at the middle of the frame but not at the top's position. Where am i suppose to be wrong? pls help..
Have a look into this screenshot..
But i want to set that TextField at the top ..
Here is Source code:
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.scene.control.TextField;
public class Main extends Application {
#Override
public void start (Stage primaryStage){
primaryStage.setTitle("Modern web browser.");
WebView wv = new WebView();
WebEngine we = wv.getEngine();
we.load("http://www.google.com/");
TextField tf = new TextField("http://www.google.com/");
tf.setMaxHeight(Double.MIN_VALUE);
GridPane sp = new GridPane();
sp.getChildren().add(wv);
sp.getChildren().add(tf);
Scene scene = new Scene(sp, 600, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Please help.. Thanks in Advanced!
EDITED
As #Nagesh Kumar answered me after using VBox getting this error:
Change your layout to VBox from GridPane as follows
VBox vbox = new VBox();
Now first add your TextField first so that it will be on top as follows
vbox.getChildren().add(tf);
then a WebView as follows
vbox.getChildren().add(wv);
Edit:
Yes you can try this
VBox vbox = new VBox(5);
5 pixel space between controls put in VBox
HiEveryone,
I'm new Javafx and trying to change the float of this button as to Right side from the current Left side.. How to do that?
I'm using simply Pane as a container at the moment.
Have a look into this screenshot:
How to do that? ..pls help
Thanks in advance!
EDITED
Source code:
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
public class Main extends Application {
#Override
public void start (Stage primaryStage){
primaryStage.setTitle("Modern web browser made by Rajendra arora.");
WebView wv = new WebView();
wv.setLayoutX(0.0);
wv.setLayoutY(40.0);
wv.setPrefHeight(Double.MAX_EXPONENT);
wv.setPrefWidth(Double.MAX_EXPONENT);
WebEngine we = wv.getEngine();
we.load("http://www.google.com/");
Button btn=new Button("Go");
TextField tf = new TextField("http://www.google.com/");
tf.setLayoutX(1.0);
tf.setPrefWidth(Double.MAX_EXPONENT);
tf.setPrefHeight(25.0);
Pane sp = new Pane();
sp.getChildren().add(tf);
sp.getChildren().add(btn);
sp.getChildren().add(wv);
Scene scene = new Scene(sp, 600, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Use a HBox to hold the textfield and button, later add it to the Pane
HBox hbox = new HBox();
hbox.getChildren().addAll(tf, btn);
Pane sp = new Pane();
sp.getChildren().add(hbox);
sp.getChildren().add(wv);
I would like to align my button so that the first button is on the left of the screen and the other two are on the right. I am currently using HBox to try to position them but i cannot seem to able to figure out how to lay them out properly. The code below is what i am using now.
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class ButtonTest extends Application {
private Button min, close, openfile;
public static void main(String[] args){
launch(args);
}
#Override
public void start(final Stage stage) throws Exception {
stage.setTitle("Button Test");
Group root = new Group();
BorderPane borderpane = new BorderPane();
setUpButtons();
HBox hbox = new HBox();
hbox.setSpacing(10);
hbox.getChildren().add(openfile);
HBox hbox1 = new HBox();
hbox1.setAlignment(Pos.CENTER_RIGHT);
hbox1.getChildren().addAll(min, close);
hbox.getChildren().add(hbox1);
HBox.setHgrow(hbox1, Priority.ALWAYS);
borderpane.setTop(hbox);
root.getChildren().add(borderpane);
Scene scene = new Scene(root,800,600);
stage.setFullScreen(true);
scene.getStylesheets().add("button.css");
stage.setScene(scene);
stage.show();
}
private void setUpButtons() {
close = new Button("x");
close.setId("closeBtn");
min = new Button("_");
min.setId("minBtn");
openfile = new Button("Open file");
openfile.setId("openFileBtn");
}
}
Any help would be appreciated
Thanks
Set the aligment of your outer HBox (hbox) to LEFT, and the aligment of your inner HBox (hbox1) to RIGHT.
Then you can have outer content on the left, and inner content on the right.
*EDIT: got your problem now. Stop using Group and add the borderpane directly to the Scene:
Scene scene = new Scene(borderpane,800,600);