0

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.

2
  • Are you repainting the textfield? Commented Oct 24, 2013 at 0:56
  • No, I'm trying to add textfields each time a person press the button "New Column", or more specifically, I'm trying to add hboxes each time a person presses that button. However it isn't adding those boxes to my vbox or it isn't displaying them. Commented Oct 24, 2013 at 1:09

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

I can do that, or the equivalent in JavaFX, however I'd need to add this arraylist every time a new column is entered. I don't suppose that will be a problem?
@Argus You should have tagged it as JavaFX -_-"
I forgot about that, in any case, this is still useful.
Sorry, I forgot, I was actually implementing it.
Yeah, its doing exactly what I need it do. Thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.