2014-02-06 91 views
0

我對Java GUI的製作非常陌生,並且試圖在此嘗試。基本上我有另一個班級從Excel表格讀取關於自動化測試包的數據。我想要做的是將這些測試添加到此資源樹中。但是,當我嘗試使用JTree - 無法轉換爲javax.swing.tree.MutableTreeNode

addTest(new testInfo 
     ("TESTING", 
      "TESTING MESSAGE")); 

它似乎在抱怨。我的代碼:

package com.main; 

import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.io.IOException; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.List; 

import javax.swing.JEditorPane; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JSplitPane; 
import javax.swing.JTree; 
import javax.swing.UIManager; 
import javax.swing.event.TreeSelectionEvent; 
import javax.swing.event.TreeSelectionListener; 
import javax.swing.tree.DefaultMutableTreeNode; 
import javax.swing.tree.MutableTreeNode; 
import javax.swing.tree.TreeSelectionModel; 

public class ExcelReaderGui extends JPanel 
implements TreeSelectionListener { 
/** 
* 
*/ 
private static final long serialVersionUID = 1L; 
private JEditorPane htmlPane; 
private JTree tree; 
private URL helpURL; 
private static boolean DEBUG = false; 
private static ReadExcel re = new ReadExcel(); 
private List<testInfo> testsToAdd = new ArrayList<testInfo>();; 

//Optionally play with line styles. Possible values are 
//"Angled" (the default), "Horizontal", and "None". 
private static boolean playWithLineStyle = false; 
private static String lineStyle = "Horizontal"; 

//Optionally set the look and feel. 
private static boolean useSystemLookAndFeel = false; 

public ExcelReaderGui() { 
    super(new GridLayout(1,0)); 

    //Create the nodes. 
    addTest(new testInfo 
      ("TESTING", 
       "TESTING MESSAGE")); 
    DefaultMutableTreeNode top = 
      new DefaultMutableTreeNode("Automated tests"); 
    createNodes(top); 


    //Create a tree that allows one selection at a time. 
    tree = new JTree(top); 
    tree.getSelectionModel().setSelectionMode 
    (TreeSelectionModel.SINGLE_TREE_SELECTION); 

    //Listen for when the selection changes. 
    tree.addTreeSelectionListener(this); 

    if (playWithLineStyle) { 
     System.out.println("line style = " + lineStyle); 
     tree.putClientProperty("JTree.lineStyle", lineStyle); 
    } 

    //Create the scroll pane and add the tree to it. 
    JScrollPane treeView = new JScrollPane(tree); 

    //Create the HTML viewing pane. 
    htmlPane = new JEditorPane(); 
    htmlPane.setEditable(false); 
    initHelp(); 
    JScrollPane htmlView = new JScrollPane(htmlPane); 

    //Add the scroll panes to a split pane. 
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); 
    splitPane.setTopComponent(treeView); 
    splitPane.setBottomComponent(htmlView); 

    Dimension minimumSize = new Dimension(100, 50); 
    htmlView.setMinimumSize(minimumSize); 
    treeView.setMinimumSize(minimumSize); 
    splitPane.setDividerLocation(150); 
    splitPane.setPreferredSize(new Dimension(700, 400)); 

    //Add the split pane to this panel. 
    add(splitPane); 
} 

/** Required by TreeSelectionListener interface. */ 
public void valueChanged(TreeSelectionEvent e) { 
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) 
      tree.getLastSelectedPathComponent(); 

    if (node == null) return; 

    Object nodeInfo = node.getUserObject(); 
    if (node.isLeaf()) { 
     testInfo book = (testInfo)nodeInfo; 
     displayURL(book.resultOfTest); 
     if (DEBUG) { 
      System.out.print(book.resultOfTest + ": \n "); 
     } 
    } else { 
     displayURL("Drill down in the resource trees above to find tests with their results"); 
    } 
    if (DEBUG) { 
     System.out.println(nodeInfo.toString()); 
    } 
} 

private class testInfo { 
    public String testName; 
    public String resultOfTest; 

    public testInfo(String test, String testResult) { 
     testName = test; 
     resultOfTest = testResult; 
    } 

    public String toString() { 
     return testName; 
    } 
} 

private void initHelp() { 
    String s = "TreeDemoHelp.html"; 
    helpURL = getClass().getResource(s); 
    if (helpURL == null) { 
     System.err.println("Couldn't open help file: " + s); 
    } else if (DEBUG) { 
     System.out.println("Help URL is " + helpURL); 
    } 

    displayURL("Drill down in the resource trees above to find tests with their results"); 
} 

private void displayURL(String url) { 
    htmlPane.setText(url); 
} 

public void addTest(testInfo testToAdd) { 
    testsToAdd.add(testToAdd); 
} 

private void createNodes(DefaultMutableTreeNode top) { 
    DefaultMutableTreeNode category = null; 
    DefaultMutableTreeNode test = null; 

    category = new DefaultMutableTreeNode("14 February 2014"); 
    top.add(category); 

    for (testInfo newTest : testsToAdd) { 
     category.add((MutableTreeNode) newTest); 
    } 

    category = new DefaultMutableTreeNode("13 february 2014"); 
    top.add(category); 

    //TODO Tests for previous dates 
} 

/** 
* Create the GUI and show it. For thread safety, 
* this method should be invoked from the 
* event dispatch thread. 
*/ 
private static void createAndShowGUI() { 
    if (useSystemLookAndFeel) { 
     try { 
      UIManager.setLookAndFeel(
        UIManager.getSystemLookAndFeelClassName()); 
     } catch (Exception e) { 
      System.err.println("Couldn't use system look and feel."); 
     } 
    } 

    //Create and set up the window. 
    JFrame frame = new JFrame("My Excel Reader"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //Add content to the window. 
    frame.add(new ExcelReaderGui()); 

    //Display the window. 
    frame.pack(); 
    frame.setVisible(true); 
} 

public static void main(String[] args) { 
    //Schedule a job for the event dispatch thread: 
    //creating and showing this application's GUI. 
    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       re.setInputFile("D:\\Users\\liagordo\\java\\RMG\\dashboard.xls"); 
       re.read(); 
      } catch (IOException e) { 
       System.out.println("Error reading excel file"); 
       e.printStackTrace(); 
      } 
      createAndShowGUI(); 
     } 
    }); 
} 
} 

當我運行上面的代碼我收到錯誤:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: 
com.main.ExcelReaderGui$testInfo cannot be cast to javax.swing.tree.MutableTreeNode 
at com.main.ExcelReaderGui.createNodes(ExcelReaderGui.java:157) 
at com.main.ExcelReaderGui.<init>(ExcelReaderGui.java:53) 
at com.main.ExcelReaderGui.createAndShowGUI(ExcelReaderGui.java:186) 
at com.main.ExcelReaderGui.access$1(ExcelReaderGui.java:171) 
at com.main.ExcelReaderGui$1.run(ExcelReaderGui.java:205) 
+0

1)您需要指定異常指向的代碼行。 2)你對這條信息有什麼不瞭解? –

回答

0

我討厭它,當我花了年齡試圖找到答案,然後我將它張貼在堆棧溢出,答案過了一會兒,我就想到了。無論如何,以解決這個問題,我改變:

for (testInfo newTest : testsToAdd) { 
    category.add((MutableTreeNode) newTest); 
} 

要:

for (testInfo newTest : testsToAdd) { 
test = new DefaultMutableTreeNode(newTest); 
category.add(test); 
} 
+0

使用Java命名約定。類名以大寫字母開頭。 –

1

你想投testInfo對象爲MutableTreeNode在此代碼

category.add((MutableTreeNode) newTest); 

是impossible.because投testInfoMutableTreeNodetestInfoMutableTreeNode應該有一個繼承關係,但在你的代碼中沒有y之間的繼承關係

+0

我已經找到了解決方案,但是我給了你一個投票解釋,謝謝 – CustomNet

相關問題