Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 05-05-2004, 05:55 PM   #1
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
Question problem with java

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);
}

}
__________________
There are two secrets to staying young, being happy, and achieving success. You have to laugh and find humor every day, and you have to have a dream.
Force Flow is offline   Reply With Quote
Old 05-05-2004, 05:56 PM   #2
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
And here's the confirmation window class:


/*
*Compiler: NetBeans 3.5.1
*Description: This class will create a confirmation window upon "X"ing out of the program window
*
*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//confirmation window GUI
public class GHP7_confirm extends JFrame implements ActionListener {
public GHP7_confirm() {

setSize(200, 100);

//main panel
Container pane = getContentPane();
pane.setLayout(new BorderLayout());

//question label
JLabel confirmL = new JLabel("Are you sure you want to exit?");
pane.add(confirmL, BorderLayout.CENTER);

//buttonpanel
JPanel buttonpanel = new JPanel();
buttonpanel.setLayout(new FlowLayout());

//yes button
JButton yesB = new JButton("Yes");
yesB.addActionListener(this);
JPanel yesP = new JPanel();
yesP.setLayout(new FlowLayout());
yesP.add(yesB);

//no button
JButton noB = new JButton("No");
noB.addActionListener(this);
JPanel noP = new JPanel();
noP.setLayout(new FlowLayout());
noP.add(noB);

//add buttons to button panel
buttonpanel.add(yesP);
buttonpanel.add(noP);

pane.add(buttonpanel, BorderLayout.SOUTH);

}


public void actionPerformed(ActionEvent e) {

String AC = e.getActionCommand();

if (AC.equals("Yes"))
System.exit(0);
else if (AC.equals("No"))
dispose();
else {
dispose();
System.out.println("Error in confirmation window");
}
}





}
Force Flow is offline   Reply With Quote
Old 05-05-2004, 06:12 PM   #3
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
Haven't tried you code (don't have the JDK around right now) but why not use Swing's JOptionPane?
Code:
int r = JOptionPane.showConfirmDialog(
    null,   // dialog parent
    "Are you sure?", // body
    "Are you sure?",  // title
    JOptionPane.YES_NO_OPTION // buttons
);
if (r == JOptionPane.YES_OPTION) {
    // user clicked yes
    System.exit(0);
} else {
    // user clicked no
}
More info about JOptionPane:
http://java.sun.com/j2se/1.4.2/docs/...ptionPane.html

To handle window events, you need a WindowListener, not an ActionListener, and override the windowClosing method with that piece of code, more info:
http://java.sun.com/j2se/1.4.2/docs/...wListener.html

Last edited by aym; 05-05-2004 at 06:15 PM.
aym is offline   Reply With Quote
Old 05-05-2004, 10:13 PM   #4
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
Ok, it sort of works, but the JOptionPane is the first thing I see when I run the application. How would I be able to set it to run only with:

setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

Or can't I?

[edit] btw, I've only been working with java GUI for about a week, so I'm not aware of many features yet. Thanks for pointing this one out
Force Flow is offline   Reply With Quote
Old 05-06-2004, 10:55 AM   #5
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
Where did you place the JOptionPane code? It should be in the windowClosing method, if you still can't fix it, post code.
aym is offline   Reply With Quote
Old 05-06-2004, 07:18 PM   #6
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
I just put it in the wrong place. Ok, so now it's in windowClosing, but now when I go to "X" out the window, nothing happens.

/*
*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) {

int r = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Memo Saver", JOptionPane.YES_NO_OPTION);
if (r == JOptionPane.YES_OPTION)
System.exit(0);
else
dispose();
}
}


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);
}

}
Force Flow is offline   Reply With Quote
Old 05-07-2004, 09:24 AM   #7
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
It's windowClosing and not WindowClosing, it doesn't work because you didn't override the correct function.

First character in method names is always lower case BTW.

HTH
aym is offline   Reply With Quote
Old 05-07-2004, 11:01 AM   #8
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
I didn't know that.

Thanks a bunch, aym_7. The JOptionPane looks like it will come in handy
Force Flow is offline   Reply With Quote
Old 05-07-2004, 11:22 AM   #9
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
No problem and yes, JOptionPane is quite useful for several kinds of dialogs.

Last edited by aym; 05-07-2004 at 11:25 AM.
aym is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 12:54 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2