Solved

Add a cancel button

Posted on 2003-11-22
8
1,001 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
ID: 9805213
where did u want to add it?
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 9805226
> 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
ID: 9807844
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 9807863
Use YES_NO_CANCEL_OPTION  in showOptionDialog().
0
 

Author Comment

by:CruZ3r
ID: 9822901
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
ID: 9822912
you're missing the voteButton type, should be:

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

929 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

8 Experts available now in Live!

Get 1:1 Help Now