使用JavaFX如何将组合框初始化为特定的选择?
我目前正在使用JavaFX进行一个项目,其中我有3个使用JavaFX如何将组合框初始化为特定的选择?,javafx,combobox,Javafx,Combobox,我目前正在使用JavaFX进行一个项目,其中我有3个ComboBoxes。一个用于选择和显示小时,另一个用于显示分钟,第三个用于选择AM或PM 当选择一个组合框es中的值时,它会在我的SQL数据库中输入一个时间戳。我现在要做的是在窗口打开时显示一个选择,使用initialize方法,使用一个索引,目标是从数据库中的选择中获取我的时间戳,并使用数据库中的时间设置组合框es 我试过: StartCB.getSelectionModel().setSelectedIndex(1); 及 希望当我打开
ComboBox
es。一个用于选择和显示小时,另一个用于显示分钟,第三个用于选择AM或PM
当选择一个组合框es中的值时,它会在我的SQL数据库
中输入一个时间戳。我现在要做的是在窗口打开时显示一个选择,使用initialize方法,使用一个索引,目标是从数据库中的选择中获取我的时间戳,并使用数据库中的时间设置组合框
es
我试过:
StartCB.getSelectionModel().setSelectedIndex(1);
及
希望当我打开窗户时,数值会显示出来
有什么想法吗?我已从下面的类中复制了我的代码。谢谢你,
杰夫
/**
*FXML控制器类
*
*@jdhinkle
*/
公共类ModifyAppointmentController实现可初始化{
@FXML
私人桌面视图客户;
@FXML
私有表列CustomerId;
@FXML
私有表列客户名称;
@FXML
私有文本字段标题;
@FXML
私有文本字段描述;
@FXML
私有文本字段位置;
@FXML
私人TextField联系人;
@FXML
私有文本字段URL;
@FXML
私有文本字段启动;
@FXML
私有文本字段端;
@FXML
私人按钮保存;
@FXML
私人按钮取消;
@FXML
专用日期选择器开始日期;
@FXML
私有日期选择器EndDate;
@FXML
专用组合框StartCB;
@FXML
专用组合框StartCB1;
@FXML
专用组合框StartCB2;
@FXML
专用组合框EndCB;
@FXML
专用组合框EndCB1;
@FXML
专用组合框EndCB2;
私有对象客户;
私人物品预约;
私有静态客户选择Customera;
公共静态无效设置SelectedCustomera(客户选择){
SelectedCustomerA=已选择;
}
选择私人静态预约;
公共静态无效设置选定应用程序(选定的应用程序){
selectedappoims=已选择;
}
私有静态登录选择登录;
公共静态无效设置选定应用程序(已选择登录){
SelectedLogin=Selected;
}
私有本地时间开始;
/**
*初始化控制器类。
*/
@凌驾
公共void初始化(URL、ResourceBundle rb){
Title.setText(selectedappoint.getTitle());
Description.setText(selectedappoint.getDescription());
Location.setText(selectedappoint.getLocation());
setText(LoginScreenController.getContact());
URL.setText(selectedappoint.getURL());
LocalDateTime startD=LocalDateTime.parse(selectedappoint.getStart().toString());
//StartCB.getSelectionModel().setSelectedIndex(1);
//StartCB1.getSelectionModel().selectFirst();
//StartCB2.getSelectionModel().selectFirst();
//如果(startD.getHour()>=12){
//布尔小时(真);
//小时=StartCB2.getSelectionModel().selectLast();
// }
//Start.setText(startD.toLocalTime().toString());
StartDate.setValue(startD.toLocalDate());
//End.setText(selectedappoint.getEnd());
LocalDateTime endD=LocalDateTime.parse(selectedappoint.getEnd().toString());
//End.setText(endD.toLocalTime().toString());
setValue(endD.toLocalDate());
System.out.println(selectedappoint.getTitle());
CustomerId.setCellValueFactory(新属性ValueFactory(“CustomerId”);
CustomerName.setCellValueFactory(新属性ValueFactory(“CustomerName”);
Customers.getItems().setAll(CustomerTab());
Customers.getSelectionModel().SelectEditeProperty().addListener(
(可观察、旧值、新值)->SetSelectedCustomerA(新值));
Customers.getSelectionModel().select(indexCust());
ObservableList小时数=FXCollections.observableArrayList();
对于(int i=1;i LocalDateTime-->ZonedDateTime(系统默认值)--的分区SameInstance(转换为UTC)--->ZonedDateTime到LocalDateTime---->TimeStsmp
试一试{
PreparedStatement语句1=DataBaseC.getConnection().prepareStatement(“更新约会集约会.customerId=?其中约会.appointmentId=?”;
语句1.setString(1,SelectedCustomerA.getCustomerID());
语句1.setString(2,selectedAppoint.getAppointId());
语句1.executeUpdate();
PreparedStatement语句2=DataBaseC.getConnection().prepareStatement(“更新约会集约会.title=?其中约会ID=?”;
语句2.setString(1,Title.getText());
语句2.setString(2,selectedAppoint.getAppointId());
语句2.executeUpdate();
PreparedStatement语句3=DataBaseC.getConnection().prepareStatement(“更新约会集约会.description=?其中约会ID=?”;
语句3.setString(1,Description.getText());
语句3.setString(2,selectedAppoint.getAppointId());
语句3.executeUpdate();
PreparedStatement语句4=DataBaseC.getConnection().prepareStatement(“更新约会集约会。位置=?其中约会ID=?”;
语句4.setString(1,Location.getText());
语句4.setString(2,selectedAppoint.getAppointId());
语句4.executeUpdate();
PreparedStatement语句5=DataBaseC.getConnection().prepareStatement(“更新约会集约会。联系人=?其中约会ID=?”;
语句5.setString(1,Contact.getText());
语句5.setString(2,selectedAppoint.getAppointId());
语句5.ExecuteUpdate
StartCB1.getSelectionModel().selectFirst();
/**
* FXML Controller class
*
* @author jdhinkle
*/
public class ModifyAppointmentController implements Initializable {
@FXML
private TableView<Customer> Customers;
@FXML
private TableColumn<Customer, String> CustomerId;
@FXML
private TableColumn<Customer, String> CustomerName;
@FXML
private TextField Title;
@FXML
private TextField Description;
@FXML
private TextField Location;
@FXML
private TextField Contact;
@FXML
private TextField URL;
@FXML
private TextField Start;
@FXML
private TextField End;
@FXML
private Button Save;
@FXML
private Button Cancel;
@FXML
private DatePicker StartDate;
@FXML
private DatePicker EndDate;
@FXML
private ComboBox StartCB;
@FXML
private ComboBox StartCB1;
@FXML
private ComboBox StartCB2;
@FXML
private ComboBox EndCB;
@FXML
private ComboBox EndCB1;
@FXML
private ComboBox EndCB2;
private Object customer;
private Object appointment;
private static Customer SelectedCustomerA;
public static void SetSelectedCustomerA(Customer Selected) {
SelectedCustomerA = Selected;
}
private static Appointments SelectedAppointment;
public static void SetSelectedAppointment(Appointments Selected) {
SelectedAppointment = Selected;
}
private static Login SelectedLogin;
public static void SetSelectedAppointment(Login Selected) {
SelectedLogin = Selected;
}
private LocalTime startD;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
Title.setText(SelectedAppointment.getTitle());
Description.setText(SelectedAppointment.getDescription());
Location.setText(SelectedAppointment.getLocation());
Contact.setText(LoginScreenController.getContact());
URL.setText(SelectedAppointment.getURL());
LocalDateTime startD = LocalDateTime.parse(SelectedAppointment.getStart().toString());
// StartCB.getSelectionModel().setSelectedIndex(1);
// StartCB1.getSelectionModel().selectFirst();
// StartCB2.getSelectionModel().selectFirst();
// if (startD.getHour() >= 12 ){
// boolean hour(true);
// hour = StartCB2.getSelectionModel().selectLast();
// }
// Start.setText(startD.toLocalTime().toString());
StartDate.setValue(startD.toLocalDate());
//End.setText(SelectedAppointment.getEnd());
LocalDateTime endD = LocalDateTime.parse(SelectedAppointment.getEnd().toString());
// End.setText(endD.toLocalTime().toString());
EndDate.setValue(endD.toLocalDate());
System.out.println(SelectedAppointment.getTitle());
CustomerId.setCellValueFactory(new PropertyValueFactory<>("customerID"));
CustomerName.setCellValueFactory(new PropertyValueFactory<>("customerName"));
Customers.getItems().setAll(CustomerTab());
Customers.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> SetSelectedCustomerA(newValue));
Customers.getSelectionModel().select(indexCust());
ObservableList<Integer> hours = FXCollections.observableArrayList();
for (int i = 1; i <= 12; i++) {
hours.add(i);
}
StartCB.setItems(hours);
EndCB.setItems(hours);
//StartCB.getSelectionModel().setSelectedIndex(1);
ObservableList<String> minutes = FXCollections.observableArrayList();
minutes.add("00");
minutes.add("15");
minutes.add("30");
minutes.add("45");
StartCB1.setItems(minutes);
EndCB1.setItems(minutes);
ObservableList<String> ampm = FXCollections.observableArrayList();
ampm.add("AM");
ampm.add("PM");
StartCB2.setItems(ampm);
EndCB2.setItems(ampm);
}
public LocalTime STimeSet() {
LocalTime StartTimeV = null;
int n1 = (int) StartCB.getSelectionModel().getSelectedItem();
if (StartCB2.getSelectionModel().getSelectedIndex() == 1) {
n1 = n1 + 12;
}
int index = StartCB1.getSelectionModel().getSelectedIndex();
int minutes = 0;
switch (index) {
case 0: minutes = 0; break;
case 1: minutes = 15; break;
case 2: minutes = 30; break;
case 3: minutes = 45; break;
default: minutes = 0;
}
StartTimeV = LocalTime.of(n1, minutes);
return StartTimeV;
}
public LocalTime ETimeSet() {
LocalTime EndTimeV = null;
int n1 = (int) EndCB.getSelectionModel().getSelectedItem();
if (EndCB2.getSelectionModel().getSelectedIndex() == 1) {
n1 = n1 + 12;
}
int index = EndCB1.getSelectionModel().getSelectedIndex();
int minutes = 0;
switch (index) {
case 0: minutes = 0; break;
case 1: minutes = 15; break;
case 2: minutes = 30; break;
case 3: minutes = 45; break;
default: minutes = 0;
}
EndTimeV = LocalTime.of(n1, minutes);
return EndTimeV;
}
// public LocalTime STimeCB() {
// LocalTime StartTimeCB = SelectedAppointment.getStart();
// }
//public LocalTime STimeSetCB () {
// LocalTime StartTimeVCB = startD;
//}
public int indexCust() {
ObservableList<Customer> alist = CustomerTab();
int index = -1;
for (Customer c : alist) {
//System.out.println(c.getCustomerID() + " " + SelectedAppointment.getCustomerID() );
index++;
if (c.getCustomerID().equals(SelectedAppointment.getCustomerID())) {
return index;
}
}
System.out.println("Customer ID not found");
return -1;
}
public ObservableList<Customer> CustomerTab() {
SimpleStringProperty customerID = new SimpleStringProperty("ID");
SimpleStringProperty customerName = new SimpleStringProperty("name");
SimpleStringProperty active = new SimpleStringProperty("active");
SimpleStringProperty address = new SimpleStringProperty("address");
SimpleStringProperty address2 = new SimpleStringProperty("address2");
SimpleStringProperty city = new SimpleStringProperty("city");
SimpleStringProperty postalCode = new SimpleStringProperty("zipCode");
SimpleStringProperty country = new SimpleStringProperty("country");
SimpleStringProperty phone = new SimpleStringProperty("phone");
ObservableList<Customer> customerTab = FXCollections.observableArrayList();
try {
PreparedStatement statement = DataBaseC.getConnection().prepareStatement("SELECT customer.customerId, customer.customerName, customer.active, address.address, address.address2, address.addressId, city.city, city.cityId, address.postalCode, country.country, country.countryId, address.phone FROM customer, address, city, country WHERE address.addressId = customer.addressId AND city.cityId = address.cityId AND city.countryId = country.countryId;");
ResultSet rs = statement.executeQuery();
// ResultSetMetaData rsmd = rs.getMetaData();
System.out.println(rs.toString());
while (rs.next()) {
customerID = new SimpleStringProperty(rs.getString("customer.customerId"));
customerName = new SimpleStringProperty(rs.getString("customer.customerName"));
active = new SimpleStringProperty(rs.getString("customer.active"));
address = new SimpleStringProperty(rs.getString("address.address"));
address2 = new SimpleStringProperty(rs.getString("address.address2"));
city = new SimpleStringProperty(rs.getString("city.city"));
postalCode = new SimpleStringProperty(rs.getString("address.postalCode"));
country = new SimpleStringProperty(rs.getString("country.country"));
phone = new SimpleStringProperty(rs.getString("address.phone"));
SimpleStringProperty addressId = new SimpleStringProperty(rs.getString("address.addressId"));
SimpleStringProperty cityId = new SimpleStringProperty(rs.getString("city.cityId"));
SimpleStringProperty countryId = new SimpleStringProperty(rs.getString("country.countryId"));
customerTab.add(new Customer(customerID, customerName, active, address, address2, addressId, city, cityId, postalCode, country, countryId, phone));
}
} catch (SQLException sqe) {
System.out.println("Check your customer SQL");
} catch (Exception e) {
System.out.println("Something besides the SQL went wrong.");
}
return customerTab;
}
@FXML
private void SaveAction(ActionEvent event) throws IOException {
if (Title.getText().trim().isEmpty()
|| Description.getText().trim().isEmpty()
|| Location.getText().trim().isEmpty()
|| Contact.getText().trim().isEmpty()
|| URL.getText().trim().isEmpty()
|| Start.getText().trim().isEmpty()
|| End.getText().trim().isEmpty()) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Empty Fields");
alert.setHeaderText("Empty Fields");
alert.setContentText("One or more fields are empty. Please fill out all fields");
alert.showAndWait();
return;
}
//Time from String --> LocalDateTime ---> ZonedDateTime(System Default) --Zoned SameInstance of (convert to UTC) ---> ZonedDateTime to LocalDateTime ---> TimeStsmp
try {
PreparedStatement statement1 = DataBaseC.getConnection().prepareStatement("UPDATE appointment SET appointment.customerId = ? WHERE appointment.appointmentId = ?");
statement1.setString(1, SelectedCustomerA.getCustomerID());
statement1.setString(2, SelectedAppointment.getAppointmentID());
statement1.executeUpdate();
PreparedStatement statement2 = DataBaseC.getConnection().prepareStatement("UPDATE appointment SET appointment.title = ? WHERE appointmentId = ?");
statement2.setString(1, Title.getText());
statement2.setString(2, SelectedAppointment.getAppointmentID());
statement2.executeUpdate();
PreparedStatement statement3 = DataBaseC.getConnection().prepareStatement("UPDATE appointment SET appointment.description = ? WHERE appointmentId = ?");
statement3.setString(1, Description.getText());
statement3.setString(2, SelectedAppointment.getAppointmentID());
statement3.executeUpdate();
PreparedStatement statement4 = DataBaseC.getConnection().prepareStatement("UPDATE appointment SET appointment.location = ? WHERE appointmentId = ?");
statement4.setString(1, Location.getText());
statement4.setString(2, SelectedAppointment.getAppointmentID());
statement4.executeUpdate();
PreparedStatement statement5 = DataBaseC.getConnection().prepareStatement("UPDATE appointment SET appointment.contact = ? WHERE appointmentId = ?");
statement5.setString(1, Contact.getText());
statement5.setString(2, SelectedAppointment.getAppointmentID());
statement5.executeUpdate();
PreparedStatement statement6 = DataBaseC.getConnection().prepareStatement("UPDATE appointment SET appointment.url = ? WHERE appointmentId = ?");
statement6.setString(1, URL.getText());
statement6.setString(2, SelectedAppointment.getAppointmentID());
statement6.executeUpdate();
//Time from String --> LocalDateTime ---> ZonedDateTime(System Default) --Zoned SameInstance of (convert to UTC) ---> ZonedDateTime to LocalDateTime ---> TimeStsmp
PreparedStatement statement7 = DataBaseC.getConnection().prepareStatement("UPDATE appointment SET appointment.start = ? WHERE appointmentId = ?");
LocalDate SDate = StartDate.getValue();
//LocalTime STime = LocalTime.parse(Start.getText());
LocalTime STime = STimeSet();
LocalDateTime StartDateTime = LocalDateTime.of(SDate, STime);
ZonedDateTime startT = StartDateTime.atZone(ZoneId.systemDefault());
ZonedDateTime startTime = startT.withZoneSameInstant(ZoneId.of("UTC"));
//ZonedDateTime startTime = ZonedDateTime.of(StartDateTime, ZoneId.of("UTC"));
Timestamp startTS = Timestamp.valueOf(startTime.toLocalDateTime());
statement7.setTimestamp(1, startTS);
statement7.setString(2, SelectedAppointment.getAppointmentID());
statement7.executeUpdate();
PreparedStatement statement8 = DataBaseC.getConnection().prepareStatement("UPDATE appointment SET appointment.start = ? WHERE appointmentId = ?");
LocalDate EDate = EndDate.getValue();
//LocalTime ETime = LocalTime.parse(End.getText());
LocalTime ETime = ETimeSet();
LocalDateTime EndDateTime = LocalDateTime.of(EDate, ETime);
ZonedDateTime endT = EndDateTime.atZone(ZoneId.systemDefault());
ZonedDateTime endTime = endT.withZoneSameInstant(ZoneId.of("UTC"));
//ZonedDateTime endTime = ZonedDateTime.of(EndDateTime, ZoneId.of("UTC"));
Timestamp endTS = Timestamp.valueOf(endTime.toLocalDateTime());
statement7.setTimestamp(1, endTS);
statement7.setString(2, SelectedAppointment.getAppointmentID());
statement7.executeUpdate();
} catch (SQLException s) {
System.out.println("SQL code does not execute.");
}
FXMLLoader loader = new FXMLLoader();
Stage oldWindow = (Stage) Save.getScene().getWindow();
oldWindow.close();
Parent root = FXMLLoader.load(ApptAppJeffHinkle.class.getResource("view/MainApp.fxml"));
Scene scene2 = new Scene(root);
Stage stage = new Stage();
stage.setScene(scene2);
stage.show();
}
@FXML
private void CancelAction(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader();
Stage oldWindow = (Stage) Save.getScene().getWindow();
oldWindow.close();
Parent root = FXMLLoader.load(ApptAppJeffHinkle.class.getResource("view/MainApp.fxml"));
Scene scene2 = new Scene(root);
Stage stage = new Stage();
stage.setScene(scene2);
stage.show();
//tabPane.getSelectionModel().select(1);
}
}