在从Combobox Javafx中选择项目时,如何将文本设置为TextField? [英] How to set text to a TextField when selecting an Item from a Combobox Javafx?

查看:135
本文介绍了在从Combobox Javafx中选择项目时,如何将文本设置为TextField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从组合框中获取所选项,在事件之后单击它在TextField上显示一个不同的文本,声明为txtActivity



代码不工作
需要帮助



这里卡住了

  if(cmbComponent.getSelectionModel()。getSelectedItem()。equals(Ministry of Water)){
txtActivity.setText(1.0.0);

}

这是我的完整代码

 包应用程序; 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

public class SafeguardTrackingToolController implements可初始化{

@FXML
private TextField txtActivity;
@FXML
private TextArea txtComments;
/ * @ FXML
private ComboBox< String> cmbComponent; * /

@FXML
private ComboBox<详细信息> cmbComponent;
private ObservableList<详细信息> cmbComponentData = FXCollections.observableArrayList();

@FXML
private TableColumn
tblComponent,
tblActivity,
tableColumnForTor,
tblContract,
tblfirst,
tblFinal,
tblBank,
tblDisclosure,
tblNema,
tblBudgetRe,
tblBudgetPro,
tblBegin,
tblComments;

@FXML
private TableView<项目> tableViewForExcel;

@FXML
private DatePicker
dateTor,
dateContract,
dateFirstDraft,
dateFinalDraft,
dateBank,
dateDisclosure,
dateNema,
dateBudget,
dateProvided,
dateImplementation;

@FXML
private Button btnAdd,btnReset,btnCreateAnotherSheet,btnExcel,btnDelete;


private final ObservableList<项目> data = FXCollections.observableArrayList(new Items(null,null,null,null,null,null,null,null,null,null,null,null,


@Override
public void initialize(URL location,ResourceBundle resources){
// TODO自动生成方法存根

tblComponent .setCellValueFactory(new PropertyValueFactory(Component));
tblActivity.setCellValueFactory(new PropertyValueFactory(Activity));
tableColumnForTor.setCellValueFactory(new PropertyValueFactory(Torr));
tblContract.setCellValueFactory(new PropertyValueFactory(Contract));
tblfirst.setCellValueFactory(new PropertyValueFactory(FirstDraft));
tblFinal.setCellValueFactory(new PropertyValueFactory(FinalDraft));
tblBank.setCellValueFactory(new PropertyValueFactory(Bank));
tblDisclosure.setCellValueFactory(new PropertyValueFactory(Disclosure));
tblNema.setCellValueFactory(new PropertyValueFactory(Nema));
tblBudgetRe.setCellValueFactory(new PropertyValueFactory(Budget));
tblBudgetPro.setCellValueFactory(new PropertyValueFactory(Provided));
tblBegin.setCellValueFactory(new PropertyValueFactory(Implementation));
tblComments.setCellValueFactory(new PropertyValueFactory(Comment));


cmbComponentData.add(new Details(Ministry of Water,));
cmbComponentData.add(new Details(Ministry of Lands,));
cmbComponentData.add(new Details(Ministry of Minerals,));
cmbComponentData.add(new Details(Ministry of ManKind,));

if(cmbComponent.getSelectionModel()。getSelectedItem()。equals(Ministry of Water)){
txtActivity.setText(1.0.0);

}

cmbComponent.setItems(cmbComponentData);




tableViewForExcel.getItems()。setAll(this.data);
tableViewForExcel.setEditable(true);
}


public static class Items {
// setters和getters应该与简单的字符串属性同名

private final SimpleStringProperty Component;
private final SimpleStringProperty Activity;
private final SimpleStringProperty Torr;
private final SimpleStringProperty Contract;
private final SimpleStringProperty FirstDraft;
private final SimpleStringProperty FinalDraft;
private final SimpleStringProperty Bank;
private final SimpleStringProperty披露;
private final SimpleStringProperty Nema;
private final SimpleStringProperty Budget;
private final SimpleStringProperty提供;
private final SimpleStringProperty实现;
private final SimpleStringProperty注释;

private Item(String Comp,String Act,
String Tor,String Co,
String FDraft,String FinalDra,
String Ban,String Dis,String Nem,
String Bud,String Pro,String Im,String Comm){



this.Component = new SimpleStringProperty(Comp);
this.Activity = new SimpleStringProperty(Act);
this.Torr = new SimpleStringProperty(Tor);
this.Contract = new SimpleStringProperty(Co);
this.FirstDraft = new SimpleStringProperty(FDraft);
this.FinalDraft = new SimpleStringProperty(FinalDra);
this.Bank = new SimpleStringProperty(Ban);
this.Disclosure = new SimpleStringProperty(Dis);
this.Nema = new SimpleStringProperty(Nem);
this.Budget = new SimpleStringProperty(Bud);
this.Provided = new SimpleStringProperty(Pro);
this.Implementation = new SimpleStringProperty(Im);
this.Comment = new SimpleStringProperty(Comm);

}

public String getComponent(){
return Component.get();
}

public void setComponent(String Comp){
Component.set(Comp);
}


public String getActivity(){
return Activity.get();
}

public void setAcivity(String Act){
Activity.set(Act);
}


public String getTorr(){
return Torr.get();
}

public void setTorr(String Tor){
Torr.set(Tor);
}


public String getContract(){
return Contract.get();
}

public void setContract(String Co){
Contract.set(Co);
}


public String getFirstDraft(){
return FirstDraft.get();
}

public void setFirstDraft(String FDraft){
FirstDraft.set(FDraft);
}


public String getFinalDraft(){
return FinalDraft.get();
}

public void setFinalDraft(String FinalDra){
FinalDraft.set(FinalDra);
}


public String getBank(){
return Bank.get();
}

public void setBank(String Ban){
Bank.set(Ban);
}


public String getDisclosure(){
return Disclosure.get();
}

public void setDisclosure(String Dis){
Disclosure.set(Dis);
}


public String getNema(){
return Nema.get()
}

public void setNema(String Nem){
Nema.set(Nem);
}

public String getBudget(){
return Budget.get();
}

public void setBudget(String Bud){
Budget.set(Bud);
}


public String getProvided(){
return Provided.get();
}

public void setProvide(String Pro){
Provided.set(Pro);
}



public String getImplementation(){
return Implementation.get();
}

public void setImplementation(String Im){
Implementation.set(Im);
}


public String getComment(){
return Comment.get();
}

public void setComment(String Comm){
Comment.set(Comm);
}

}


private void write()throws FileNotFoundException,IOException {


try {


文件myFile =新文件(example.xlsx);
final XSSFWorkbook工作簿;
if(myFile.exists()== false){
System.out.println(Creating a new workbook'+ myFile +');
workbook = new XSSFWorkbook();

} else {
System.out.println(追加到现有工作簿+ myFile +');
final InputStream is = new FileInputStream(myFile);
try {
workbook = new XSSFWorkbook(is);
} finally {
is.close();
}

}
int sheetIndex = 1;
while(workbook.getSheet(sample+ sheetIndex)!= null){
sheetIndex ++;
}

XSSFSheet spreadsheet = workbook.createSheet(sample);

XSSFRow row = null;
XSSFCell cell = null;

row = spreadsheet.createRow(0);
cell = row.createCell(0);
cell.setCellValue(Component);

cell = row.createCell(1);
cell.setCellValue(Activity);

cell = row.createCell(2);
cell.setCellValue(TOR);

cell = row.createCell(3);
cell.setCellValue(Contract);

cell = row.createCell(4);
cell.setCellValue(First Draft);

cell = row.createCell(5);
cell.setCellValue(final Draft);

cell = row.createCell(6);
cell.setCellValue(银行结余);

cell = row.createCell(7);
cell.setCellValue(Disclosure);

cell = row.createCell(8);
cell.setCellValue(NEMA License);

cell = row.createCell(9);
cell.setCellValue(Budget Request& Prepared);

cell = row.createCell(10);
cell.setCellValue(Budget Provided);

cell = row.createCell(11);
cell.setCellValue(Begin& Implementation);

cell = row.createCell(12);
cell.setCellValue(Comments);



int i = 1;
for(Items item:tableViewForExcel.getItems()){
row = spreadsheet.createRow(i);
// row.createCell(0).setCellValue(item.getComponent());

cell = row.createCell(0);
cell.setCellValue(tblComponent.getCellData(1).toString());

cell = row.createCell(1);
cell.setCellValue(tblActivity.getCellData(1).toString());

cell = row.createCell(2);
cell.setCellValue(tableColumnForTor.getCellData(1).toString());

cell = row.createCell(3);
cell.setCellValue(tblContract.getCellData(1).toString());

cell = row.createCell(4);
cell.setCellValue(tblfirst.getCellData(1).toString());

cell = row.createCell(5);
cell.setCellValue(tblFinal.getCellData(1).toString());

cell = row.createCell(6);
cell.setCellValue(tblBank.getCellData(1).toString());

cell = row.createCell(7);
cell.setCellValue(tblDisclosure.getCellData(1).toString());

cell = row.createCell(8);
cell.setCellValue(tblNema.getCellData(1).toString());

cell = row.createCell(9);
cell.setCellValue(tblBudgetRe.getCellData(1).toString());

cell = row.createCell(10);
cell.setCellValue(tblBudgetPro.getCellData(1).toString());

cell = row.createCell(11);
cell.setCellValue(tblBegin.getCellData(1).toString());

cell = row.createCell(12);
cell.setCellValue(tblComments.getCellData(1).toString());

// ....添加其他列数据
i ++;
}

FileOutputStream out = new FileOutputStream(myFile);
workbook.write(out);
out.close();
System.out.println(Data wrtten Successfully);
} catch(Exception e){
e.printStackTrace();
}

}


@FXML
private void onClickToAddOntheTable(ActionEvent event){
tableViewForExcel.getItems()。 add(new Items(
cmbComponent.getValue()。toString(),
txtActivity.getText(),
dateTor.getValue()。toString(),
dateContract.getValue (),
dateFirstDraft.getValue()。toString(),
dateFinalDraft.getValue()。toString(),
dateBank.getValue b $ b dateDisclosure.getValue()。toString(),
dateNema.getValue()。toString(),
dateBudget.getValue()。toString(),
dateProvided.getValue .toString(),
dateImplementation.getValue()。toString(),

txtComments.getText()));

onClickToReset(event);



}

@FXML
private void onClickToSaveInExcel(ActionEvent event){
try {
write ();
} catch(FileNotFoundException e){
// TODO自动生成的catch块
e.printStackTrace();
} catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();
}
}

@FXML
private void onClickToReset(ActionEvent event){
cmbComponent.setValue(null);
txtActivity.clear();
dateTor.setValue(null);;
dateContract.setValue(null);
dateFirstDraft.setValue(null);
dateFinalDraft.setValue(null);
dateBank.setValue(null);
dateDisclosure.setValue(null);
dateNema.setValue(null);
dateBudget.setValue(null);
dateProvided.setValue(null);
dateImplementation.setValue(null);
txtComments.clear();
}



@FXML
private void onClickToResetTable(ActionEvent event){
tableViewForExcel.getItems()。clear
}


@FXML
private void onClickToCreateAnotherSheet(ActionEvent event){
LoadAnotherSheet();

}

private void LoadAnotherSheet(){
父root = null;
try {
root = FXMLLoader.load(getClass()。getResource(SafeguardTrackingToolNewSheetUI.fxml));
场景scene = new Scene(root);
Stage nStage = new Stage();
nStage.setScene(scene);
//nStage.setMaximized(true);
nStage.setTitle(Another Sheet);
nStage.show();
Stage stage =(Stage)btnCreateAnotherSheet.getScene()。getWindow();
stage.close();
} catch(IOException e){
e.printStackTrace();
}

}



public class Details {

private final StringProperty ministryOfWater;
private final StringProperty ministyOfLands;

public Details(String ministryOfWater,String ministyOfLands){
this.ministryOfWater = new SimpleStringProperty(ministryOfWater);
this.ministyOfLands = new SimpleStringProperty(ministyOfLands);
}

public String getMinistryOfWater(){
return ministryOfWater.get();
}

public void setMinistryOfWater(String ministryOfWater){
this.ministryOfWater.set(ministryOfWater);
}

public StringProperty mistryOfWaterProperty(){
return ministryOfWater;
}
//完成的水务部门

public String getMinistyOfLands(){
return ministyOfLands.get();
}

public void setMinistyOfLands(String ministyOfLands){
this.ministyOfLands.set(ministyOfLands);
}

public StringProperty ministyOfLandsProperty(){
return ministyOfLands;
}

//完成土地部门

@Override
public String toString(){
return getMinistryOfWater() + getMinistyOfLands();
}

}

}


getSelectedItem()时,选择 null 所以你的谓词总是失败。一种方法是设置所需的索引并对其进行测试。

  cmbComponent.getSelectionModel 
if(cmbComponent.getSelectionModel()。getSelectedIndex()== 0){
txtActivity.setText(1.0.0);
}




我尝试使用 else if 条件设置另一个文本,如2.0.0,它不会响应。


查看您之前关于此主题的



已测试:

  import javafx.application.Application; 
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/ **
* @see http://stackoverflow.com/a/38347332/230513
* /
public class ComboBoxTest extends Application {

@Override
public void start(Stage primaryStage){
primaryStage.setTitle(ComboBoxTest);

final TextField txtActivity = new TextField();
final ComboBox cmbComponent = new ComboBox();
ObservableList< Details> cmbComponentData = FXCollections.observableArrayList();
cmbComponentData.add(new Details(Ministry of Water,1.0.0));
cmbComponentData.add(new Details(Ministry of Lands,2.0.0));
cmbComponentData.add(new Details(Ministry of Minerals,3.0.0));
cmbComponentData.add(new Details(Manikind,4.0.0));
cmbComponent.setItems(cmbComponentData);
cmbComponent.getSelectionModel()。selectFirst();
txtActivity.setText(cmbComponentData.get(0).getText());
cmbComponent.valueProperty()。addListener((o,ov,nv) - > {
Details d =(Details)nv;
txtActivity.setText(d.getText());
});

VBox root = new VBox();
root.getChildren()。addAll(cmbComponent,txtActivity);
场景scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}

private静态类详细信息{

private final StringProperty name;
private final StringProperty text;

public Details(String name,String text){
this.name = new SimpleStringProperty(name);
this.text = new SimpleStringProperty(text);
}

public String getName(){
return name.get();
}

public void setName(String name){
this.name.set(name);
}

public StringProperty nameProperty(){
return name;
}

public String getText(){
return text.get();
}

public void setText(String text){
this.text.set(text);
}

public StringProperty textProperty(){
return text;
}

@Override
public String toString(){
return getName();
}
}

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

}


I want to get the selected item from the combobox, after the event click it displays a different text on a TextField declared as txtActivity

I am stuck at this point at which the code is not working need help

am stuck at this point

if(cmbComponent.getSelectionModel().getSelectedItem().equals("Ministry of Water")){
    txtActivity.setText("1.0.0");

} 

This is my full code

package application;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

public class SafeguardTrackingToolController implements Initializable {

    @FXML
    private TextField txtActivity;
    @FXML
    private TextArea txtComments;
    /*@FXML
    private ComboBox<String> cmbComponent;*/

    @FXML
    private ComboBox < Details > cmbComponent;
    private ObservableList < Details > cmbComponentData = FXCollections.observableArrayList();

    @FXML
    private TableColumn
    tblComponent,
    tblActivity,
    tableColumnForTor,
    tblContract,
    tblfirst,
    tblFinal,
    tblBank,
    tblDisclosure,
    tblNema,
    tblBudgetRe,
    tblBudgetPro,
    tblBegin,
    tblComments;

    @FXML
    private TableView < Items > tableViewForExcel;

    @FXML
    private DatePicker
    dateTor,
    dateContract,
    dateFirstDraft,
    dateFinalDraft,
    dateBank,
    dateDisclosure,
    dateNema,
    dateBudget,
    dateProvided,
    dateImplementation;

    @FXML
    private Button btnAdd, btnReset, btnCreateAnotherSheet, btnExcel, btnDelete;


    private final ObservableList < Items > data = FXCollections.observableArrayList(new Items(null, null, null, null, null, null, null, null, null, null, null, null, null));


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // TODO Auto-generated method stub

        tblComponent.setCellValueFactory(new PropertyValueFactory("Component"));
        tblActivity.setCellValueFactory(new PropertyValueFactory("Activity"));
        tableColumnForTor.setCellValueFactory(new PropertyValueFactory("Torr"));
        tblContract.setCellValueFactory(new PropertyValueFactory("Contract"));
        tblfirst.setCellValueFactory(new PropertyValueFactory("FirstDraft"));
        tblFinal.setCellValueFactory(new PropertyValueFactory("FinalDraft"));
        tblBank.setCellValueFactory(new PropertyValueFactory("Bank"));
        tblDisclosure.setCellValueFactory(new PropertyValueFactory("Disclosure"));
        tblNema.setCellValueFactory(new PropertyValueFactory("Nema"));
        tblBudgetRe.setCellValueFactory(new PropertyValueFactory("Budget"));
        tblBudgetPro.setCellValueFactory(new PropertyValueFactory("Provided"));
        tblBegin.setCellValueFactory(new PropertyValueFactory("Implementation"));
        tblComments.setCellValueFactory(new PropertyValueFactory("Comment"));


        cmbComponentData.add(new Details("Ministry of Water", ""));
        cmbComponentData.add(new Details("Ministry of Lands", ""));
        cmbComponentData.add(new Details("Ministry of Minerals", ""));
        cmbComponentData.add(new Details("Ministry of ManKind", ""));

        if (cmbComponent.getSelectionModel().getSelectedItem().equals("Ministry of Water")) {
            txtActivity.setText("1.0.0");

        }

        cmbComponent.setItems(cmbComponentData);




        tableViewForExcel.getItems().setAll(this.data);
        tableViewForExcel.setEditable(true);
    }


    public static class Items {
        //the setters and getters should be of the same name with the simple string property

        private final SimpleStringProperty Component;
        private final SimpleStringProperty Activity;
        private final SimpleStringProperty Torr;
        private final SimpleStringProperty Contract;
        private final SimpleStringProperty FirstDraft;
        private final SimpleStringProperty FinalDraft;
        private final SimpleStringProperty Bank;
        private final SimpleStringProperty Disclosure;
        private final SimpleStringProperty Nema;
        private final SimpleStringProperty Budget;
        private final SimpleStringProperty Provided;
        private final SimpleStringProperty Implementation;
        private final SimpleStringProperty Comment;

        private Items(String Comp, String Act,
            String Tor, String Co,
            String FDraft, String FinalDra,
            String Ban, String Dis, String Nem,
            String Bud, String Pro, String Im, String Comm) {



            this.Component = new SimpleStringProperty(Comp);
            this.Activity = new SimpleStringProperty(Act);
            this.Torr = new SimpleStringProperty(Tor);
            this.Contract = new SimpleStringProperty(Co);
            this.FirstDraft = new SimpleStringProperty(FDraft);
            this.FinalDraft = new SimpleStringProperty(FinalDra);
            this.Bank = new SimpleStringProperty(Ban);
            this.Disclosure = new SimpleStringProperty(Dis);
            this.Nema = new SimpleStringProperty(Nem);
            this.Budget = new SimpleStringProperty(Bud);
            this.Provided = new SimpleStringProperty(Pro);
            this.Implementation = new SimpleStringProperty(Im);
            this.Comment = new SimpleStringProperty(Comm);

        }

        public String getComponent() {
            return Component.get();
        }

        public void setComponent(String Comp) {
            Component.set(Comp);
        }


        public String getActivity() {
            return Activity.get();
        }

        public void setAcivity(String Act) {
            Activity.set(Act);
        }


        public String getTorr() {
            return Torr.get();
        }

        public void setTorr(String Tor) {
            Torr.set(Tor);
        }


        public String getContract() {
            return Contract.get();
        }

        public void setContract(String Co) {
            Contract.set(Co);
        }


        public String getFirstDraft() {
            return FirstDraft.get();
        }

        public void setFirstDraft(String FDraft) {
            FirstDraft.set(FDraft);
        }


        public String getFinalDraft() {
            return FinalDraft.get();
        }

        public void setFinalDraft(String FinalDra) {
            FinalDraft.set(FinalDra);
        }


        public String getBank() {
            return Bank.get();
        }

        public void setBank(String Ban) {
            Bank.set(Ban);
        }


        public String getDisclosure() {
            return Disclosure.get();
        }

        public void setDisclosure(String Dis) {
            Disclosure.set(Dis);
        }


        public String getNema() {
            return Nema.get();
        }

        public void setNema(String Nem) {
            Nema.set(Nem);
        }

        public String getBudget() {
            return Budget.get();
        }

        public void setBudget(String Bud) {
            Budget.set(Bud);
        }


        public String getProvided() {
            return Provided.get();
        }

        public void setProvide(String Pro) {
            Provided.set(Pro);
        }



        public String getImplementation() {
            return Implementation.get();
        }

        public void setImplementation(String Im) {
            Implementation.set(Im);
        }


        public String getComment() {
            return Comment.get();
        }

        public void setComment(String Comm) {
            Comment.set(Comm);
        }

    }


    private void write() throws FileNotFoundException, IOException {


        try {


            File myFile = new File("example.xlsx");
            final XSSFWorkbook workbook;
            if (myFile.exists() == false) {
                System.out.println("Creating a new workbook '" + myFile + "'");
                workbook = new XSSFWorkbook();

            } else {
                System.out.println("Appending to existing workbook '" + myFile + "'");
                final InputStream is = new FileInputStream(myFile);
                try {
                    workbook = new XSSFWorkbook(is);
                } finally {
                    is.close();
                }

            }
            int sheetIndex = 1;
            while (workbook.getSheet("sample" + sheetIndex) != null) {
                sheetIndex++;
            }

            XSSFSheet spreadsheet = workbook.createSheet("sample");

            XSSFRow row = null;
            XSSFCell cell = null;

            row = spreadsheet.createRow(0);
            cell = row.createCell(0);
            cell.setCellValue("Component");

            cell = row.createCell(1);
            cell.setCellValue("Activity");

            cell = row.createCell(2);
            cell.setCellValue("TOR");

            cell = row.createCell(3);
            cell.setCellValue("Contract");

            cell = row.createCell(4);
            cell.setCellValue("First Draft");

            cell = row.createCell(5);
            cell.setCellValue("Final Draft");

            cell = row.createCell(6);
            cell.setCellValue("Bank Clearance");

            cell = row.createCell(7);
            cell.setCellValue("Disclosure");

            cell = row.createCell(8);
            cell.setCellValue("NEMA Licence");

            cell = row.createCell(9);
            cell.setCellValue("Budget Request & Prepared");

            cell = row.createCell(10);
            cell.setCellValue("Budget Provided");

            cell = row.createCell(11);
            cell.setCellValue("Begin & Implementation");

            cell = row.createCell(12);
            cell.setCellValue("Comments");



            int i = 1;
            for (Items item: tableViewForExcel.getItems()) {
                row = spreadsheet.createRow(i);
                // row.createCell(0).setCellValue(item.getComponent());

                cell = row.createCell(0);
                cell.setCellValue(tblComponent.getCellData(1).toString());

                cell = row.createCell(1);
                cell.setCellValue(tblActivity.getCellData(1).toString());

                cell = row.createCell(2);
                cell.setCellValue(tableColumnForTor.getCellData(1).toString());

                cell = row.createCell(3);
                cell.setCellValue(tblContract.getCellData(1).toString());

                cell = row.createCell(4);
                cell.setCellValue(tblfirst.getCellData(1).toString());

                cell = row.createCell(5);
                cell.setCellValue(tblFinal.getCellData(1).toString());

                cell = row.createCell(6);
                cell.setCellValue(tblBank.getCellData(1).toString());

                cell = row.createCell(7);
                cell.setCellValue(tblDisclosure.getCellData(1).toString());

                cell = row.createCell(8);
                cell.setCellValue(tblNema.getCellData(1).toString());

                cell = row.createCell(9);
                cell.setCellValue(tblBudgetRe.getCellData(1).toString());

                cell = row.createCell(10);
                cell.setCellValue(tblBudgetPro.getCellData(1).toString());

                cell = row.createCell(11);
                cell.setCellValue(tblBegin.getCellData(1).toString());

                cell = row.createCell(12);
                cell.setCellValue(tblComments.getCellData(1).toString());

                //.... add other column data as well
                i++;
            }

            FileOutputStream out = new FileOutputStream(myFile);
            workbook.write(out);
            out.close();
            System.out.println("Data is wrtten Successfully");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @FXML
    private void onClickToAddOntheTable(ActionEvent event) {
        tableViewForExcel.getItems().add(new Items(
            cmbComponent.getValue().toString(),
            txtActivity.getText(),
            dateTor.getValue().toString(),
            dateContract.getValue().toString(),
            dateFirstDraft.getValue().toString(),
            dateFinalDraft.getValue().toString(),
            dateBank.getValue().toString(),
            dateDisclosure.getValue().toString(),
            dateNema.getValue().toString(),
            dateBudget.getValue().toString(),
            dateProvided.getValue().toString(),
            dateImplementation.getValue().toString(),

            txtComments.getText()));

        onClickToReset(event);



    }

    @FXML
    private void onClickToSaveInExcel(ActionEvent event) {
        try {
            write();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @FXML
    private void onClickToReset(ActionEvent event) {
        cmbComponent.setValue(null);
        txtActivity.clear();
        dateTor.setValue(null);;
        dateContract.setValue(null);
        dateFirstDraft.setValue(null);
        dateFinalDraft.setValue(null);
        dateBank.setValue(null);
        dateDisclosure.setValue(null);
        dateNema.setValue(null);
        dateBudget.setValue(null);
        dateProvided.setValue(null);
        dateImplementation.setValue(null);
        txtComments.clear();
    }



    @FXML
    private void onClickToResetTable(ActionEvent event) {
        tableViewForExcel.getItems().clear();
    }


    @FXML
    private void onClickToCreateAnotherSheet(ActionEvent event) {
        LoadAnotherSheet();

    }

    private void LoadAnotherSheet() {
        Parent root = null;
        try {
            root = FXMLLoader.load(getClass().getResource("SafeguardTrackingToolNewSheetUI.fxml"));
            Scene scene = new Scene(root);
            Stage nStage = new Stage();
            nStage.setScene(scene);
            //nStage.setMaximized(true);
            nStage.setTitle("Another Sheet");
            nStage.show();
            Stage stage = (Stage) btnCreateAnotherSheet.getScene().getWindow();
            stage.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



    public class Details {

        private final StringProperty ministryOfWater;
        private final StringProperty ministyOfLands;

        public Details(String ministryOfWater, String ministyOfLands) {
            this.ministryOfWater = new SimpleStringProperty(ministryOfWater);
            this.ministyOfLands = new SimpleStringProperty(ministyOfLands);
        }

        public String getMinistryOfWater() {
            return ministryOfWater.get();
        }

        public void setMinistryOfWater(String ministryOfWater) {
            this.ministryOfWater.set(ministryOfWater);
        }

        public StringProperty ministryOfWaterProperty() {
                return ministryOfWater;
            }
            //finished  ministry of water

        public String getMinistyOfLands() {
            return ministyOfLands.get();
        }

        public void setMinistyOfLands(String ministyOfLands) {
            this.ministyOfLands.set(ministyOfLands);
        }

        public StringProperty ministyOfLandsProperty() {
            return ministyOfLands;
        }

        //finished with the ministry of lands 

        @Override
        public String toString() {
            return getMinistryOfWater() + " " + getMinistyOfLands();
        }

    }

}

解决方案

When getSelectedItem() is called, the selection is null, so your predicate always fails. One approach would be set the desired index and test it.

cmbComponent.getSelectionModel().selectFirst();
if (cmbComponent.getSelectionModel().getSelectedIndex() == 0) {
    txtActivity.setText("1.0.0");
}

I tried using the else if condition to set another text like "2.0.0" it won't respond.

Looking at your previous question on this topic, it looks like you want to update the TextField when the ComboBox changes. Implementing the first approach suggested by @fabian, the updated example below adds a ChangeListener to the ComboBox. The listener fetches the required text from the selected instance of Details and uses it to update the TextField.

As tested:

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @see http://stackoverflow.com/a/38347332/230513
 */
public class ComboBoxTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("ComboBoxTest");

        final TextField txtActivity = new TextField();
        final ComboBox cmbComponent = new ComboBox();
        ObservableList<Details> cmbComponentData = FXCollections.observableArrayList();
        cmbComponentData.add(new Details("Ministry of Water", "1.0.0"));
        cmbComponentData.add(new Details("Ministry of Lands", "2.0.0"));
        cmbComponentData.add(new Details("Ministry of Minerals", "3.0.0"));
        cmbComponentData.add(new Details("Ministry of ManKind", "4.0.0"));
        cmbComponent.setItems(cmbComponentData);
        cmbComponent.getSelectionModel().selectFirst();
        txtActivity.setText(cmbComponentData.get(0).getText());
        cmbComponent.valueProperty().addListener((o, ov, nv) -> {
            Details d = (Details) nv;
            txtActivity.setText(d.getText());
        });

        VBox root = new VBox();
        root.getChildren().addAll(cmbComponent, txtActivity);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private static class Details {

        private final StringProperty name;
        private final StringProperty text;

        public Details(String name, String text) {
            this.name = new SimpleStringProperty(name);
            this.text = new SimpleStringProperty(text);
        }

        public String getName() {
            return name.get();
        }

        public void setName(String name) {
            this.name.set(name);
        }

        public StringProperty nameProperty() {
            return name;
        }

        public String getText() {
            return text.get();
        }

        public void setText(String text) {
            this.text.set(text);
        }

        public StringProperty textProperty() {
            return text;
        }

        @Override
        public String toString() {
            return getName();
        }
    }

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

}

这篇关于在从Combobox Javafx中选择项目时,如何将文本设置为TextField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆