1

同名のファイルを作成しようとしたときに、既にファイルが存在する場合にアラートを表示したい。コードを完全には完成させていません。ボタンの値 Yes/No を UI から取得したい。

コード:

これは、コントローラーのコーディング方法です。

package application;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.TreeMap;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

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.stage.Stage;

public class WarningController implements Initializable {
    @FXML
    public Button yes;

    @FXML
    public Button no;

    public static String type;

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

    }

     public String confirmSelection(ActionEvent event)throws IOException{
         Button button = (Button) event.getSource();
             type = button.getText();
            if(type.equals("Yes")){
                Stage stage = (Stage) yes.getScene().getWindow();
                stage.close();
                //System.out.println("Yes");
                return type;
            }
            else{
                //System.out.println("No");
                Stage stage1 = (Stage) no.getScene().getWindow();
                stage1.close();
                return type;
            }

     }
/********************************************************************************/
     public void writesheet(String[][] result,String ComboValue,String[] heading) throws IOException{ 
            //Create blank workbook
            XSSFWorkbook workbook = new XSSFWorkbook(); 
            //Create a blank sheet
            XSSFSheet spreadsheet = workbook.createSheet( " Employee Info ");
            //Create row object
            XSSFRow row;

            String[][] towrite=result;
            int rows=towrite.length;
            //int cols=towrite[0].length;
           // System.out.println(rows +"    "+ cols);

            Map < String, Object[] > empinfo = new TreeMap < String, Object[] >();
            empinfo.put("0", heading);
            for(int i=1;i<=rows;i++){
                empinfo.put( Integer.toString(i),towrite[i-1]);
            }


            //Iterate over data and write to sheet
            Set < String > keyid = empinfo.keySet();
            int rowid = 0;
            for (String key : keyid)
            {
               row = spreadsheet.createRow(rowid++);
               Object [] objectArr = empinfo.get(key);
               int cellid = 0;
               for (Object obj : objectArr)
               {
                  Cell cell = row.createCell(cellid++);
                  //cell.setCellValue((String)obj);
                  cell.setCellValue(obj.toString());
               }
            }

            //Write the workbook in file system
            File f=new File(("C:\\"+ComboValue+".xlsx"));
            if(f.exists()){
                Stage primaryStage=new Stage();
                Parent root=FXMLLoader.load(getClass().getResource("/application/Warning.fxml"));
                Scene scene = new Scene(root,350,150);
                scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                primaryStage.setScene(scene);
                primaryStage.show();
                System.out.println(type);
            }
            FileOutputStream out = new FileOutputStream(f);
            workbook.write(out);
            out.close();
            System.out.println(ComboValue+"  "+"Excel document written successfully" );
            workbook.close();
            }


}

writesheet関数でボタンの値(文字列型で格納)を使いたいです。今はNULLを返しています。

警告を表示する他の方法があれば提案してください。私は 2 つの fxml ファイルを使用していますが、これは 2 番目の Excel ファイルです。

ここに画像の説明を入力

  [1]: http://i.stack.imgur.com/ZK6UC.jpg
4

2 に答える 2

5

Alertクラスを使用するだけです。これは、必要なほとんどの yes/no ダイアログの機能を提供します。

Alert alert = new Alert(AlertType.WARNING, 
                        "File already exists. Do you want to override?", 
                        ButtonType.YES, ButtonType.NO);

Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.YES){
    // ... user chose YES
} else {
    // ... user chose NO or closed the dialog
}

ここにも良いチュートリアルがあります。

于 2016-10-06T16:01:40.810 に答える