Solved

Add a cancel button

Posted on 2003-11-22
8
1,002 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

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 opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

809 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