So I have this code here, and it is supposed to dynamically add textfields and buttons depending on how many times the user presses New Column however it is not adding anything.
newColumn.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent e){
String column = columns.getText();
columns.clear();
final HBox hbox1 = new HBox();
final TextField textField = new TextField();
textField.setText(column);
Button delete = new Button("X");
vbox.getChildren().add(hbox1);
delete.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent e){
vbox.getChildren().remove(hbox1);
}
});
}
});
This is the part of the code that is supposed to add the new buttons. Here's the rest of the code, that displays the window and everythig else:
package GUI;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Example extends Application{
/**
* #param args
*/
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
Scene scene = new Scene(new Group());
primaryStage.setTitle("Parameters");
primaryStage.setWidth(500);
primaryStage.setHeight(800);
showWindow(scene);
primaryStage.setScene(scene);
primaryStage.show();
}
public void showWindow(Scene scene){
final VBox vbox = new VBox();
final HBox hbox = new HBox();
final TextField columns = new TextField();
Button newColumn = new Button("New Column");
Button done = new Button("Done");
hbox.setSpacing(5);
hbox.getChildren().addAll(columns, newColumn);
vbox.setSpacing(5);
vbox.setPadding(new Insets(20, 0, 0, 20));
vbox.getChildren().addAll(hbox);
newColumn.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent e){
String column = columns.getText();
columns.clear();
final HBox hbox1 = new HBox();
final TextField textField = new TextField();
textField.setText(column);
Button delete = new Button("X");
vbox.getChildren().add(hbox1);
delete.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent e){
vbox.getChildren().remove(hbox1);
}
});
}
});
vbox.getChildren().addAll(done);
((Group) scene.getRoot()).getChildren().addAll(vbox);
}
}
I'm also using JavaFX, if that helps.
If you want to be dynamic in adding and removing JComponents , consider storing them in an ArrayList or Vector. In the start, there will be a default JTextField and then then user will add one after another.
In the actionPerformed() of the JButton, create a new JTextField of whatever size you want. Call invalidate() on the content pane of your JFrame and then just add whatever component you need.
Good to go !
SSCCE
package stack;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class DynamicButtons extends JFrame{
JButton addMore = new JButton("AddMore");
Container contentPane = null;
public DynamicButtons(){
contentPane = this.getContentPane();
contentPane.setLayout(new FlowLayout());
addMore.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
JButton newButton = new JButton("ABCD");
contentPane.invalidate();
contentPane.add(newButton);
pack();
}
});
contentPane.add(addMore);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run(){
new DynamicButtons();
}
});
}
}
The OP needs JavaFX
1. Get your whatever Pane you are using.
2. Call the yourPane.getChildren()
3. In the listener of the button, just add the child to the list you obtained above
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
}
I have a Label and a ProgressBar in a GridPane.
I've registered an onMouseClicked-event handler on the GridPane.
If I click on the Label the handler gets triggered.
If I click on the ProgressBar the handler doesn't get triggered.
Why? How can I fix it?
package test;
import javafx.application.Application;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;
public class ProgressBarTestApplication extends Application {
#Override
public void start(Stage primaryStage) {
GridPane gridPane = new GridPane();
ColumnConstraints columnConstraints0 = new ColumnConstraints();
columnConstraints0.setHgrow(Priority.SOMETIMES);
ColumnConstraints columnConstraints1 = new ColumnConstraints();
columnConstraints1.setHgrow(Priority.NEVER);
gridPane.getColumnConstraints().addAll(columnConstraints0, columnConstraints1);
RowConstraints rowConstraints0 = new RowConstraints();
rowConstraints0.setVgrow(Priority.SOMETIMES);
gridPane.getRowConstraints().add(rowConstraints0);
Label someLabel = new Label("Some Label:");
ProgressBar progressBar = new ProgressBar();
progressBar.setPrefWidth(250.0d);
someLabel.setLabelFor(progressBar);
gridPane.add(someLabel, 0, 0);
gridPane.add(progressBar, 1, 0);
gridPane.setCursor(Cursor.HAND);
gridPane.setHgap(5.0d);
gridPane.setOnMouseClicked(event -> System.out.println("Clicked!"));
Scene scene = new Scene(gridPane, 350, 150);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
It would seem that the MouseEvent is being consumed. I had a quick look through the ProgressBar class and it would seem that the ProgressIndicatorSkin's BehaviourBase may be causing the event to be consumed.
A quick and dirty solution would be to set the mouse clicked EventHandler for the ProgressBar to re-fire the event to the GridPane:
progressBar.setOnMouseClicked(e -> gridPane.fireEvent(e));
or to work with any Parent node:
progressBar.setOnMouseClicked(e -> progressBar.getParent().fireEvent(e));
or subclass ProgressBar to automatically apply this behavior:
public class NoConsumeProgressBar extends ProgressBar {
public NoConsumeProgressBar() {
setOnMouseClicked(e -> {
Parent parent = getParent();
if (parent != null) {
parent.fireEvent(e);
}
});
}
}
I'm just working on a little project and started to get in JavaFX.
I have a problem and I don't know any solutions..
So I managed it to bring a Scrollbar(incl Listener) into the root.
But the listener doesn't work.
I want to add many comboboxes one below the other and when I reached the scenesize(LayoutX) it should be possible to scrolldown.
How can I solve my problem?
package application;
import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.Pane;
import javafx.scene.text.*;
import javafx.stage.Stage;
import application.XMLActions;;
public class Main extends Application {
/**
* Globale Variablen
*/
int abstandszaehler = 0;
private Pane root = new Pane();
private ScrollBar sB = new ScrollBar();
private XMLActions xA = new XMLActions();
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("XML-Zeilenersteller");
primaryStage.setResizable(false);
/**
* Elemente für die Root
*/
//Buttons
Button newXMLLine = new Button();
newXMLLine.setText("Einfügen neuer XML-Zeile");
newXMLLine.setLayoutX(735);
newXMLLine.setLayoutY(80);
newXMLLine.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
setComboBox();
}
});
Button newXMLDocument = new Button();
newXMLDocument.setText("Erstelle XML-Dokument");
newXMLDocument.setLayoutX(735);
newXMLDocument.setLayoutY(550);
newXMLDocument.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Test2");
}
});
//Überschrift
Text header = new Text(105, 50, "XML Lines Creator");
header.setFont(new Font(30)); header.setStyle("-fx-underline: true;");
//Scrollbar
sB.setLayoutX(715);
sB.setLayoutY(80);
sB.setMin(0);
sB.setOrientation(Orientation.VERTICAL);
sB.setPrefHeight(500);
sB.setMax(360);
sB.setUnitIncrement(30);
sB.setBlockIncrement(35);
sB.valueProperty().addListener((ObservableValue<? extends Number> ov,
Number old_val, Number new_val) -> {
System.out.println(-new_val.doubleValue());
});
/**
* Hauptseite als Root
* Rootbearbeitungen
*/
root.setStyle("-fx-background-color: lightsteelblue");
root.getChildren().addAll(sB,newXMLDocument,header,newXMLLine );
//Scene setzen
Scene mainScene = new Scene(root, 900, 600);
primaryStage.setScene(mainScene);
primaryStage.show();
}
public void setComboBox(){
ComboBox cB = new ComboBox(xA.getList());
root.getChildren().add(cB);
cB.setLayoutX(80);
cB.setLayoutY(80 + abstandszaehler);
abstandszaehler = abstandszaehler + 30;
}
}
EDIT 1:
I got a little progress with that code in the listener:
root.setLayoutY(-new_val.doubleValue());
Replace the Pane with a ScrollPane. On the ScrollPane you can define the policy for your scrollbar.
If you define it like this, it will behave like a Pane:
ScrollPane sp = new ScrollPane();
sp.setHbarPolicy(ScrollBarPolicy.NEVER);
sp.setVbarPolicy(ScrollBarPolicy.NEVER);
Take a look at this article by Oracle.
First, if you wish to layout controls vertically consider using VBox. This VBox should then be enclosed by a ScrollPane.
If you then set the VBox's prefHeight to Control.USE_COMPUTED_SIZE and maxHeight to Double.POSITIVE_INFINITY the VBox should resize to fit it's content without limit, and the enclosing ScrollPane should show and hide scrollbars as necessary.
I have SplitPane with two AnchorPane (left and right side). There are Label, TextField and Button on each AnchorPane, they are arranged in one line. Label must bind to the left side AnchorPane, Button to the right and TextField must be stretched between them. Left AnchorPane must be bind to left part of SplitPane.
My code works, but when I move divider after some time Buttons jump off the binding with SplitPane.
AnchorPane width bind to SplitPane.Divider, Label, TextField, Button bind to AnchorPane.
Can you help me?
Sorry for my English.
import javafx.application.Application;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void start(Stage stage) {
stage.setTitle("BackUpManager");
AnchorPane root = new AnchorPane();
Scene scene = new Scene(root, 800,600);
SplitPane splitPane = new SplitPane();
splitPane.setLayoutY(50);
splitPane.prefWidthProperty().bind(root.widthProperty());
splitPane.prefHeightProperty().bind(root.heightProperty().subtract(50));
AnchorPane rRoot = new AnchorPane();
AnchorPane wRoot = new AnchorPane();
splitPane.getItems().addAll(rRoot,wRoot);
rRoot.setMinWidth(200);
rRoot.prefWidthProperty().bind(splitPane.getDividers().get(0).positionProperty());
Button rBrowse = new Button();
rRoot.getChildren().add(rBrowse);
rBrowse.setText("Browse");
DoubleBinding db0 = rRoot.widthProperty().subtract(55);
db0.addListener(new javafx.beans.value.ChangeListener<Number>() {
public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
rBrowse.setLayoutX(db0.getValue());
}});
Label rLabel = new Label("Reserve dir");
rRoot.getChildren().add(rLabel);
rLabel.setLayoutY(3);
TextField rPath = new TextField();
rRoot.getChildren().add(rPath);
rPath.setLayoutX(60);
rPath.prefWidthProperty().bind(rRoot.widthProperty().subtract(115));
wRoot.prefWidthProperty().bind(splitPane.widthProperty().subtract(splitPane.getDividers().get(0).positionProperty()));
wRoot.setMinWidth(200);
Button wBrowse = new Button();
wRoot.getChildren().add(wBrowse);
wBrowse.setText("Browse");
DoubleBinding db1 = wRoot.widthProperty().subtract(55);
db1.addListener(new javafx.beans.value.ChangeListener<Number>() {
public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
wBrowse.setLayoutX(db1.getValue());
}});
Label wLabel = new Label("Working dir");
wRoot.getChildren().add(wLabel);
wLabel.setLayoutY(3);
TextField wPath = new TextField();
wRoot.getChildren().add(wPath);
wPath.setLayoutX(64);
wPath.prefWidthProperty().bind(wRoot.widthProperty().subtract(119));
rBrowse.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle (ActionEvent e) {
System.out.println("Called");
}
});
root.getChildren().addAll(splitPane);
stage.setScene(scene);
stage.show();
}
}
I have a TextField in my program that will have data entered by the user, but I also have a variable value somewhere else that I need to permanently display at the end of my TextField. It cannot disappear when the user enters any data in the TextField. Can anyone give me a good implementation? Thanks.
[UserInput (miles)]
**Above is an example of what I am talking about. "Miles" needs to always be in the TextField while the UserInput is changing.
EDIT: "Implementation" was a bad choice of words. Let me rephrase, I can set up the field myself, but I am having trouble finding a way to set permanent text in a textfield. Just wondering if anyone knows an easy way.
You could put a transparent textfield over a label and bind the 2 together. Something like this but with better styling.
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Text extends Application {
#Override
public void start(Stage primaryStage) {
TextField txtUser = new TextField();
txtUser.setStyle("-fx-background-color: transparent;-fx-border-color:blue;");
Label txtBG = new Label(" (miles)");
Label labelUser = new Label();
labelUser.textProperty().bind(txtUser.textProperty());
Label labelAll = new Label();
labelAll.textProperty().bind(Bindings.concat(
labelUser.textProperty())
.concat(txtBG.textProperty()));
StackPane sp = new StackPane();
sp.getChildren().addAll(txtBG, txtUser);
sp.setPrefSize(100, 12);
VBox root = new VBox();
root.getChildren().addAll(sp,labelUser,labelAll);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("transparent text test");
primaryStage.setScene(scene);
primaryStage.show();
}
}
I would use a HBox instead of a stack pane but it's one way to satisfy the requirement that "miles" is 'inside' the texfield's borders.
This is a small example doing what you want ! I have used the focus property of textfield to add and remove miles from it !
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TextBinding extends Application {
#Override
public void start(Stage primaryStage) {
final TextField user = new TextField();
TextField demo = new TextField();
user.setStyle("-fx-background-color: transparent;-fx-border-color:blue;");
user.focusedProperty().addListener(new ChangeListener<Boolean>()
{
#Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue)
{
if (newPropertyValue)
{
user.setText(user.getText().replace(" miles", ""));
}
else
{
user.setText(user.getText().concat(" miles"));
}
}
});
VBox root = new VBox();
root.getChildren().addAll(user,demo);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("transparent text test");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String args[]) {
launch(args);
}
}