JAVA FX中的警报 [英] Alert in JAVA FX

查看:163
本文介绍了JAVA FX中的警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试创建具有相同名称的文件时,我想在文件已存在时显示警报.我尚未完全完成代码.我想从UI中检索按钮值Yes/No.

I want to display an alert when a file already exists when trying to create the file with same name . I have not completed the code fully. I want to retrieve the button value Yes/No from the UI .

代码:

这是控制器的编码方式.

This is how the controller is coded.

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函数中使用按钮值(以String类型存储).现在它返回NULL.

I want to use button value(stored in String type) in writesheet function. Now it is returning NULL.

请建议是否还有其他显示警告的方法.我正在使用两个fxml文件,这是第二个excel文件.

Please suggest if there is any other way to show warning.I am using two fxml files and this is the second excel file.

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

推荐答案

只需使用

Simply use the Alert class. It provides functionality for most yes/no dialogs that you ever need.

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
}

此处是一个很好的教程.

这篇关于JAVA FX中的警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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