java - JFileChooser.showOpenDialog(new JFrame());不弹出

标签 java swing jframe jfilechooser

我正在使用下面的方法从用户那里检索文件。但大多数时候它不会弹出。难道我做错了什么? 先感谢您。

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileNameExtensionFilter;


public class FileChooser {
    public String getFile(){
        //public static void main(String[] args){
        JFileChooser fc=new JFileChooser();
          JFrame jf = new JFrame("ExcelReading for Updating Columns"); // added
          jf.setAlwaysOnTop(true); // added
          fc.setDialogTitle("Excel File For Updating Columns");
          fc.setApproveButtonText("Choose Excel File");
          FileNameExtensionFilter filter = new FileNameExtensionFilter("XLS files", "xls");
          jf.setAlwaysOnTop(true); // added

          int returnVal=fc.showOpenDialog(jf);
          if (returnVal == JFileChooser.APPROVE_OPTION) {
            return fc.getSelectedFile().getAbsolutePath();
          }
         else return null;

        }

}

编辑添加了以下信息:

主要方法:使用该类读取excel表格中的信息。我相信我的方法 showMessage 和 JOptionPane.showMessageDialog 方法是造成这种情况的原因。

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.ArrayList;   
import javax.swing.JOptionPane;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
public class ReadFromExcel {

    public static void main(String[] args) 
    {

        /*
         * 
         * 
         * Gets wanted info from excel file. puts them into an Student ArrayList and given to AddingToDatabase.java to enter to database
         * This goes through and reads every single row
         * then goes onto the next row
         * 
         * to do:
         * go down one column at a time.
         */
        FileChooser fc= new FileChooser();
        int desiredColumn=0;
        String file;
        String out=" ";
        String answer="n";
        showMessage("This program will ask you which columns you will want to update to the database. "
                + "\nPlease add all students to the database first before running this program.");
        boolean repeat=false;
        do{
            showMessage("Please select the file you want to get data from");
            file = fc.getFile();
            if (file==null){
                repeat=false;
                out="You did not select a file. ";
            }

            else{
                String sheet = JOptionPane.showInputDialog(null, "Enter the name of the sheet you want information from\n*Case Sensitive*");

                desiredColumn = Integer.parseInt(JOptionPane.showInputDialog(null, "Which column would you like to update? You must enter a number:"
                        + "\nChapter Paid=2"
                        + "\nNational Paid=3"
                        + "\nLast Name=4"
                        + "\nFirst Name=5"
                        + "\nEmail=6"
                        + "\nMajor=8"
                        + "\nYear=9"));
                /*System.out.println("Which column would you like to update? You must enter a number: "
                    + "\nChapter Paid=2"
                    + "\nNational Paid=3"
                    + "\nLast Name=4"
                    + "\nFirst Name=5"
                    + "\nEmail=6"
                    + "\nMajor=8"
                    + "\nYear=9");*/


                if(desiredColumn == 2||desiredColumn==3|desiredColumn==4||desiredColumn==5||desiredColumn==6||desiredColumn==8||desiredColumn==9)
                {   
                    int k=JOptionPane.showConfirmDialog(null, "Confirm following information:\nFilepath: "+file+"\nSheet name: "+sheet+"\nColumn to be updated: "+desiredColumn);
                    if(k!=0)
                    {
                        break;
                    }
                    else
                        complete(desiredColumn);
                }
                else    
                    showMessage(out+"You didn't choose a valid column");


                //System.out.println("Would you like to do another update? y/n?");
                answer = JOptionPane.showInputDialog(null,"Would you like to do another update? y/n?");


                if(answer.charAt(0)=='y')
                {

                    repeat=true;
                }
                else 
                {

                    repeat=false;
                }
            }
        }while(repeat);
        showMessage(out+"Program will now terminate");
        System.exit(0);


    }
    public static void complete(int desiredColumn){
        try
        {
            String fName=null;
            String lName=null;
            int SID = 0;
            String email=null;
            boolean chapterPaid=false;
            boolean nationalPaid=false;
            String major=null;
            String year=null;
            String temp=null;
            int tempInt=0;

            //UpdateToDatabase addD= new UpdateToDatabase();
            UpdateByPHP php=new UpdateByPHP();
            //this is starting at 2nd row(where the values start)
            int currentRow=2;
            //this starts at the first cell
            int currentCell=1;

            //stating exactly which cells i want information from. cells start at 1 which is defined by the currentCell above.
            final int chapterPaidCell=2;
            final int nationalPaidCell=3;
            final int lnameCell=4;
            final int fnameCell=5;
            final int emailCell=6;
            final int studIDCell=7;
            final int majorCell=8;
            final int yearCell=9;

            ArrayList<Students> array= new ArrayList<Students>();
            FileInputStream fileInputStream = new FileInputStream("/Users/jashmit/Desktop/Workbook2.xls");
            HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
            HSSFSheet worksheet = workbook.getSheet("Sheet1");
            Iterator<Row> rowIterator = worksheet.iterator();

            //moves toward starting row. not really needed.
            for (int i=1; i<currentRow; i++)
            {
                rowIterator.next();//skip row of titles
            }


            while (rowIterator.hasNext())//top to bottom
            {
                //variables to be added to database
                //so variables reset every line
                fName=null;
                lName=null;
                SID = 0;
                email=null;
                chapterPaid=false;
                nationalPaid=false;
                major=null;

                Row row = rowIterator.next();
                Iterator <Cell> cellIterator = row.cellIterator();


                while (cellIterator.hasNext()) //columns, left to right
                {

                    Cell cell = cellIterator.next();
                    if(currentCell==chapterPaidCell && currentCell==desiredColumn)
                    {
                        if(cell.getStringCellValue().toLowerCase().equals("yes"))
                        {
                            chapterPaid=true;
                        }
                        else 
                        {           
                            chapterPaid=false;
                        }
                    }
                    else if(currentCell==nationalPaidCell && currentCell==desiredColumn)
                    {
                        if(cell.getStringCellValue().toLowerCase().equals("yes"))
                        {
                            nationalPaid=true;
                        }
                        else 
                            nationalPaid=false;
                    }
                    else if(currentCell==lnameCell && currentCell==desiredColumn)
                    {
                        lName=cell.getStringCellValue();
                    }
                    else if(currentCell==fnameCell && currentCell==desiredColumn)
                    {
                        fName=cell.getStringCellValue();
                    }
                    else if(currentCell==emailCell && currentCell==desiredColumn)
                    {
                        email=cell.getStringCellValue();
                    }
                    else if(currentCell==studIDCell)
                    {
                        SID=(int) cell.getNumericCellValue();
                    }
                    else if (currentCell==majorCell && currentCell==desiredColumn)
                    {
                        major=cell.getStringCellValue();
                    }
                    else if (currentCell==yearCell && currentCell==desiredColumn)
                    {
                        temp=cell.getStringCellValue();
                        tempInt=temp.indexOf('(');
                        if(tempInt==-1)
                        {
                            year=cell.getStringCellValue(); 
                        }
                        else
                        {
                            year=temp.substring(0,tempInt);
                        }
                    }
                    currentCell++;
                }
                currentCell=1;
                array.add(new Students(fName,lName, SID ,email,chapterPaid,nationalPaid,major,year));
            }

            //now after reading from excel, put info into database
            workbook.close();

            php.writePHP(array, desiredColumn);
            return;

        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    public static void showMessage(String text)
    {

        JOptionPane.showMessageDialog(null, text);
    }
}

最佳答案

您不需要额外的 JFrame 来显示 JFileChooser。 这是您修改后的代码。

public class FileChooser {
public String getFile(){
    //public static void main(String[] args){
    JFileChooser fc=new JFileChooser();
    fc.setDialogTitle("Excel File For Updating Columns");
    fc.setApproveButtonText("Choose Excel File");
     FileNameExtensionFilter filter = new FileNameExtensionFilter("XLS files", "xls");
      int returnVal=fc.showOpenDialog(null);
      if (returnVal == JFileChooser.APPROVE_OPTION) {
        return fc.getSelectedFile().getAbsolutePath();
      }
     else return null;
    }
    public static void main(String[] args) {
        new FileChooser().getFile();
    }
}

关于java - JFileChooser.showOpenDialog(new JFrame());不弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32291830/

相关文章:

java - BigDecimal(java) 中指数表示法的缩放问题

java - 简单的 diff 算法,如 winmerge.exe

java - 如何在java GUI中嵌入10秒的MP4视频?

java - 使用 if 语句和单选按钮在 Jlabel 上显示文本

java - 我应该在 netbeans 中创建 Images 文件夹吗

java - java中多线程的意外答案

java - JLabel/JPanel 定位问题。 (网格包布局)

java - 在 JPanel 之间传递数据

java - 在手动拉伸(stretch)框架之前,文本框不会显示在框架上

java - 在 GWT 应用程序中外部化 HTML 的最佳方法?