如何根据第一个组合框选择过滤第二个组合框中的内容

时间:2018-05-29 01:54:17

标签: java jframe jcombobox netbeans-8 ui-design

我想在用户选择本科或研究生时过滤学位。我通过互联网搜索,但我找不到一个示例代码的明确答案。

      private String[] itemsUndergraduate = new String[]{"Computer Science", "Software Engineering"};
    private String[] itemsPostgraduate = new String[]{"BA", "Msc"};
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
   UPselect.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
    String[] itemsUndergraduate = new String[]{"Computer Science", "Software Engineering"};
    String[] itemsPostgraduate = new String[]{"BA", "Msc"};
    String s = (String) UPselect.getSelectedItem();
    if (s.equals("Undergraduate Degrees")){
        //Assign the first list to the combobox
        jComboBox1 = new JComboBox(itemsUndergraduate);
    }
    else{
        //Assign the second list to the combobox
        jComboBox1 = new JComboBox(itemsPostgraduate);
    }
}

});

这是我的代码到目前为止,我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

回应您的评论和更新的代码,是的,您走的是正确的道路。

这是一个例子。首先,我们需要有两个我们以后可以使用的列表。

String[] itemsUndergraduate = new String[]{"Computer Science", "Software Engineering"};
String[] itemsPostgraduate = new String[]{"BA", "Msc"};

现在,当选择第一个组合框时,我们可以更改第二个组合框的内容以匹配其中一个列表:

UPselect.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e){
        String s = (String) UPselect.getSelectedItem();

        //Added this line to help you debug the code
        System.out.print("Does this bit of code ever happen??");
        System.out.print("Value of selected item is: "+s);

        if (s.equals("Undergraduate Degrees")){
            //Assign the first list to the combobox
            jComboBox1 = new JComboBox(itemsUndergraduate);
        }
        else{
            //Assign the second list to the combobox
            jComboBox1 = new JComboBox(itemsPostgraduate);
        }
    }
}

答案 1 :(得分:1)

优良作法是在模型上建模和修改数据,而不是直接更新UI,以下是使用Build command failed. Error while executing process /Users/bhaskarrajaryal/Library/Android/sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /Users/bhaskarrajaryal/AndroidStudioProjects/wowtime/app/.externalNativeBuild/cmake/debug/armeabi-v7a --target baseUrl} [1/1] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libbaseUrl.so FAILED: : && /Users/bhaskarrajaryal/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=armv7-none-linux-androideabi --gcc-toolchain=/Users/bhaskarrajaryal/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/bhaskarrajaryal/Library/Android/sdk/ndk-bundle/sysroot -fPIC -isystem /Users/bhaskarrajaryal/Library/Android/sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=16 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot /Users/bhaskarrajaryal/Library/Android/sdk/ndk-bundle/platforms/android-16/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--fix-cortex-a8 -Wl,--exclude-libs,libunwind.a -L/Users/bhaskarrajaryal/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libbaseUrl.so -o ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libbaseUrl.so CMakeFiles/baseUrl.dir/src/cpp/baseUrl.cpp.o -llog -latomic -lm "/Users/bhaskarrajaryal/Library/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/libgnustl_static.a" && : /Users/bhaskarrajaryal/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/include/stdexcept:136: error: undefined reference to 'std::logic_error::logic_error(char const*)' clang++: error: linker command failed with exit code 1 (use -v to see invocation) ninja: build stopped: subcommand failed. 的相同示例。

DefaultComboBoxModel

发布时:without selection

On Change:enter image description here