Created
November 13, 2022 21:41
-
-
Save sedj601/35fa30ac59dff76c2631e7686e9da8b2 to your computer and use it in GitHub Desktop.
Stackoverflow question: https://stackoverflow.com/questions/74421996/javafx-multiple-choice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.LocalDate; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javafx.application.Application; | |
import javafx.geometry.Pos; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Label; | |
import javafx.scene.layout.ColumnConstraints; | |
import javafx.scene.layout.GridPane; | |
import javafx.scene.layout.RowConstraints; | |
import javafx.scene.layout.StackPane; | |
import javafx.stage.Stage; | |
public class App extends Application { | |
@Override | |
public void start(Stage primaryStage) { | |
LocalDate localDate = LocalDate.now(); | |
GridPane gpRoot = new GridPane(); | |
gpRoot.setHgap(3); | |
gpRoot.setVgap(3); | |
List<User> users = new ArrayList(); | |
users.add(new User("John Doe")); | |
users.add(new User("Kim Doe")); | |
users.add(new User("Jane Doe")); | |
generateHeader(localDate, gpRoot); | |
for(int i = 0; i < users.size(); i++) | |
{ | |
generateRow(localDate, users.get(i), gpRoot, i + 1); | |
} | |
StackPane root = new StackPane(gpRoot); | |
Scene scene = new Scene(root); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
public void generateRow(LocalDate localDate, User user, GridPane gpRoot, int row) | |
{ | |
Label lblName = new Label(user.getUserName()); | |
lblName.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); | |
lblName.setAlignment(Pos.CENTER); | |
gpRoot.add(lblName, 0, row); | |
for(int i = 0; i < localDate.lengthOfMonth(); i++) | |
{ | |
int day = i + 1; | |
StackPane stackPane = new StackPane(); | |
stackPane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); | |
stackPane.setOnMouseClicked((t) -> { | |
if(stackPane.getStyle().isEmpty()) | |
{ | |
stackPane.setStyle("-fx-background-color: lightblue;"); | |
LocalDate selectedDate = LocalDate.of(localDate.getYear(), localDate.getMonth(), day); | |
user.getSelectedDates().add(selectedDate); | |
System.out.print(user.getUserName() + ": \t"); | |
user.getSelectedDates().forEach(date -> System.out.print(date + "\t")); | |
System.out.println(); | |
} | |
else | |
{ | |
stackPane.setStyle(""); | |
LocalDate selectedDate = LocalDate.of(localDate.getYear(), localDate.getMonth(), day); | |
user.getSelectedDates().remove(selectedDate); | |
System.out.print(user.getUserName() + ": \t"); | |
user.getSelectedDates().forEach(date -> System.out.print(date + "\t")); | |
System.out.println(); | |
} | |
}); | |
gpRoot.add(stackPane, day, row); | |
} | |
} | |
public void generateHeader(LocalDate localDate, GridPane gpRoot) | |
{ | |
gpRoot.getColumnConstraints().add(new ColumnConstraints(100)); | |
gpRoot.getRowConstraints().add(new RowConstraints(25)); | |
Label lblDate = new Label("Date: "); | |
lblDate.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); | |
lblDate.setAlignment(Pos.CENTER); | |
gpRoot.add(lblDate, 0, 0); | |
for(int i = 0; i < localDate.lengthOfMonth(); i++) | |
{ | |
gpRoot.getColumnConstraints().add(new ColumnConstraints(25)); | |
Label lblNumberDate = new Label(Integer.toString(i + 1)); | |
lblNumberDate.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); | |
lblNumberDate.setAlignment(Pos.CENTER); | |
lblNumberDate.setStyle("-fx-background-color: yellow;"); | |
gpRoot.add(lblNumberDate, i + 1, 0); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.LocalDate; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* | |
* @author Sed | |
*/ | |
public class User { | |
private String name; | |
private List<LocalDate> selectedDates = new ArrayList(); | |
public User(String name) { | |
this.name = name; | |
} | |
public String getUserName() { | |
return name; | |
} | |
public void setUserName(String name) { | |
this.name = name; | |
} | |
public List<LocalDate> getSelectedDates() { | |
return selectedDates; | |
} | |
public void setSelectedDates(List<LocalDate> selectedDates) { | |
this.selectedDates = selectedDates; | |
} | |
public void addSelecterdDate(LocalDate localDate) | |
{ | |
this.selectedDates.add(localDate); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment