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("Feedba ck 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(BorderFact ory.create EmptyBorde r(70,70,70 ,70));
choicePanel.setBorder(Bord erFactory. createEmpt yBorder(18 0,200,80,2 00));
// 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></ht ml>");
radioButtons[0].setActionC ommand(yes NoCommand) ;
radioButtons [1]= new JRadioButton("<html><font color=blue>Average</font>< /html>");
radioButtons[1].setActionC ommand(yes NoCommand) ;
radioButtons [2]= new JRadioButton("<html><font color=blue>Bad</font></htm l>");
radioButtons[2].setActionC ommand(yes NoCommand) ;
for (int i = 0; i < numButtons; i++) {
group.add(radioButtons[i]) ;
}
//select 1st button as default
radioButtons[0].setSelecte d(true);
voteButton = new JButton("Submit");
voteButton.addActionListen er(new ActionListener() {
public void actionPerformed(ActionEven t e) {
String command = group.getSelection().getAc tionComman d();
//yesnocommand
if (command == yesNoCommand) {
int n = JOptionPane.showConfirmDia log(
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("callin g createPane");
return createPane(simpleDialogDes c + ":",
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("return ing 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.setDefaultCloseOpera tion(JFram e.EXIT_ON_ CLOSE);
frame.pack();
frame.setVisible(true);
}
}
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("Feedba
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(BorderFact
choicePanel.setBorder(Bord
// 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></ht
radioButtons[0].setActionC
radioButtons [1]= new JRadioButton("<html><font color=blue>Average</font><
radioButtons[1].setActionC
radioButtons [2]= new JRadioButton("<html><font color=blue>Bad</font></htm
radioButtons[2].setActionC
for (int i = 0; i < numButtons; i++) {
group.add(radioButtons[i])
}
//select 1st button as default
radioButtons[0].setSelecte
voteButton = new JButton("Submit");
voteButton.addActionListen
public void actionPerformed(ActionEven
String command = group.getSelection().getAc
//yesnocommand
if (command == yesNoCommand) {
int n = JOptionPane.showConfirmDia
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("callin
return createPane(simpleDialogDes
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("return
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.setDefaultCloseOpera
frame.pack();
frame.setVisible(true);
}
}
where did u want to add it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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().
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></ht ml>");
radioButtons[0].setActionC ommand(yes NoCommand) ;
radioButtons [1]= new JRadioButton("<html><font color=blue>Average</font>< /html>");
radioButtons[1].setActionC ommand(yes NoCommand) ;
radioButtons [2]= new JRadioButton("<html><font color=blue>Bad</font></htm l>");
radioButtons[2].setActionC ommand(yes NoCommand) ;
for (int i = 0; i < numButtons; i++) {
group.add(radioButtons[i]) ;
}
//select 1st button as default
radioButtons[0].setSelecte d(true);
JButton voteButton = new JButton("Submit");
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionList ener(new ActionListener()
{
public void actionPerformed(ActionEven t e) {
System.exit(0);
return;
}
});
voteButton.addActionListen er(new ActionListener() {
public void actionPerformed(ActionEven t e) {
String command = group.getSelection().getAc tionComman d();
//yesnocommand
if (command == yesNoCommand) {
int n = JOptionPane.showConfirmDia log(
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("callin g createPane");
return createPane(simpleDialogDes c + ":",
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("return ing pane");
return pane;
}
JButton cancelButton,voteButton) {
^
C:\ck\feedback\FeedBack.ja va:149: ')' expected
}
^
2 errors
there's 2 errors. i dunno whatz wrong with it.
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></ht
radioButtons[0].setActionC
radioButtons [1]= new JRadioButton("<html><font color=blue>Average</font><
radioButtons[1].setActionC
radioButtons [2]= new JRadioButton("<html><font color=blue>Bad</font></htm
radioButtons[2].setActionC
for (int i = 0; i < numButtons; i++) {
group.add(radioButtons[i])
}
//select 1st button as default
radioButtons[0].setSelecte
JButton voteButton = new JButton("Submit");
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionList
{
public void actionPerformed(ActionEven
System.exit(0);
return;
}
});
voteButton.addActionListen
public void actionPerformed(ActionEven
String command = group.getSelection().getAc
//yesnocommand
if (command == yesNoCommand) {
int n = JOptionPane.showConfirmDia
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("callin
return createPane(simpleDialogDes
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("return
return pane;
}
JButton cancelButton,voteButton) {
^
C:\ck\feedback\FeedBack.ja
}
^
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) {
private JPanel createPane(String description,
JRadioButton[] radioButtons,
JButton cancelButton, JButton voteButton) {