2012-04-23 8 views
0

コンボボックスから値を取り出し、メソッドに挿入したいと考えています。私の問題は、メソッドがNode型の変数を取る必要があることです。私は上記のコードをしようとすると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

+0

Nodeクラスに文字列を取るコンストラクタまたはセッターはありませんか? – THelper

答えて

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で十分でない場合。

関連する問題