简体   繁体   中英

multiple JComboBox in one JPanel

I have a JPanel with three JComboBox. Here is the code I wrote:

public class Main {

private static String pat_order;
private static String PS_selection;
private static String ovlp_selection;


public static void main(String args[]) throws FileNotFoundException, IOException {
    Date start_time = new Date();
    try {
        GridBagConstraints gbc = new GridBagConstraints();
        final JComboBox jc = new JComboBox();
        jc.addItem("ARR");
        jc.addItem("SRR");

        final JComboBox jc1 = new JComboBox();
        jc1.addItem("RR");
        jc1.addItem("IQC");

        final JComboBox jc2 = new JComboBox();
        jc2.addItem("YES");
        jc2.addItem("NO");

        JPanel myPanel = new JPanel(new GridBagLayout());
        myPanel.add(jc, gbc); 
        myPanel.add(jc1, gbc); 
        myPanel.add(jc2, gbc);

        jc.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent ie) {
                String order = (String) jc.getSelectedItem();
                pat_order = order;
            }
        });


        jc1.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent ie) {

                String PS_method = (String) jc1.getSelectedItem();
                PS_selection = PS_method;
            }
        });


        jc2.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent ie) {

                String ovlp_user = (String) jc2.getSelectedItem();
                ovlp_selection = ovlp_user;
            }
        });


        if (pat_order == "ARR") {
            Arrays.sort(patterns_array, new ColumnComparator(0));
        } else if (pat_order == "SRR") {
            Arrays.sort(patterns_array, new ColumnComparator(1));
        }

       if (PS_selection == "RR") {
           System.out.println("RR");
                        } else if (PS_selection == "IQC") {
                              System.out.println("IQC");
                            }
       if (ovlp_selection == "YES") {
            Overlap a = new Overlap(Xdisc, final_patterns, k, Yresid, Xresid, projectname, pat_order, PS_selection);
        }

   } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    } finally {
    }
    }
    }

The problem is that the first JComboBox is working fine, but the second and third is doing nothing. It would be great if you can help me in this problem.

You seem to be running your UI as if it was a console program. UI's don't work this way, they respond to events. These events may come in any order and at any time...

Dialogs are a great way of control the flow of the execution. They will block code until the dialog is closed, allowing you to ascertain the results and take appropriate action.

Take a look at How to use dialogs for more details...

While you there, you might also want to take a look through Creating a UI with Swing which will explain more of the concepts you need to understand.

Like the fact that a UI needs some kind of Window in order to be displayed on the screen

尝试添加ActionListener而不是ItemListener

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM