简体   繁体   English

从jcombobox获取文件路径

[英]Get the file path from jcombobox

I am trying to select my file from the jcombobox and display the data in a text area. 我试图从jcombobox中选择我的文件,并在文本区域中显示数据。 Currently, I have list out the file name but now once i select the filename from the dropdown its showing me this error: 当前,我已经列出了文件名,但是现在一旦从下拉列表中选择文件名,它就会向我显示此错误:

SEVERE: null

This is my current code: 这是我当前的代码:

private void jCmboxActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // TODO add your handling code here:
    //JComboBox jCmbox = (JComboBox)evt.getSource();
    String stateName = (String)jCmbox.getSelectedItem();
    updateData(stateName);

}                                      

public void updateData(String path){
    String csvFilename = "";
    URL url;
    try {
        url = new URL(csvFilename);
    } catch (MalformedURLException ex) {
        Logger.getLogger(VisualizationPanel.class.getName()).log(Level.SEVERE, null, ex);
    }
    url = VisualizationPanel.class.getResource(path);
    CSVData data = CSVData.loadFromFile(url.toString()); //loads the csv data
    VisualizationPanel visPanel = new VisualizationPanel(this, data); //draws the vis panel and adds the data to it
}

I am new to java can someone pls help me on this. 我是java的新手,有人可以帮我这个忙。 Thank you. 谢谢。

In updateData() you are creating a new URL from an empty string. updateData()您将从一个空字符串创建一个新URL This is what is throwing your exception. 这就是引发您异常的原因。

You can see that on the first line of updateData() you define csvFilename to be an empty string. 您可以看到在updateData()的第一行上,您将csvFilename定义为空字符串。 You never reassign a value to that variable so when you come to use it in the URL constructor it is still empty. 您永远不会为该变量重新分配值,因此当您在URL构造函数中使用它时,它仍然为空。
We can see from the URL constructor spec that a the exception you are seeing will be thrown if something is wrong with the spec. 我们可以URL构造器规范中看到,如果规范出现问题,则会抛出您所看到的异常。
What you could do is change the line String csvFilename = ""; 可以做的是更改String csvFilename = ""; to String csvFilename = path; String csvFilename = path; , and see if that fixes your issue. ,看看是否可以解决您的问题。

In fact, though, since you are rewriting your URL variable straight after the try catch you should just be able to remove that block and avoid the issue completely. 不过,实际上,由于您是在try catch之后立即重写URL变量,因此您应该能够删除该块并完全避免该问题。 Try this: 尝试这个:

public void updateData(String path){
    String csvFilename = "";
    URL url = VisualizationPanel.class.getResource(path);
    CSVData data = CSVData.loadFromFile(url.toString()); //loads the csv data
    VisualizationPanel visPanel = new VisualizationPanel(this, data); //draws the vis panel and adds the data to it
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM