Solved

Add a cancel button

Posted on 2003-11-22
8
1,000 Views
Last Modified: 2009-03-04
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);
    }
}
0
Comment
Question by:CruZ3r
  • 4
  • 2
8 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
where did u want to add it?
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
Comment Utility
> pane.add(showButton, BorderLayout.SOUTH);

Instead of adding your submit button to pane, create a new panel (with FlowLayout) and add your submit and new cancel button to it. Then add that new panel to pane.

JPanel buttons = new JPanel(new FlowLayout());
buttons.add(showButton);
buttons.add(cancelButton);
pane.add(buttons, BorderLayout.SOUTH);
0
 

Author Comment

by:CruZ3r
Comment Utility
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.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 92

Expert Comment

by:objects
Comment Utility
Use YES_NO_CANCEL_OPTION  in showOptionDialog().
0
 

Author Comment

by:CruZ3r
Comment Utility
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.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
you're missing the voteButton type, should be:

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now