Force Flow
05-05-2004, 06:55 PM
I've written a little app that will store strings and can be accessed again (until the window is closed). The problem is that I am trying to write in a confirmation window (when the "x" in the upper right is clicked), to display a new window saying "Are you sure you want to exit?" with yes and no buttons.
The two classes compile correctly, but when the "x" is clicked, nothing happens. Any idea what's wrong?
Here's the code for the main class:
/*
*Compiler: NetBeans 3.5.1
*Description: This program will save 2 seperate memos and allow them to be accessed as long as
* the window is kept open.
*
*/
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class GHP7 extends JFrame implements ActionListener {
public static final int w = 600; //java window width
public static final int h = 325; //java window height
public static final int wTA = 10; //text area width
public static final int hTA = 40; //text area height
private JTextArea memoTA;
private String memo1 = "There is currently nothing saved for Memo 1.";
private String memo2 = "There is currently nothing saved for Memo 2.";
public GHP7(int width, int height) {
int wW = w; //set window width to static final width
int hW = h; //set window width to static final width
//if text area is not default this resizes applet window accordingly
if ((width != wTA) || (height != hTA)) {
//determine window width
if (width > 10)
hW = ( ( (width - 10) / 5) * 80) + hW;
//determine window height
if (height > 40)
wW = ( ( (height - 40) / 5) * 50) + wW;
}
setSize(wW, hW);
//confirmation window
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new confirmationwindow());
Container pane = getContentPane();
pane.setLayout(new FlowLayout());
//main panel
JPanel mainP = new JPanel();
mainP.setLayout(new BorderLayout());
//text panel
JPanel textpanel = new JPanel();
//text area size
if ((width != wTA) || (height != hTA))
memoTA = new JTextArea(width, height);
else
memoTA = new JTextArea(wTA, hTA);
textpanel.add(memoTA);
memoTA.setWrapStyleWord(true); //word wrap
memoTA.setLineWrap(true); //character wrap
memoTA.setBorder(new EtchedBorder(Color.WHITE, Color.DARK_GRAY)); //border for TA
mainP.add(textpanel, BorderLayout.CENTER);
//button panel
JPanel buttonpanel = new JPanel();
buttonpanel.setLayout(new GridLayout(2,3));
//titled border for button panel
Border buttonborder = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Navigation Panel" );
buttonpanel.setBorder(buttonborder);
//save button - memo1
JButton memo1B = new JButton("Save Memo 1");
memo1B.addActionListener(this);
JPanel memo1P = new JPanel();
memo1P.setLayout(new FlowLayout());
memo1P.add(memo1B);
//save button - memo2
JButton memo2B = new JButton("Save Memo 2");
memo2B.addActionListener(this);
JPanel memo2P = new JPanel();
memo2P.setLayout(new FlowLayout());
memo2P.add(memo2B);
//open button - memo1
JButton openmemo1B = new JButton("Open Memo 1");
openmemo1B.addActionListener(this);
JPanel openmemo1P = new JPanel();
openmemo1P.setLayout(new FlowLayout());
openmemo1P.add(openmemo1B);
//open button - memo2
JButton openmemo2B = new JButton("Open Memo 2");
openmemo2B.addActionListener(this);
JPanel openmemo2P = new JPanel();
openmemo2P.setLayout(new FlowLayout());
openmemo2P.add(openmemo2B);
//clear button
JButton clearB = new JButton("Clear");
clearB.addActionListener(this);
JPanel clearP = new JPanel();
clearP.setLayout(new FlowLayout());
clearP.add(clearB);
//exit button
JButton exitB = new JButton("Exit");
exitB.addActionListener(this);
JPanel exitP = new JPanel();
exitP.setLayout(new FlowLayout());
exitP.add(exitB);
//add buttons to button panel
buttonpanel.add(memo1P);
buttonpanel.add(openmemo1P);
buttonpanel.add(clearP);
buttonpanel.add(memo2P);
buttonpanel.add(openmemo2P);
buttonpanel.add(exitP);
//resize button panel
JPanel buttonpanelsize = new JPanel();
buttonpanelsize.setLayout(new FlowLayout());
buttonpanelsize.add(buttonpanel);
//add buttonpanel to main panel
mainP.add(buttonpanelsize, BorderLayout.SOUTH);
//add main panel to main container
pane.add(mainP);
}
public void actionPerformed(ActionEvent e) {
String buttonpressed = e.getActionCommand();
if (buttonpressed.equals("Save Memo 1")) {
memo1 = memoTA.getText();
memoTA.setText("Memo 1 has been saved.");
}
else if (buttonpressed.equals("Save Memo 2")) {
memo2 = memoTA.getText();
memoTA.setText("Memo 2 has been saved.");
}
else if (buttonpressed.equals("Open Memo 1"))
memoTA.setText(memo1);
else if (buttonpressed.equals("Open Memo 2"))
memoTA.setText(memo2);
else if (buttonpressed.equals("Clear"))
memoTA.setText(" ");
else if (buttonpressed.equals("Exit"))
//System.exit(0);
dispose();
else
memoTA.setText("Error in memo interface. We apologize for the inconvenience.");
}
//confirmation window call
private class confirmationwindow extends WindowAdapter {
public void WindowClosing(WindowEvent e) {
GHP7_confirm askwindow = new GHP7_confirm();
askwindow.setVisible(true);
}
}
public static void main(String [] args) {
GHP7 guimemo = new GHP7(wTA, hTA);
guimemo.setTitle("Memo Saver - Default Window Size");
guimemo.setVisible(true);
GHP7 guimemo2 = new GHP7(5, 60);
guimemo2.setTitle("Memo Saver - Resized Window");
guimemo2.setVisible(true);
}
}
The two classes compile correctly, but when the "x" is clicked, nothing happens. Any idea what's wrong?
Here's the code for the main class:
/*
*Compiler: NetBeans 3.5.1
*Description: This program will save 2 seperate memos and allow them to be accessed as long as
* the window is kept open.
*
*/
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class GHP7 extends JFrame implements ActionListener {
public static final int w = 600; //java window width
public static final int h = 325; //java window height
public static final int wTA = 10; //text area width
public static final int hTA = 40; //text area height
private JTextArea memoTA;
private String memo1 = "There is currently nothing saved for Memo 1.";
private String memo2 = "There is currently nothing saved for Memo 2.";
public GHP7(int width, int height) {
int wW = w; //set window width to static final width
int hW = h; //set window width to static final width
//if text area is not default this resizes applet window accordingly
if ((width != wTA) || (height != hTA)) {
//determine window width
if (width > 10)
hW = ( ( (width - 10) / 5) * 80) + hW;
//determine window height
if (height > 40)
wW = ( ( (height - 40) / 5) * 50) + wW;
}
setSize(wW, hW);
//confirmation window
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new confirmationwindow());
Container pane = getContentPane();
pane.setLayout(new FlowLayout());
//main panel
JPanel mainP = new JPanel();
mainP.setLayout(new BorderLayout());
//text panel
JPanel textpanel = new JPanel();
//text area size
if ((width != wTA) || (height != hTA))
memoTA = new JTextArea(width, height);
else
memoTA = new JTextArea(wTA, hTA);
textpanel.add(memoTA);
memoTA.setWrapStyleWord(true); //word wrap
memoTA.setLineWrap(true); //character wrap
memoTA.setBorder(new EtchedBorder(Color.WHITE, Color.DARK_GRAY)); //border for TA
mainP.add(textpanel, BorderLayout.CENTER);
//button panel
JPanel buttonpanel = new JPanel();
buttonpanel.setLayout(new GridLayout(2,3));
//titled border for button panel
Border buttonborder = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Navigation Panel" );
buttonpanel.setBorder(buttonborder);
//save button - memo1
JButton memo1B = new JButton("Save Memo 1");
memo1B.addActionListener(this);
JPanel memo1P = new JPanel();
memo1P.setLayout(new FlowLayout());
memo1P.add(memo1B);
//save button - memo2
JButton memo2B = new JButton("Save Memo 2");
memo2B.addActionListener(this);
JPanel memo2P = new JPanel();
memo2P.setLayout(new FlowLayout());
memo2P.add(memo2B);
//open button - memo1
JButton openmemo1B = new JButton("Open Memo 1");
openmemo1B.addActionListener(this);
JPanel openmemo1P = new JPanel();
openmemo1P.setLayout(new FlowLayout());
openmemo1P.add(openmemo1B);
//open button - memo2
JButton openmemo2B = new JButton("Open Memo 2");
openmemo2B.addActionListener(this);
JPanel openmemo2P = new JPanel();
openmemo2P.setLayout(new FlowLayout());
openmemo2P.add(openmemo2B);
//clear button
JButton clearB = new JButton("Clear");
clearB.addActionListener(this);
JPanel clearP = new JPanel();
clearP.setLayout(new FlowLayout());
clearP.add(clearB);
//exit button
JButton exitB = new JButton("Exit");
exitB.addActionListener(this);
JPanel exitP = new JPanel();
exitP.setLayout(new FlowLayout());
exitP.add(exitB);
//add buttons to button panel
buttonpanel.add(memo1P);
buttonpanel.add(openmemo1P);
buttonpanel.add(clearP);
buttonpanel.add(memo2P);
buttonpanel.add(openmemo2P);
buttonpanel.add(exitP);
//resize button panel
JPanel buttonpanelsize = new JPanel();
buttonpanelsize.setLayout(new FlowLayout());
buttonpanelsize.add(buttonpanel);
//add buttonpanel to main panel
mainP.add(buttonpanelsize, BorderLayout.SOUTH);
//add main panel to main container
pane.add(mainP);
}
public void actionPerformed(ActionEvent e) {
String buttonpressed = e.getActionCommand();
if (buttonpressed.equals("Save Memo 1")) {
memo1 = memoTA.getText();
memoTA.setText("Memo 1 has been saved.");
}
else if (buttonpressed.equals("Save Memo 2")) {
memo2 = memoTA.getText();
memoTA.setText("Memo 2 has been saved.");
}
else if (buttonpressed.equals("Open Memo 1"))
memoTA.setText(memo1);
else if (buttonpressed.equals("Open Memo 2"))
memoTA.setText(memo2);
else if (buttonpressed.equals("Clear"))
memoTA.setText(" ");
else if (buttonpressed.equals("Exit"))
//System.exit(0);
dispose();
else
memoTA.setText("Error in memo interface. We apologize for the inconvenience.");
}
//confirmation window call
private class confirmationwindow extends WindowAdapter {
public void WindowClosing(WindowEvent e) {
GHP7_confirm askwindow = new GHP7_confirm();
askwindow.setVisible(true);
}
}
public static void main(String [] args) {
GHP7 guimemo = new GHP7(wTA, hTA);
guimemo.setTitle("Memo Saver - Default Window Size");
guimemo.setVisible(true);
GHP7 guimemo2 = new GHP7(5, 60);
guimemo2.setTitle("Memo Saver - Resized Window");
guimemo2.setVisible(true);
}
}