【问题标题】:Java netbeans form - taking value from combo boxJava netbeans 表单 - 从组合框中获取值
【发布时间】:2012-04-23 11:39:36
【问题描述】:

我想从组合框中获取一个值并将其插入到方法中。我的问题是该方法需要采用 Node 类型的变量。

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

当我尝试上面的代码时,我收到以下错误:

类 busplanner.ShortestPath 中的方法 computeRoutes 不能应用于给定类型; 必需:busplanner.Node 找到:java.lang.String 原因:实参java.lang.String不能通过方法调用转换为busplanner.Node

【问题讨论】:

  • 为了更好的帮助,请尽快使用SSCCE编辑您的问题
  • Node 类中没有构造函数或设置器接受 String 吗?

标签: java string swing netbeans jcombobox


【解决方案1】:

您可以将节点放置在组合框中,并为每个节点的文本使用渲染器。

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 不够用。

【讨论】:

    猜你喜欢
    • 2011-08-16
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多