2012-04-23 23 views
0

我想從組合框中取值並將其插入方法中。我的問題是該方法需要採取類型節點的變量。Java netbeans表單 - 從組合框中取值

ShortestPath.computeRoutes(jComboBoxDepFrom.getSelectedItem().toString());

當我嘗試上面的代碼中,我得到以下錯誤:

method computeRoutes in class busplanner.ShortestPath cannot be applied to given types; required: busplanner.Node found: java.lang.String reason: actual argument java.lang.String cannot be converted to busplanner.Node by method invocation conversion

+2

爲更好地幫助更快地編輯您的問題與[SSCCE](http://sscce.org/) – mKorbel 2012-04-23 11:43:03

+0

沒有一個構造函數或設置在Node類中接受一個字符串? – THelper 2012-04-26 10:40:32

回答

2

你可以放置節點的組合框,並使用渲染每個節點的文本。

jComboBoxDepFrom.setRenderer(new BasicComboBoxRenderer() { 

    @Override 
    public Component getListCellRendererComponent(JList list, 
               Object value, 
               int index, 
               boolean isSelected, 
               boolean cellHasFocus) { 
     Node node = (Node)value; 
     return super.getListCellRendererComponent(list, node.getText(), 
       index, isSelected, cellHasFocus); 
    }; 
}); 

如果Node.toString不足夠。