Hallo, ich habe ein TableView mit einem ToggleButton in einer Spalte und in einer separaten Scene mehrere ToggleButtons verbunden als Gruppe. Auf den ToggleButtons sind Symbole abgebildet. Ich möchte nun, dass der Nutzer zwischen den verschiedenen Symbolen auswählen kann und das jeweilige Symbol dann auf dem ToggleButton in der TableView erscheint. Versucht habe ich es mit einem changeListener, bekomme aber nur eine NullPointException. Mache ich beim Listener iwas falsch oder muss ich beim Button im TableView noch etwas ergänzen? Bin für alle Tipps dankbar. Hier der Code:
TableViewButton:
Separate Scene mit ToggleButtons:
Hier noch ein Screenshot welcher es vll. besser erklärt was ich will
https://pl.vc/19nl24
TableViewButton:
Code:
/**
* Button click in column to get SymbolScene
*/
Callback<TableColumn<Points, String>, TableCell<Points, String>> cellFactory = //
new Callback<TableColumn<Points, String>, TableCell<Points, String>>() {
@Override
public TableCell call(final TableColumn<Points, String> param) {
final TableCell<Points, String> cell = new TableCell<Points, String>() {
final ToggleButton pointsSymbolButton = new ToggleButton(" ");
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
setText(null);
} else {
pointsSymbolButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(
getClass().getResource("/address/view/SymbolView.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root1));
stage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
});
setGraphic(pointsSymbolButton);
pointsSymbolButton.setStyle("-fx-border-color: transparent; -fx-border-width: 0; -fx-background-radius: 0; -fx-background-color: transparent;");
}
}
};
return cell;
}
};
pointsSymbolColumn.setCellFactory(cellFactory);
Separate Scene mit ToggleButtons:
Code:
public SymbolTableCell() {
}
public void buildGridPaneAddButtons() {
//empty the GridPane
gridPane.getChildren().clear();
//create GridPane
int numRows = 14;
int numColumns = 14;
for (int row = 0; row < numRows; row++) {
RowConstraints rc = new RowConstraints();
rc.setFillHeight(true);
rc.setVgrow(Priority.ALWAYS);
gridPane.getRowConstraints().add(rc);
}
for (int col = 0; col < numColumns; col++) {
ColumnConstraints cc = new ColumnConstraints();
cc.setFillWidth(true);
cc.setHgrow(Priority.ALWAYS);
gridPane.getColumnConstraints().add(cc);
}
String selectedFamily = listView.getSelectionModel().getSelectedItem();
if (selectedFamily != null) {
// System.out.println("selected Font: '" + selectedFamily + "'");
//Group for Buttons
ToggleGroup groupForToggleButtons = new ToggleGroup();
//create ToggleButtons raster
for (int i = 0; i < 196; i++) {
Font selectedFont = Font.font(selectedFamily, 18.0);
Character character = new Character((char) (i));
java.awt.Font awtFont = new java.awt.Font(selectedFamily, 0, 40);
// int i 9,10,13 and 32 have empty characters, so we won't
// display them in the panel
if (awtFont.canDisplay(character.charValue()) && (i != 9) && (i != 10) && (i != 13) && (i != 32)) {
// System.out.println("create Button: int='" + i + "' ==>
// character='" + character + "'");
ToggleButton buttonForSymbols = createButton("" + character);
buttonForSymbols.setToggleGroup(groupForToggleButtons);
buttonForSymbols.setFont(selectedFont);
buttonForSymbols.setAlignment(Pos.CENTER);
buttonForSymbols.setUserData("" + character);
// Create BorderPane with Character(Top), Separator(Center),
// Label(Bottom)
HBox symbolHBox = new HBox();
symbolHBox.getChildren().add(buttonForSymbols);
Label symbolLabel = new Label();
symbolLabel.setText("" + i);
Separator symbolSeparator = new Separator();
BorderPane symbolBorderPane = new BorderPane();
symbolBorderPane.setTop(symbolHBox);
symbolBorderPane.setCenter(symbolSeparator);
symbolBorderPane.setBottom(symbolLabel);
symbolLabel.setAlignment(Pos.CENTER);
symbolBorderPane.setAlignment(symbolLabel, Pos.CENTER);
// when ToggleButton selected, set new Style of BorderPane
// (red border)
symbolBorderPane.styleProperty()
.bind(Bindings.when(buttonForSymbols.selectedProperty())
.then("-fx-border-color: red; -fx-border-width: 2.5;")
.otherwise("-fx-border-color: black; -fx-border-width: 1;"));
gridPane.add(symbolBorderPane, i % numRows, i / numColumns);
groupForToggleButtons.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
public void changed(ObservableValue<? extends Toggle> ov, Toggle toggle, Toggle new_toggle) {
if (new_toggle != null)
pointsSymbolButton.setText((String) groupForToggleButtons.getSelectedToggle().getUserData());
}
});
}
}
}
}
// ToggleButton to use .then .otherwise style properties
private ToggleButton createButton(String text) {
ToggleButton buttonPanelForSymbols = new ToggleButton(text);
buttonPanelForSymbols.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
buttonPanelForSymbols.setStyle(
"-fx-border-color: transparent; -fx-border-width: 0; -fx-background-radius: 0; -fx-background-color: transparent;");
return buttonPanelForSymbols;
}
Hier noch ein Screenshot welcher es vll. besser erklärt was ich will
https://pl.vc/19nl24
Zuletzt bearbeitet: