Link to home
Start Free TrialLog in
Avatar of CruZ3r
CruZ3r

asked on

Add a cancel button

I wan to add a "cancel" button in my gui application but i dunno how to add it in my codes. can u experts help me out in this?

my codes below:



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;


public class FeedBack extends JPanel {

      JLabel label;
    JFrame frame;
    String simpleDialogDesc = "How do u feel about our services?";

    public FeedBack(JFrame frame) {
        this.frame = frame;
        JLabel title;
        JLabel qn1;

        // Create the components.
        JPanel choicePanel = createSimpleDialogBox();

        System.out.println("Feedback swing");

        title = new JLabel("Please fill this Feed Back form in order to allow us to provide a better service for you",
                                  JLabel.CENTER);

   label = new JLabel("Click the above button to submit", JLabel.CENTER);
      label.setBorder(BorderFactory.createEmptyBorder(70,70,70,70));
        choicePanel.setBorder(BorderFactory.createEmptyBorder(180,200,80,200));

        // Set the layout.
        setLayout(new BorderLayout());
        add(title, BorderLayout.NORTH);
        add(label, BorderLayout.SOUTH);
        add(choicePanel, BorderLayout.CENTER);
    }

    void setLabel(String newText)
    {
        label.setText(newText);
    }
private JPanel createSimpleDialogBox()

{

final int numButtons = 3;
        JRadioButton[] radioButtons = new JRadioButton[numButtons];

        final ButtonGroup group = new ButtonGroup();

        JButton voteButton = null;

        final String yesNoCommand = "yesno";

        radioButtons [0]= new JRadioButton("<html><font color=blue>Good</font></html>");
             radioButtons[0].setActionCommand(yesNoCommand);

             radioButtons [1]= new JRadioButton("<html><font color=blue>Average</font></html>");
             radioButtons[1].setActionCommand(yesNoCommand);

             radioButtons [2]= new JRadioButton("<html><font color=blue>Bad</font></html>");
             radioButtons[2].setActionCommand(yesNoCommand);


             for (int i = 0; i < numButtons; i++) {
            group.add(radioButtons[i]);
        }

        //select 1st button as default
         radioButtons[0].setSelected(true);

        voteButton = new JButton("Submit");

        voteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String command = group.getSelection().getActionCommand();

              //yesnocommand
             if (command == yesNoCommand) {
                    int n = JOptionPane.showConfirmDialog(
                            frame, "Are you confirm with this decision?",
                            "A Follow-up Question",
                            JOptionPane.YES_NO_OPTION);

                   if (n == JOptionPane.YES_OPTION) {
                        setLabel("Thanks for your Feedback.We will try our best to improve our services");
                    } else if (n == JOptionPane.NO_OPTION) {
                        setLabel("OK please try again");
                    }

                  }
               return;
            }
        });
      System.out.println("calling createPane");
        return createPane(simpleDialogDesc + ":",
                          radioButtons,
                          voteButton);
    }

     private JPanel createPane(String description,
                              JRadioButton[] radioButtons,
                              JButton showButton) {
        int numChoices = radioButtons.length;
        JPanel box = new JPanel();
        JLabel label = new JLabel(description);

        box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
        box.add(label);

        for (int i = 0; i < numChoices; i++) {
            box.add(radioButtons[i]);
        }

          JPanel pane = new JPanel();
        pane.setLayout(new BorderLayout());
        pane.add(box, BorderLayout.NORTH);
        pane.add(showButton, BorderLayout.SOUTH);
        System.out.println("returning pane");
        return pane;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("FeedBack");

        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridLayout(1,1));
        contentPane.add(new FeedBack(frame));

        // Exit when the window is closed.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.pack();
        frame.setVisible(true);
    }
}
Avatar of Mick Barry
Mick Barry
Flag of Australia image

where did u want to add it?
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CruZ3r
CruZ3r

ASKER

I wan to add it in the "yes or no" command there. It's a pop up window that prompt users whether they are sure with their answers anot.

the window will show "ARe you confirm with this decision?" and there's a yes/no button to click on. but i dunno how to add in the cancel button in there.
Use YES_NO_CANCEL_OPTION  in showOptionDialog().
Avatar of CruZ3r

ASKER

i tried adding the cancel button at the submit button there. but there's an error.


private JPanel createSimpleDialogBox()

{

final int numButtons = 3;
        JRadioButton[] radioButtons = new JRadioButton[numButtons];
       
        final ButtonGroup group = new ButtonGroup();

   

        final String yesNoCommand = "yesno";
       
        radioButtons [0]= new JRadioButton("<html><font color=blue>Good</font></html>");
             radioButtons[0].setActionCommand(yesNoCommand);
             
             radioButtons [1]= new JRadioButton("<html><font color=blue>Average</font></html>");
             radioButtons[1].setActionCommand(yesNoCommand);
             
             radioButtons [2]= new JRadioButton("<html><font color=blue>Bad</font></html>");
             radioButtons[2].setActionCommand(yesNoCommand);
             
             
             for (int i = 0; i < numButtons; i++) {
            group.add(radioButtons[i]);
        }
       
        //select 1st button as default
         radioButtons[0].setSelected(true);

       JButton voteButton = new JButton("Submit");
        JButton cancelButton = new JButton("Cancel");
       
        cancelButton.addActionListener(new ActionListener()
        {
              public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                    return;
              }
              
    });
       
        voteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String command = group.getSelection().getActionCommand();
               
              //yesnocommand  
             if (command == yesNoCommand) {
                    int n = JOptionPane.showConfirmDialog(
                            frame, "Are you confirm with this decision?",
                            "A Follow-up Question",
                            JOptionPane.YES_NO_OPTION);
                           
                   if (n == JOptionPane.YES_OPTION) {
                        setLabel("Thanks for your Feedback.We will try our best to improve our services");
                    } else if (n == JOptionPane.NO_OPTION) {
                        setLabel("OK please try again");
                    }
                        
                  }
               return;
            }
        });


       
      System.out.println("calling createPane");
        return createPane(simpleDialogDesc + ":",
                          radioButtons,
                          cancelButton,voteButton);                    
    }
   
    private JPanel createPane(String description,
                              JRadioButton[] radioButtons,
                              JButton cancelButton,voteButton) {
        int numChoices = radioButtons.length;
        JPanel box = new JPanel();
        JLabel label = new JLabel(description);

        box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
        box.add(label);

        for (int i = 0; i < numChoices; i++) {
            box.add(radioButtons[i]);
        }

        JPanel pane = new JPanel();
        pane.setLayout(new BorderLayout());
        pane.add(box, BorderLayout.NORTH);
        pane.add(cancelButton, BorderLayout.SOUTH);
        pane.add(voteButton, BorderLayout.SOUTH);
        System.out.println("returning pane");
        return pane;
    }







                              JButton cancelButton,voteButton) {
                                                             ^
C:\ck\feedback\FeedBack.java:149: ')' expected
}
^
2 errors


there's 2 errors. i dunno whatz wrong with it.
you're missing the voteButton type, should be:

private JPanel createPane(String description,
                              JRadioButton[] radioButtons,
                              JButton cancelButton, JButton voteButton) {