Solved

Add a cancel button

Posted on 2003-11-22
8
1,003 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
javap not working 8 60
ejb example issues 3 28
What browser will run Java? 7 68
American Express @Work site and Java 4 32
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

830 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