我使用以下方法从用户检索文件。大多数时候它不会弹出来。难道我做错了什么? 先感谢您。
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);
}
}
答案 0 :(得分:1)
您不需要额外的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();
}
}
答案 1 :(得分:0)
showOpenDialog
需要父组件作为参数。
该组件必须已经可见。
最简单的解决方案是使用null