我已经创建了一个JOptionPane.showInputDialog,以便在我的实现中有5个预定义的查询。 但是当用户选择一个选项并点击" ok"按钮面板关闭。 我希望当用户点击"确定"按钮,面板不会关闭但保持打开状态,以便用户可以根据需要选择其他查询。 我如何改变" ok"按钮动作?有解决方案吗? 我的代码如下:
...
String sparqlQueryString = new String();
String[] queryOptions = { "1. ...",
"2. ....",
"3. ...",
"4. ...",
5. ..."};
JPanel myPanel = new JPanel();
String sparqlQueries = (String)JOptionPane.showInputDialog(null, myPanel, "Predefined SparQL Queries", JOptionPane.PLAIN_MESSAGE, null, queryOptions, queryOptions[0]);
switch(sparqlQueries){
case "1. ...":
sparqlQueryString = ...
myCode
break;
case "2. ...":
sparqlQueryString = ...
myCode
break;
case "3. ...":
sparqlQueryString = ...
myCode
break;
case "4. ...":
sparqlQueryString = ...
myCode
break;
case "5. ...":
sparqlQueryString = ...
myCode
break;
if(!sparqlQueryString.isEmpty()) {
File finalResults = new File(args[1]);
try (FileWriter fileWriter = new FileWriter(finalResults)) {
StringBuilder results = new StringBuilder();
Query query = QueryFactory.create(sparqlQueryString);
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://localhost:8890/sparql", query, new PreemptiveBasicAuthenticator(new SimpleAuthenticator(virtuosoUserName, virtuosoPassword.toCharArray()), true));
ResultSet resultSet = qexec.execSelect();
List<String> columnNames = resultSet.getResultVars();
while ( resultSet.hasNext() ) {
QuerySolution freqresult = resultSet.next();
int i = 0;
for(String var : columnNames)
{
i=i+1;
results.append(var).append(":");
if (freqresult.get(var) == null)
results.append("{null}");
else if (freqresult.get(var).isLiteral()) {
results.append(freqresult.getLiteral(var).toString().substring(freqresult.getLiteral(var).toString().lastIndexOf("#")+1));
}
else {
results.append(freqresult.getResource(var).toString().substring(freqresult.getResource(var).toString().lastIndexOf("#")+1));
//results.append(" ");
}
if(i!=columnNames.size()){
results.append(';');
}
}
results.append("\n");
}
int last = results.lastIndexOf("\n");
if (last >= 0) { results.delete(last, results.length()); }
//qexec.close();
fileWriter.write(results.toString());
}
}
答案 0 :(得分:2)
不要为此使用JOptionPane,而是使用非模态JDialog(JDialog API link),保持打开状态,因为作为JDialog,您可以完全控制何时或是否关闭。这些很容易创建,类似于JFrame,除了在构造函数中,您传入父窗口,通常是JFrame,标题和ModalityType,这里是ModalityType.MODELESS。