2011-10-30 5 views
1

ScalaSwing JTreeコンポーネント(Java)の間の相互運用性に問題があります。JTreeノードが更新されない(Scalaで)

JTreeは正しく更新されませんが、JOptionPaneの表示を停止して新しいエンティティの名前を入力する必要があります。この行には[* * *]とマークされています。ご覧のとおり、JOptionPaneメソッドの呼び出しをコメントアウトして、静的テキスト "xxx"を提供しました。この場合、JTreeは正しく更新されます。

スイングスレッドモデルとは関係があると思っていましたが、アップデートテキストをRunnableクラスにラップしても問題は解決されません。関連項目Why isn't my JTree updating when the TreeModel adds new nodes?

なぜJTreeがJTreeによってノードを正しく更新できないのですか?

私はScalaに動的更新を可能にするSwing Treeの実装がまだないため、このようにしています。 http://github.com/kenbot/ScalaSwingTreeWrapper

ヒントや指摘をいただければ幸いです。

乾杯、

import scala.swing._ 
    import javax.swing.{JOptionPane,JTree,SwingUtilities} 
    import javax.swing.tree.{DefaultTreeModel,DefaultMutableTreeNode,TreePath} 

    object XApp extends SimpleSwingApplication { 

     val APP_NAME: String = "AppName" 

     def getNameDialog(q: String): String = 
     { 
      JOptionPane.showInputDialog(top.self, q, APP_NAME, JOptionPane.PLAIN_MESSAGE); 
     } 

     def menuProjectNewX = { 
      // Get the name of the X 
      var name = "xxx"; // getNameDialog ("Enter the name of the X:") [***] 

      def doUpdate = new Runnable() { 
       def run() 
       { 
        pl ("Running"); 

        // Get the root 
        var root = treeModel.getRoot().asInstanceOf[DefaultMutableTreeNode] 

        // Insert new object 
        var newNode = new DefaultMutableTreeNode(name) 
        treeModel.insertNodeInto(newNode, root, root.getChildCount()) 

        // Expand the tree 
        var tp = new TreePath(newNode.getPath().asInstanceOf[Array[Object]]) 
        tree.scrollPathToVisible(tp) 
       } 
      } 

      SwingUtilities.invokeLater(doUpdate); 
     } 

     var tree: JTree = null 
     var treeModel: DefaultTreeModel = null 
     var flow: FlowPanel = null 

     def top = new MainFrame { 

      // Create the menu bar 
      menuBar = new MenuBar() { 
       contents += new Menu("Project") { 
        contents += new MenuItem(Action("New X...")  { menuProjectNewX }) 
       } 
      } 

      title = APP_NAME 
      preferredSize = new Dimension (1000, 800) 
      location = new Point(50,50) 


      treeModel = new DefaultTreeModel(new DefaultMutableTreeNode("(root)")) 
      tree = new JTree(treeModel) 
      //flow = new FlowPanel 

      var splitPane = new SplitPane (Orientation.Vertical, new Component { 
        override lazy val peer = tree 
       }, new FlowPanel) 

      splitPane.dividerLocation = 250 
      contents = splitPane 

     }  

    } 

答えて

1

ナイジェル問題がJOptionPaneが示されるたびに、新しいフレームを作成し、代わりにメインフレームを再利用しているということです。 topは方法であり、JOptionPaneを表示するには「トップ」を参照すると、new MainFrameが作成されます。したがって、最後に、表示されているものとは別のMainFrameにあるツリーにノードを追加します。これを解決する

一つの方法は、単に変数にメインフレームを格納することです:

var mainFrame: MainFrame = null 

def top = 
    { 
    mainFrame = new MainFrame { 
     // Rest of the code 
    } 

    mainFrame 
    } 
} 

// To show the JOptionPane 
JOptionPane.showInputDialog(mainFrame.self, q, APP_NAME, JOptionPane.PLAIN_MESSAGE); 
+0

どうもありがとうございました...私は定義されたメソッドを毎回呼び出すのではなく、保存された使用していたことに気づきませんでした変数。 : - /乾杯! –

関連する問題