java - 从 ResultSet 创建一个 Object[](对于 JOptionPane 对话框)

标签 java swing sparql jena joptionpane

我有一个返回一些结果的 SPARQL 查询。我需要让用户选择接下来要使用这些结果中的哪一个。我已经使用 Scanner 对其进行了编码,但我现在想将其转换为 JOptionPane 对话框,以便它对用户更加友好。我假设它是通过 JOptionPane.showInputDialog 实现的。但是,我不知道如何以正确的格式“翻译”JOptionPane 对象列表中的 Jena ResultSet,然后提取所选元素。我怎样才能实现这个目标?

Query query = QueryFactory.create(queryString);
QueryExecution qe= QueryExecutionFactory.create(query, model);
ResultSet resultset = qe.execSelect();
ResultSet results = ResultSetFactory.copyResults(resultset); 
final ResultSet results2 = ResultSetFactory.copyResults(results);

System.out.println( "== Available Options ==" );
ResultSetFormatter.out(System.out, results, query);

System.out.println( "== Select Option ==" );
System.out.println( "== Type 0,1,2,3.. to choose Option ==" );

Scanner input = new Scanner( System.in );
final String inputs ;
inputs = input.next();

final String[] indices = inputs.split("\\s*,\\s*");

final List<QuerySolution> selectedSolutions = new ArrayList<QuerySolution>( indices.length ) {{
        final List<QuerySolution> solutions = ResultSetFormatter.toList( results2 );
        for ( final String index : indices ) {
            add( solutions.get( Integer.valueOf( index )));
        }
    }};

String s = selectedSolutions.toString();

Pattern p = Pattern.compile("#([^>]+)>");
Matcher m = p.matcher(s);

最佳答案

我不是一个 Java GUI 程序员,但根据 GUI 教程,看起来这并不太难。您只需要能够传递用户应该能够选择的内容的数组。这意味着您可能会获得结果集的这些值,但这并不太难。下面是一个例子。正如我提到的,我不是一个 Java GUI 程序员,我为此学到的东西来自教程 How to Make Dialogsjavadoc for JOptionPane 。它最终看起来或多或少像这样:

option pane screenshot

在此代码中,我没有对 JOptionPane.showInputDialog 的结果进行任何操作,但它是选定的资源。只需将其转换回 Jena Resource 即可,一切就绪。

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;

public class SPARQLJOptions {

    /**
     * Content for a model
     */
    final static String modelContent = "" +
            "@prefix : <http://example.org/>.\n" +
            ":a :p :b, :c, :d .\n" +
            ":b :p :c, :a .\n" +
            "";

    /** 
     * A query to run against the model (will return b, c, and d).
     */
    final static String query = "" +
            "prefix : <http://example.org/> \n" +
            "select ?value where { \n" +
            " :a :p ?value \n" +
            "}" +
            "";

    public static void main(String[] args) {
        // Create a model and read in the model contents
        final Model model = ModelFactory.createDefaultModel();
        model.read( new ByteArrayInputStream( modelContent.getBytes()), null, "TTL" );

        // Run the query and copy the values of ?value into an List<Object>
        final ResultSet results = QueryExecutionFactory.create( query, model ).execSelect();
        List<Object> values = new ArrayList<Object>();
        while ( results.hasNext() ) {
            values.add( results.next().get( "value" ));
        }

        // Show an input dialog whose options are the elements in the list and
        // whose default selection is the first element of the list.
        Resource choice = (Resource) JOptionPane.showInputDialog(
                null,                           // no container window
                "Select a resource",
                "Selecting Resource...",
                JOptionPane.QUESTION_MESSAGE,
                null,                           // no Icon
                values.toArray(),
                values.get(0));

        System.out.println( choice );
    }
}

关于java - 从 ResultSet 创建一个 Object[](对于 JOptionPane 对话框),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19147201/

相关文章:

java - SPARQL 选择查询在 JENA 中返回意外结果

java - 使用ImageIO.write Java实时保存jpg

java - 从文件中读取以比较字符

日期的 Java hashmap 搜索键

java - FontMetrics.stringWidth() 和 FontMetrics.getStringBounds() 有什么区别?

java - 线程循环卡住游戏窗口

java - 如何使 drawString 居中?

sparql - SPARQL 更新中的 UNION 运算符

javascript - 使用 Wikidata 获取地点的坐标

java - 如何从字符串中获取数字值?