Credit Cards | Myspace Comments | Bollywood | Mortgages | Adverse Credit Remortgage
problem with java [Archive] - PCMech Forums

PDA

View Full Version : problem with java


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

}

Force Flow
05-05-2004, 06:56 PM
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");
}
}





}

aym
05-05-2004, 07:12 PM
Haven't tried you code (don't have the JDK around right now) but why not use Swing's JOptionPane?

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/api/javax/swing/JOptionPane.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/api/java/awt/event/WindowListener.html

Force Flow
05-05-2004, 11:13 PM
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 ;)

aym
05-06-2004, 11:55 AM
Where did you place the JOptionPane code? It should be in the windowClosing method, if you still can't fix it, post code.

Force Flow
05-06-2004, 08:18 PM
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);
}

}

aym
05-07-2004, 10:24 AM
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

Force Flow
05-07-2004, 12:01 PM
I didn't know that.

Thanks a bunch, aym_7. The JOptionPane looks like it will come in handy ;)

aym
05-07-2004, 12:22 PM
No problem :) and yes, JOptionPane is quite useful for several kinds of dialogs.