2017-05-30 70 views
1

我想讓一個節點成爲一個可點擊的URL,但我似乎無法弄清楚如何。Java JTree節點是一個可點擊的URL鏈接

我搜索了高和低,我似乎無法找到解決方案。

這是我的代碼:

public class NyttigeLinks { 

    private static JFrame nyttigeLinks; 

    public static void main(String[] args) { 

        initialize(); 
      } 

    public NyttigeLinks() { 

    } 

    private static void initialize() { 
     nyttigeLinks = new JFrame(); 
     nyttigeLinks.setBounds(new Rectangle(0, 0, 350, 650)); 
     nyttigeLinks.getContentPane().setBounds(new Rectangle(0, 0, 350, 650)); 
     nyttigeLinks.getContentPane().setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 
     nyttigeLinks.getContentPane().setLayout(null); 

     JLabel logoLabel = new JLabel(""); 
     logoLabel.setIcon(new ImageIcon(NyttigeLinks.class.getResource("/images/ssiLogo.jpg"))); 
     logoLabel.setBounds(0, 0, 350, 60); 

     JTree tree = new JTree(); 

     nyttigeLinks.getContentPane().add(logoLabel); 

     JScrollPane scrollPane = new JScrollPane(tree); 
     scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
     scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 
     scrollPane.setBounds(10, 71, 324, 508); 
     nyttigeLinks.getContentPane().add(scrollPane); 

     tree.setModel(new DefaultTreeModel(
      new DefaultMutableTreeNode("Nyttige Links\t") { 
       { 
        DefaultMutableTreeNode node_1; 
        node_1 = new DefaultMutableTreeNode("Projekt Wiki"); 
         node_1.add(new DefaultMutableTreeNode("AO")); 
         node_1.add(new DefaultMutableTreeNode("Attends")); 
         node_1.add(new DefaultMutableTreeNode("Carlsberg")); 
         node_1.add(new DefaultMutableTreeNode("COOP")); 
         node_1.add(new DefaultMutableTreeNode("Dafgaard")); 
         node_1.add(new DefaultMutableTreeNode("Jysk DK")); 
         node_1.add(new DefaultMutableTreeNode("Jysk SE")); 
         node_1.add(new DefaultMutableTreeNode("Kvadrat")); 
         node_1.add(new DefaultMutableTreeNode("Solar")); 
         node_1.add(new DefaultMutableTreeNode("Stockmann")); 
         node_1.add(new DefaultMutableTreeNode("Tine")); 
         node_1.add(new DefaultMutableTreeNode("Unicef")); 
         node_1.add(new DefaultMutableTreeNode("Vectura")); 
        add(node_1); 
        node_1 = new DefaultMutableTreeNode("Helpdesk Norcic"); 
         node_1.add(new DefaultMutableTreeNode("Test")); 
        add(node_1); 
        node_1 = new DefaultMutableTreeNode("Test"); 
         node_1.add(new DefaultMutableTreeNode("AO")); 
         node_1.add(new DefaultMutableTreeNode("Attends")); 
         node_1.add(new DefaultMutableTreeNode("Carlsberg")); 
         node_1.add(new DefaultMutableTreeNode("COOP")); 
         node_1.add(new DefaultMutableTreeNode("Dafgaard")); 
         node_1.add(new DefaultMutableTreeNode("Jysk DK")); 
         node_1.add(new DefaultMutableTreeNode("Jysk SE")); 
         node_1.add(new DefaultMutableTreeNode("Kvadrat")); 
         node_1.add(new DefaultMutableTreeNode("Solar")); 
         node_1.add(new DefaultMutableTreeNode("Stockmann")); 
         node_1.add(new DefaultMutableTreeNode("Tine")); 
         node_1.add(new DefaultMutableTreeNode("Unicef")); 
         node_1.add(new DefaultMutableTreeNode("VecturaAO")); 
         node_1.add(new DefaultMutableTreeNode("Attends")); 
         node_1.add(new DefaultMutableTreeNode("Carlsberg")); 
         node_1.add(new DefaultMutableTreeNode("COOP")); 
         node_1.add(new DefaultMutableTreeNode("Dafgaard")); 
         node_1.add(new DefaultMutableTreeNode("Jysk DK")); 
         node_1.add(new DefaultMutableTreeNode("Jysk SE")); 
         node_1.add(new DefaultMutableTreeNode("Kvadrat")); 
         node_1.add(new DefaultMutableTreeNode("Solar")); 
         node_1.add(new DefaultMutableTreeNode("Stockmann")); 
         node_1.add(new DefaultMutableTreeNode("Tine")); 
         node_1.add(new DefaultMutableTreeNode("Unicef")); 
         node_1.add(new DefaultMutableTreeNode("VecturaAO")); 
         node_1.add(new DefaultMutableTreeNode("Attends")); 
         node_1.add(new DefaultMutableTreeNode("Carlsberg")); 
         node_1.add(new DefaultMutableTreeNode("COOP")); 
         node_1.add(new DefaultMutableTreeNode("Dafgaard")); 
         node_1.add(new DefaultMutableTreeNode("Jysk DK")); 
         node_1.add(new DefaultMutableTreeNode("Jysk SE")); 
         node_1.add(new DefaultMutableTreeNode("Kvadrat")); 
         node_1.add(new DefaultMutableTreeNode("Solar")); 
         node_1.add(new DefaultMutableTreeNode("Stockmann")); 
         node_1.add(new DefaultMutableTreeNode("Tine")); 
         node_1.add(new DefaultMutableTreeNode("Unicef")); 
         node_1.add(new DefaultMutableTreeNode("Vectura")); 
        add(node_1); 
       } 
      } 
     )); 
     tree.setBounds(10, 71, 324, 540); 
     tree.setRootVisible(false); 

     JLabel bottomLabelTop = new JLabel("     Nyttige Links Version 1.0"); 
     bottomLabelTop.setBounds(0, 590, 230, 14); 
     nyttigeLinks.getContentPane().add(bottomLabelTop); 

     JLabel bottomLabelBot = new JLabel("        Made by xxx"); 
     bottomLabelBot.setBounds(0, 605, 230, 15); 
     nyttigeLinks.getContentPane().add(bottomLabelBot); 

     JButton btnNewButton = new JButton("Admin"); 
     btnNewButton.setIcon(new ImageIcon(NyttigeLinks.class.getResource("/images/appIcon.ico"))); 
     btnNewButton.setBounds(240, 590, 80, 20); 
     nyttigeLinks.getContentPane().add(btnNewButton); 
     nyttigeLinks.setPreferredSize(new Dimension(350, 650)); 
     nyttigeLinks.setSize(new Dimension(350, 650)); 
     nyttigeLinks.setResizable(false); 
     nyttigeLinks.setTitle("Nyttige Links"); 
     nyttigeLinks.setIconImage(Toolkit.getDefaultToolkit().getImage(NyttigeLinks.class.getResource("/images/appIcon.ico"))); 
     nyttigeLinks.setBackground(Color.YELLOW); 
     nyttigeLinks.setBounds(100, 100, 350, 650); 
     nyttigeLinks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     nyttigeLinks.setVisible(true); 
    } 
} 

我爲是有點小白的道歉,但你必須從某個地方開始,對不對?

如果有人能指引我正確的方向,我將非常感激!祝大家晚安!

回答

1

我不認爲有一種方法可以使節點本身成爲一個URL。但是,您可以通過使用偵聽器在單擊節點時打開瀏覽器來完成同樣的事情。要打開瀏覽器,可以使用Desktop#browse(URI)方法。

關於點擊的話題,我建議在單擊節點時不要打開URL。對於一個非常煩人的用戶(IMO)。相反,我建議使用雙擊。 This answer提供了區分單擊和雙擊的好方法,以及如何將它添加到Tree


[...]但你得開始的地方,對不對?

正是!請允許我在這裏提供一些更詳細的信息,希望對學習更多內容有所幫助。

您將面臨的一個挑戰是如何知道給定節點打開哪個URL。目前您只知道顯示的String,我認爲這是一個安全的假設,您不希望爲每個節點顯示完整的URL。我建議不要使用String參數創建DefaultMutableTreeNode對象,而是建議創建一個對象來代替傳遞給DefaultMutableTreeNode構造函數。該對象還可以具有URI屬性,因此它知道要打開哪個URL。

例如:

public class LeafNodeObject { 
    private final URI uri; 

    public LeafNodeObject(final String display, final URI uri) { 
     this.display = display; 
     this.uri = uri; 
    } 

    /** 
    * Override so that we control what is display on the Node 
    */ 
    @Override 
    public String toString() { 
     return display; 
    } 

    public void onDoubleClick() { 
     try { 
      Desktop.getDesktop().browse(uri); 
     } catch (final IOException e) { 
      e.printStackTrace(); 
     } 
    } 

} 

節點可以添加像這樣:

node_1.add(new DefaultMutableTreeNode(new LeafNodeObject("Test", new URI("www.eclipse.org")))); 

而在我們的監聽器(略從上面的鏈接修改):

final MouseListener ml = new MouseAdapter() { 
    public void mousePressed(final MouseEvent e) { 
     final int selRow = tree.getRowForLocation(e.getX(), e.getY()); 
     final TreePath selPath = tree.getPathForLocation(e.getX(), e.getY()); 
     if (selRow != -1) { 
      final DefaultMutableTreeNode node = (DefaultMutableTreeNode) selPath.getLastPathComponent(); 
      if (e.getClickCount() == 1) { 
       // Single click 
      } else if (e.getClickCount() == 2) { 
       // Double click 
       ((LeafNodeObject) node.getUserObject()).onDoubleClick(); 
      } 
     } 
    } 
}; 

現在,當有一個雙擊事件,我們把onDoubleClick()方法稱爲LeafNodeObject t我們創造的帽子。

這看起來不錯,只是我們會遇到非葉節點節點的類轉換問題!爲了解決這個問題,我們可以爲這些節點創建一個類似的類(例如,ParentNodeObject)。爲了獲得良好的面向對象,我們應該認識到這兩個類的共同行爲(onDoubleClick()onSingleClick()),併爲共享能力創建一個接口。

例如:

public interface NodeObject { 
    public void onSingleClick(); 
    public void onDoubleClick(); 
} 

現在我們可以有我們的兩個類實現這個接口:

public class ParentNodeObject implements NodeObject { 
    private final String display; 

    public ParentNodeObject(final String display) { 
     this.display = display; 
    } 

    /** 
    * Override so that we control what is display on the Node 
    */ 
    @Override 
    public String toString() { 
     return display; 
    } 

    @Override 
    public void onSingleClick() { 
     // Do nothing 
    } 

    @Override 
    public void onDoubleClick() { 
     // Do nothing 
    } 
} 

public class LeafNodeObject implements NodeObject { 
    private final URI uri; 

    public LeafNodeObject(final String display, final URI uri) { 
     this.display = display; 
     this.uri = uri; 
    } 

    /** 
    * Override so that we control what is display on the Node 
    */ 
    @Override 
    public String toString() { 
     return display; 
    } 

    @Override 
    public void onSingleClick() { 
     // Do nothing 
    } 

    @Override 
    public void onDoubleClick() { 
     try { 
      Desktop.getDesktop().browse(uri); 
     } catch (final IOException e) { 
      e.printStackTrace(); 
     } 
    } 

} 

其結果是,在我們的聽衆,我們不再需要知道(和不再在乎)節點是父節點還是葉節點。我們可以簡單地調用onSingleClick()onDoubleClick(),實現處理其餘的!

if (e.getClickCount() == 1) { 
    // Single click 
    ((NodeObject) node.getUserObject()).onSingleClick(); 
} else if (e.getClickCount() == 2) { 
    // Double click 
    ((NodeObject) node.getUserObject()).onDoubleClick(); 
} 

回到你的代碼,父和子節點可以被添加像這樣:

node_1 = new DefaultMutableTreeNode(new ParentNodeObject("Helpdesk Norcic")); 
node_1.add(new DefaultMutableTreeNode(new LeafNodeObject("Test", new URI("www.eclipse.org")))); 

現在,當你是單身或雙擊任一節點,無論是onSingleClick()onDoubleClick()方法也叫做。在這種情況下,我們只關心在葉節點上雙擊時發生的情況,所以我們只需要填寫該方法。

+0

太棒了!非常感謝你的幫助。我明天早上下班的時候會考慮一下。我今晚沒有時間了。非常感謝,我一定會更新這個主題。 – TheGreyNight

+0

@ TheGreyNight不客氣!讓我知道是否有任何不完整的意義或可以使用更好的解釋。 – avojak

0

雖然我還沒有檢查上述代碼,這裏是我解決這個問題是怎麼當我在等待此主題回覆:

class SelectionListener implements TreeSelectionListener { 

    public void valueChanged(TreeSelectionEvent se) { 
     JTree tree = (JTree) se.getSource(); 
     DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); 
     String selectedNodeName = selectedNode.toString(); 
     if (selectedNode.isLeaf()) { 
      if (selectedNodeName == "Unicef") { 
       try { 
        java.awt.Desktop.getDesktop().browse(java.net.URI.create("http://www.eb.dk")); 
       } catch (IOException e1) { 
        // make a error pop up appear here 
        JOptionPane.showMessageDialog(null, "Something went wrong, please report this to the developer!", "Something went wrong", 0); 
        e1.printStackTrace(); 

       } 
      } 

      if (selectedNodeName == "Vectura") { 
       try { 
        java.awt.Desktop.getDesktop().browse(java.net.URI.create("http://www.google.com")); 
       } catch (IOException e1) { 
        // make a error pop up appear here 
        JOptionPane.showMessageDialog(null, "Something went wrong, please report this to the developer!", "Something went wrong", 0); 
        e1.printStackTrace(); 

       } 
      } 

     } 

    } 
} 

這絕對不是這樣做的一個很好的方法,因爲它會導致大量的if語句,但它足夠簡單讓我自己理解。