简体   繁体   中英

Getting CheckBox input from JOptionPane

I am making a swing app and I have am having trouble getting the input from my check box which I have embedded inside a JOptionPane

At the moment I click a button (comparison) which loads the JOptionPane and I select the two animations to run. I am thinking the reason this won't work is because I am getting the source of the button and not the embedded JCheckBox is there anyway of doing this ??

comparison.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

               JPanel a1=new JPanel();
               a1.add(bubbleCheckBox);
               a1.add(quickCheckBox);
               a1.add(insertionCheckBox);
               a1.add(selectionCheckBox);
               a1.add(mergeCheckBox);
              JOptionPane.showConfirmDialog(null, a1);

                  Object buttonPressed =e.getSource();

        if (buttonPressed.equals(insertionCheckBox)&&buttonPressed.equals(bubbleCheckBox)) {
            SortAnimator animator = new SortAnimator(new InsertionSorter(),new BubbleSorter());

        }else if(buttonPressed.equals(insertionCheckBox)&&buttonPressed.equals(quickCheckBox)){
         //    SortAnimator animator = new SortAnimator(new InsertionSorter(),new quickSorter());

        }else if(buttonPressed.equals(insertionCheckBox)&&buttonPressed.equals(selectionCheckBox)){
             SortAnimator animator = new SortAnimator(new InsertionSorter(),new SelectionSorter());

        }else if(buttonPressed.equals(insertionCheckBox)&&buttonPressed.equals(mergeCheckBox)){
                 // SortAnimator animator = new SortAnimator(new InsertionSorter(),new mergeSorter());

        }else if(buttonPressed.equals(quickCheckBox)&&buttonPressed.equals(bubbleCheckBox)){
            //

        }else if(buttonPressed.equals(quickCheckBox)&&buttonPressed.equals(mergeCheckBox)){

        }else if(buttonPressed.equals(quickCheckBox)&&buttonPressed.equals(selectionCheckBox)){

        }else if(buttonPressed.equals(selectionCheckBox)&&buttonPressed.equals(bubbleCheckBox)){
              SortAnimator animator = new SortAnimator(new SelectionSorter(),new BubbleSorter());

        }else if(buttonPressed.equals(selectionCheckBox)&&buttonPressed.equals(mergeCheckBox)){

        }else if (buttonPressed.equals(mergeCheckBox)&&buttonPressed.equals(bubbleCheckBox)){

        }else{
            //Invalid selection please select a maximum of two different algorithms
        }

Also you can't use getSource() to detect two different source of events at the same time like you did

if (buttonPressed.equals(insertionCheckBox)&&buttonPressed.equals(bubbleCheckBox))
getSource() returns the object on which the event initially occured.

To solve your problem you can use boolean values like isBubbleSelected, isMergeSelected, isInsertionSelected etc. to check the state of checkboxes ie if they are selected or not based on those boolean values you can decide which animations to play for ex: isBubbleSelected = bubleCheckBox.isSelected();

 if (isBubbleSelected&&isInsertionSelected) { SortAnimator animator = new SortAnimator(new InsertionSorter(),new BubbleSorter()); }else if(isInsertionSelected&&isQuickSelected){ // SortAnimator animator = new SortAnimator(new InsertionSorter(),new quickSorter()); }else if(isInsertionSelected&&isSelectionSelected){ SortAnimator animator = new SortAnimator(new InsertionSorter(),new SelectionSorter()); }else if(isInsertionSelected&&isMergeSelected){ // SortAnimator animator = new SortAnimator(new InsertionSorter(),new mergeSorter()); }else if(isQuickSelected&&isBubbleSelected){ // }else if(isQuickSelected&&isMergeSelected){ }else if(isQuickSelected&&isSelectionSelected){ }else if(isSelectionSelected&&isBubbleSelected){ SortAnimator animator = new SortAnimator(new SelectionSorter(),new BubbleSorter()); 

In the the ActionListener() class you could pass the JCheckBox through the constructor and then get selection when the action occurs.

ex...

public class JCheckBoxActionListener() implements ActionListener {
   JCheckBox jCheckBox;

   public JCheckBoxActionListener(JCheckBox jCheckBox) {
      this.jCheckBox = jCheckBox;
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      System.out.println(jCheckBox.isSelected());
   }
}

Add the listener... assuming 'comparison' is the JCheckBox

comparison.addActionListener(new JCheckBoxActionListener(comparison));

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