在TableView列中创建RadioButton

时间:2014-12-14 22:57:56

标签: javafx tableview

我遵循了Oracle文档中的tutorial about TableView,我想做同样的事情,但是我想要显示一个RadioButton,而不是显示TextField来修改项目。 (我在其上创建了带有RadionButton的TableView)

3 个答案:

答案 0 :(得分:6)

我在https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/table-view.htm使用了教程(参见在表格中编辑数据)并对其进行了扩展。作为值的基础,应该通过单选按钮设置,我假设一个枚举。

在我的示例中,我使用枚举Person扩展了Participation类(表明列表中的人是否参加了虚拟事件)......

public static enum Participation {
    YES,
    NO,
    MAYBE;

    public String toString() {
        return super.toString().toLowerCase();
    };
}

...

public static class Person {

    private final SimpleStringProperty firstName;
    private final SimpleStringProperty lastName;
    private final SimpleStringProperty email;
    private final SimpleObjectProperty<Participation> participation;

...

我实施了一个RadioButtonCell,它采用了仲裁EnumSet<T>。因此,您可以将它用于每个Enumeration和每个TableColumn,它们应该包含RadioButtons。

public static class RadioButtonCell<S,T extends Enum<T>> extends TableCell<S,T>{

    private EnumSet<T> enumeration;

    public RadioButtonCell(EnumSet<T> enumeration) {
        this.enumeration = enumeration;
    }

    @Override
    protected void updateItem(T item, boolean empty)
    {
        super.updateItem(item, empty);
        if (!empty) 
        {
            // gui setup
            HBox hb = new HBox(7);
            hb.setAlignment(Pos.CENTER);
            final ToggleGroup group = new ToggleGroup();

            // create a radio button for each 'element' of the enumeration
            for (Enum<T> enumElement : enumeration) {
                RadioButton radioButton = new RadioButton(enumElement.toString());
                radioButton.setUserData(enumElement);
                radioButton.setToggleGroup(group);
                hb.getChildren().add(radioButton);
                if (enumElement.equals(item)) {
                    radioButton.setSelected(true);
                }
            }

            // issue events on change of the selected radio button
            group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {

                @SuppressWarnings("unchecked")
                @Override
                public void changed(ObservableValue<? extends Toggle> observable,
                        Toggle oldValue, Toggle newValue) {
                    getTableView().edit(getIndex(), getTableColumn());
                    RadioButtonCell.this.commitEdit((T) newValue.getUserData());
                }
            });
            setGraphic(hb);
        } 
    }
}

您现在必须调整特定TableColumn的CellFactory

 participationColumn.setCellFactory((param) -> new RadioButtonCell<Person, Participation>(EnumSet.allOf(Participation.class)));

最后像往常一样更新提交数据的实际值:

participationColumn.setCellValueFactory(new PropertyValueFactory<Person, Participation>("participation"));
    participationColumn.setOnEditCommit(
            new EventHandler<CellEditEvent<Person, Participation>>() {
                @Override
                public void handle(CellEditEvent<Person, Participation> t) {
                    ((Person) t.getTableView().getItems().get(
                        t.getTablePosition().getRow())
                        ).setParticipation(t.getNewValue());
                }
            }
        );

Result

答案 1 :(得分:1)

gkri,谢谢你的代码!这非常有用。我还有一个评论:当然新的SimpleObjectProperty需要它的get&amp;设定方法。如果没有getter,表将无法正确更新,尤其是在排序时,或者在使用TreeTableView时,在展开或折叠节点时:

public void setParticipation(Participation p){
            participation.set(p);
        }

public Participation getParticipation(){
            return participation.get();
        }

所以完整的代码示例:

import java.util.EnumSet;

import javafx.application.Application;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class TableViewSample extends Application {

    private final TableView<Person> table = new TableView<>();
    private final ObservableList<Person> data =
            FXCollections.observableArrayList(
            new Person("Jacob", "Smith", "jacob.smith@example.com",Participation.MAYBE),
            new Person("Isabella", "Johnson", "isabella.johnson@example.com",Participation.MAYBE),
            new Person("Ethan", "Williams", "ethan.williams@example.com",Participation.MAYBE),
            new Person("Emma", "Jones", "emma.jones@example.com",Participation.MAYBE),
            new Person("Michael", "Brown", "michael.brown@example.com",Participation.MAYBE));
    final HBox hb = new HBox();

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Table View Sample");
        stage.setWidth(650);
        stage.setHeight(550);

        final Label label = new Label("Address Book");
        label.setFont(new Font("Arial", 20));

        table.setEditable(true);
        table.setMinWidth(640);

        TableColumn<Person,String> firstNameCol = new TableColumn("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(
                new PropertyValueFactory<>("firstName"));

        TableColumn<Person,String>lastNameCol = new TableColumn("Last Name");
        lastNameCol.setMinWidth(100);
        lastNameCol.setCellValueFactory(
                new PropertyValueFactory<>("lastName"));

        TableColumn<Person,String> emailCol = new TableColumn("Email");
        emailCol.setMinWidth(200);
        emailCol.setCellValueFactory(
                new PropertyValueFactory<>("email"));

        TableColumn<Person,Participation> participationColumn = new TableColumn("Participation");
        participationColumn.setCellFactory((param) -> new RadioButtonCell<Person, Participation>(EnumSet.allOf(Participation.class)));
        participationColumn.setCellValueFactory(new PropertyValueFactory<Person, Participation>("participation"));
        participationColumn.setOnEditCommit(
                new EventHandler<CellEditEvent<Person, Participation>>() {
                    @Override
                    public void handle(CellEditEvent<Person, Participation> t) {
                        ((Person) t.getTableView().getItems().get(
                            t.getTablePosition().getRow())
                            ).setParticipation(t.getNewValue());
                    }
                }
            );


        table.setItems(data);
        table.getColumns().addAll(firstNameCol, lastNameCol, emailCol, participationColumn );

        final TextField addFirstName = new TextField();
        addFirstName.setPromptText("First Name");
        addFirstName.setMaxWidth(firstNameCol.getPrefWidth());
        final TextField addLastName = new TextField();
        addLastName.setMaxWidth(lastNameCol.getPrefWidth());
        addLastName.setPromptText("Last Name");
        final TextField addEmail = new TextField();
        addEmail.setMaxWidth(emailCol.getPrefWidth());
        addEmail.setPromptText("Email");

        final Button addButton = new Button("Add");
        addButton.setOnAction((ActionEvent e) -> {
            data.add(new Person(
                    addFirstName.getText(),
                    addLastName.getText(),
                    addEmail.getText(),
                    Participation.NO
                    ));
            addFirstName.clear();
            addLastName.clear();
            addEmail.clear();
        });

        hb.getChildren().addAll(addFirstName, addLastName, addEmail, addButton);
        hb.setSpacing(3);

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(label, table, hb);

        ((Group) scene.getRoot()).getChildren().addAll(vbox);

        stage.setScene(scene);
        stage.show();
    }


    public static enum Participation {
        YES,
        NO,
        MAYBE;

        public String toString() {
            return super.toString().toLowerCase();
        };
    }

    public static class Person {

        private final SimpleStringProperty firstName;
        private final SimpleStringProperty lastName;
        private final SimpleStringProperty email;
        private final SimpleObjectProperty<Participation> participation = new SimpleObjectProperty<Participation>();

        private Person(String fName, String lName, String email, Participation p ) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
            this.email = new SimpleStringProperty(email);
            this.participation.setValue(p);
        }

        public String getFirstName() {
            return firstName.get();
        }

        public void setFirstName(String fName) {
            firstName.set(fName);
        }

        public String getLastName() {
            return lastName.get();
        }

        public void setLastName(String fName) {
            lastName.set(fName);
        }

        public String getEmail() {
            return email.get();
        }

        public void setEmail(String fName) {
            email.set(fName);
        }

        public void setParticipation(Participation p){
            participation.set(p);
        }

        public Participation getParticipation(){
            return participation.get();
        }
    }





    public static class RadioButtonCell<S,T extends Enum<T>> extends TableCell<S,T>{

        private EnumSet<T> enumeration;

        public RadioButtonCell(EnumSet<T> enumeration) {
            this.enumeration = enumeration;
        }

        @Override
        protected void updateItem(T item, boolean empty)
        {
            super.updateItem(item, empty);
            if (!empty) 
            {
                // gui setup
                HBox hb = new HBox(7);
                hb.setAlignment(Pos.CENTER);
                final ToggleGroup group = new ToggleGroup();

                // create a radio button for each 'element' of the enumeration
                for (Enum<T> enumElement : enumeration) {
                    RadioButton radioButton = new RadioButton(enumElement.toString());
                    radioButton.setUserData(enumElement);
                    radioButton.setToggleGroup(group);
                    hb.getChildren().add(radioButton);
                    if (enumElement.equals(item)) {
                        radioButton.setSelected(true);
                    }
                }

                // issue events on change of the selected radio button
                group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {

                    @SuppressWarnings("unchecked")
                    @Override
                    public void changed(ObservableValue<? extends Toggle> observable,
                            Toggle oldValue, Toggle newValue) {
                        getTableView().edit(getIndex(), getTableColumn());
                        RadioButtonCell.this.commitEdit((T) newValue.getUserData());
                    }
                });
                setGraphic(hb);
            } 
        }
    }
} 

答案 2 :(得分:0)

在示例中12-8 Editing a Table Cell模仿它 在那里定义EditingCell(在底部),但只需用RadioButton替换TextField。

如果您需要进一步的帮助,请在此答案下撰写评论。

快乐编码,
Kalasch