0

スレッドの使用に関していくつか質問したいのですが。私はそれらの投稿から提案された多くの投稿とリンクを見てきましたが、それでも空白になりました。いくつかのクラスがあるNetBeansプロジェクトがあります。そのうちの1つは、ボタンをクリックするだけでいくつかの処理が実行されるGuiクラスです。GUIから、別のクラスのインスタンスを呼び出し、次に他のクラスを呼び出します。これらのクラスの1つは、SparqlクエリをTDBバックエンドデータベースに送信します。今のところ、すべての出力はファイルに保存されます。

私がやりたいのは、どういうわけか、Guiから呼び出されたクラスを別のスレッドで実行するようにし、呼び出されたクラスの1つ以上からGuiのEditorPaneとTextAreaを更新できるようにすることです。これまで、Guiクラスのインスタンスを呼び出して、その中でパブリックメソッドを使用しようとしましたが、これは機能しません。インスタンスをGuiと呼んでいます

Gui gui = new Gui();
gui.setEditorPaneText("File name is: " + fn);

そして、Guiクラスのメソッドは

public void setEditorPaneText(final String string) {
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            setString(string);
            EditorPane.setText(getString());
            EditorPane.repaint();
        }
    });
}

デバッガーを実行しようとしましたが、メソッド内のコードを処理せずに、メソッドの最初の行から最後の中括弧まで処理がスキップされます。私のGuiクラスには、メインメソッドとして次のものがあります。コメントされた部分は、この問題に関する多数の投稿を読んでいるときに変更したイベントキューの以前のバージョンでした。

public static void main(String args[]) {
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            new Gui().setVisible(true);
            throw new UnsupportedOperationException("Not supported yet.");
        }
    });
}

以下は、この問題に関するいくつかの投稿を読んだ後に置き換えたmainメソッドの以前のコードです。

java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new Gui().setVisible(true);
        }
    });

役立つ情報をいただければ幸いです。ありがとうございました。

4

1 に答える 1

0

主なエラーは、Gui クラスの 2 つのインスタンスを作成することだと思います。次のスニペットが 2 回あります: new Gui(). 以下のサンプル コードを見て、Gui をワーカー スレッドに渡す方法の例を確認してください。

// This is handwritte-untested-uncompiled code to show you what I mean
public class Main {
  public static void main(String[]args) {
     SwingUtilities.invokeLater(new Runnable() {
        public void run() {
           Gui g = new Gui();
           g.show(); // show() is equal to setVisible(true)
           g.doBackendAction(); // Normally this would be invoked by a button or sthg. I was to lazy
        }
     });
  }
}


public class Gui extends JFrame {
  private JTextArea area;
  public Gui() {
    // Just some code to create the UI. Not sure if this actually does sthg right :P
    area = new JTextArea();
    setContentPane(area);
    pack();
  }


  public void setTextAreaContent(final String string) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            area.setText(string);
            this.repaint(); // Not 100% sure if we need this

        }
    });
  }

  public void doBackgroundWork() {
    BackgroundWorker w = new BackgroundWorker(this);
    new Thread(w).start(); // Start a worker thread
  }
}

public class BackgroundWorker implements Runnable {
   private Gui gui;
   public BackgroundWorker(Gui gui) {
     this.gui = gui; // we take the initial instance of Gui here as a parameter and store it for later
   }

   public void run() {
     try { Thread.sleep(10 * 1000); } catch (InterruptedException e) {; }
     this.gui.setTextAreaContent("Hello World!"); // calls back to the Gui to set the content 

   }
}
于 2011-08-06T08:41:13.307 に答える