【问题标题】:how to modify the values of other fields in JDialog box based on the value in combo box如何根据组合框中的值修改JDialog框中其他字段的值
【发布时间】:2010-09-24 18:18:01
【问题描述】:

我正在为应用程序制作一个对话框。我想根据组合框的值从其他字段(例如 textField)中获取值。谁能告诉我如何链接这两个组件? -提前致谢

【问题讨论】:

  • “其他字段”和“组合框”是否在同一个“对话框”中?
  • 是的,两者都在同一个对话框中。
  • 查看我对类似问题的回答stackoverflow.com/questions/3787056/…

标签: java swing jcombobox jtextfield jdialog


【解决方案1】:

没有神奇的方法可以将组件“链接”在一起。根据您的问题,我了解到您想根据当前选择的组合框或类似的东西来解释文本字段中的数据?因此,当您读取数据时,请使用JComboBox.getSelectedItem()/getSelectedIndex() 应用您的逻辑。

如果您想更改其他字段中的数据,或禁用它们,请根据当前选择添加监听器:

comboBox.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        int index = combo.getSelectedIndex();
        if (index == 0) {
            //disable some textfields or change format if it's a JFormattedField
        } 
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 2014-12-03
    • 2017-05-20
    相关资源
    最近更新 更多