Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WindowEvent question.

Status
Not open for further replies.

maccamlc

Technical User
Sep 6, 2002
1
AU
I am just starting with Java and User Interfaces at uni, and am doing a simple text editor. The below code, works, except with the windowClose window event, it references my exit event, but if you click cancel, the text editor disappears from view, but dos window remains open. However, if you do this through File Exit, after changing the text, it remains visible.

Just wondering if someone may know how I can edit this, to get the window Event to act the same as File/Exit.

(I know the coding is not neat, but we'll be shown a few new ways to do the action listeners this week.)

Thanks in advance!

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



/**
TextEditor creates a basic user interface to handle
text and buttons on a jframe.

version 1.0 19/8/2002
*/

public class TextEditor

{
/**
* Constructor for objects of class TextEditor
*/



public TextEditor()

{
TextEditorFrame frame = new TextEditorFrame();
frame.validate();
ShutDown shutdown = new ShutDown(frame);
frame.addWindowListener(shutdown);

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height){
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width){
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) /2 );

frame.setVisible(true);

}


class ShutDown extends WindowAdapter

{

TextEditorFrame frame = new TextEditorFrame();

public ShutDown (TextEditorFrame frame) {

this.frame = frame;

}

public void windowClosing(WindowEvent e)

{
frame.ExitActionPerformed(null);

}

}




/**
* The main method - to start the application
*
* @param args the command line parameters
* @return void
*/

public static void main(String [] args)

{
try
{
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}

catch (Exception e)

{
e.printStackTrace();
}
new TextEditor();
}

}
----------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.io.*;

/**
TextEditorFrame sets the dimensons, etc of the Frame everything
will be put on.
author
version 1.0 19/8/2002
*/


public class TextEditorFrame extends JFrame

{
JPanel contentPane = new JPanel();
String fileName;
BorderLayout borderLayout = new BorderLayout();
JToolBar toolBar = new JToolBar();
JButton button1 = new JButton();
JButton button2 = new JButton();
JButton button3 = new JButton();
ImageIcon image1; ImageIcon image2; ImageIcon image3;
JMenuBar jMenuBar = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuItemExit = new JMenuItem();
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuItemAbout = new JMenuItem();
JMenuItem jMenuItemOpen = new JMenuItem();
JMenuItem jMenuItemSave = new JMenuItem();
JMenu jMenuEdit = new JMenu();
JMenuItem jMenuItemCut = new JMenuItem();
JMenuItem jMenuItemPaste = new JMenuItem();
JMenuItem jMenuItemSelectAll = new JMenuItem();
JMenuItem jMenuItemCopy = new JMenuItem();
final JFileChooser chooser = new JFileChooser();
JLabel statusBar = new JLabel();
JTextArea jTextArea = new JTextArea("Some starting text");
JScrollPane textScroller;
String currentFile = "";
boolean change = false;
String[] Colours = { "Colour", "Black / White", "Blue / Red", "Green / Yellow"};
Color fore, back;



/**
* Constructor for objects of class TextEditorFrame
*/


public TextEditorFrame()

{
this.setSize(new Dimension(400,300));
this.setTitle("Text Editor");
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(borderLayout);


try {
image1 = new ImageIcon(TextEditorFrame.
class.getResource("Open24.gif"));
button1.setIcon(image1);
}

catch (Exception e) {

button1.setText("Open");
}

try {
image2 = new ImageIcon(TextEditorFrame.
class.getResource("Save24.gif"));
button2.setIcon(image2);
}

catch (Exception e) {

button2.setText("Save");
}

try {
image3 = new ImageIcon(TextEditorFrame.
class.getResource("Help24.gif"));
button3.setIcon(image3);
}

catch (Exception e) {

button3.setText("Help");
}

button1.setToolTipText("Open File");
button1.addActionListener(
new OpenActionAdapter(this));
toolBar.add(button1);

button2.setToolTipText("Save File");
button2.addActionListener(
new SaveActionAdapter(this));

toolBar.add(button2);

button3.setToolTipText("Help");
toolBar.add(button3);

// The file menu
jMenuFile.setText("File");
jMenuItemOpen.setText("Open");
jMenuItemSave.setText("Save As");
jMenuItemExit.setText("Exit");
jMenuItemExit.addActionListener(
new ExitActionAdapter(this));
jMenuItemOpen.addActionListener(
new OpenActionAdapter(this));
jMenuItemSave.addActionListener(
new SaveAsActionAdapter(this));
jMenuFile.add(jMenuItemOpen);
jMenuFile.add(jMenuItemSave);
jMenuFile.add(jMenuItemExit);
// The Help menu
jMenuHelp.setText("Help");
jMenuItemAbout.setText("About");
jMenuItemAbout.addActionListener(
new AboutActionAdapter(this));
jMenuHelp.add(jMenuItemAbout);

//Edit menu
jMenuEdit.setText("Edit");
jMenuItemCut.setText("Cut");
jMenuItemCut.addActionListener(
new CutActionAdapter(this));
jMenuEdit.add(jMenuItemCut);
jMenuItemCopy.setText("Copy");
jMenuItemCopy.addActionListener(
new CopyActionAdapter(this));
jMenuEdit.add(jMenuItemCopy);
jMenuItemPaste.setText("Paste");
jMenuItemPaste.addActionListener(
new PasteActionAdapter(this));
jMenuEdit.add(jMenuItemPaste);
jMenuItemSelectAll.setText("Select All");
jMenuItemSelectAll.addActionListener(
new SelectAllActionAdapter(this));
jMenuEdit.add(jMenuItemSelectAll);

// add menu object to menu bar
jMenuBar.add(jMenuFile);
jMenuBar.add(jMenuEdit);
jMenuBar.add(jMenuHelp);

// set the menu bar to jMenuBar
this.setJMenuBar(jMenuBar);

jTextArea.getDocument().addDocumentListener(new MyDocumentListener());
jTextArea.setColumns(80);
//jTextArea.setLineWrap(true);
textScroller = new JScrollPane(jTextArea);

JComboBox colorList = new JComboBox(Colours);
colorList.setSelectedIndex(0);

colorList.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
String colorName = (String)cb.getSelectedItem();

if (colorName == "Black / White") {
fore = Color.black;
back = Color.white;
}
if (colorName == "Blue / Red") {
fore = Color.blue;
back = Color.red;
}
if (colorName == "Green / Yellow") {
fore = Color.green;
back = Color.yellow;
}
jTextArea.setForeground(fore);
jTextArea.setBackground(back);

}
});

toolBar.add(colorList);


contentPane.add(toolBar, borderLayout.NORTH);
contentPane.add(statusBar, borderLayout.SOUTH);
contentPane.add(textScroller, borderLayout.CENTER);

}

public void ExitActionPerformed(ActionEvent e)
{

System.out.println("Exit");
System.out.println(change);
if (change == false) {
System.exit(0);
}
else {
int choice = JOptionPane.showConfirmDialog(contentPane, "Do you wish to save your changes, before quitting!",
"Save Changes!", JOptionPane.YES_NO_CANCEL_OPTION);
if (choice == JOptionPane.YES_OPTION) {
SaveActionPerformed(e);
System.exit(0);
}
if (choice == JOptionPane.NO_OPTION)
System.exit(0);

if (choice == JOptionPane.CANCEL_OPTION) {repaint();}
}

}

class ExitActionAdapter implements ActionListener
{
TextEditorFrame adaptee;

ExitActionAdapter(TextEditorFrame adaptee)
{
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e)
{
adaptee.ExitActionPerformed(e);
}
}

class AboutActionAdapter implements ActionListener
{
TextEditorFrame adaptee;

AboutActionAdapter(TextEditorFrame adaptee)
{
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e)
{
adaptee.AboutActionPerformed(e);
}
}

public void AboutActionPerformed(ActionEvent e)
{
AboutTextEditor dialog = new AboutTextEditor(this);
dialog.setLocationRelativeTo(this);
// more code to go here
dialog.setModal(true);
dialog.show();
repaint();
}

public void OpenActionPerformed(ActionEvent e)
{
int returnVal = chooser.showOpenDialog(getParent());
if (returnVal == JFileChooser.APPROVE_OPTION) {
fileName = chooser.getSelectedFile().getName();
try {
// Open a file of the given name.
File file = new File(fileName);
// Get the size of the opened file.
int size = (int)file.length();
// Set # character read to zero
int chars_read = 0;
// Create an input reader based from the file
FileReader in = new FileReader(file);
// Create a character array of the size of the file
char[] data = new char[size];

// Now read the characters into the array
while(in.ready()) {
chars_read += in.read(data, chars_read, size - chars_read);
}
in.close();
// Create a temporary string from the array
// and add to the JTextArea
// (Your variable name may be different)
jTextArea.setText(new String(data, 0, chars_read));
// Display the file name in the status bar.
statusBar.setText("Opened "+fileName);
currentFile = fileName;
}


catch (Exception e2) {


statusBar.setText("Error: The system has encountered a problem opening the file!");
}
System.out.println("Open File: " + fileName);
}
repaint();
}

class OpenActionAdapter implements ActionListener
{
TextEditorFrame adaptee1;

OpenActionAdapter(TextEditorFrame adaptee1)
{
this.adaptee1 = adaptee1;
}

public void actionPerformed(ActionEvent e)
{
adaptee1.OpenActionPerformed(e);
}
}

public void SaveAsActionPerformed(ActionEvent e)
{
int returnVal1 = chooser.showSaveDialog(getParent());
if (returnVal1 == JFileChooser.APPROVE_OPTION) {
currentFile = chooser.getSelectedFile().getName();
System.out.println("Save As File: " + currentFile);

try {
// Open a file of the current name.
File file = new File (currentFile);
// Create an output writer.
FileWriter out = new FileWriter(file);
String text = jTextArea.getText();
out.write(text);
out.close();
change = false;
statusBar.setText("Written to " +currentFile);
}



catch (Exception e2) {



statusBar.setText("Error: Unable to write " + currentFile);
}
}



repaint();
}

class SaveAsActionAdapter implements ActionListener
{
TextEditorFrame adaptee2;

SaveAsActionAdapter(TextEditorFrame adaptee2)
{
this.adaptee2 = adaptee2;
}

public void actionPerformed(ActionEvent e)
{
adaptee2.SaveAsActionPerformed(e);
}
}

public void SaveActionPerformed(ActionEvent e)

{
if (currentFile.equals(""))
SaveAsActionPerformed(e);
else {

try {
// Open a file of the current name.
File file = new File (currentFile);
// Create an output writer.
FileWriter out = new FileWriter(file);
String text = jTextArea.getText();
out.write(text);
out.close();
change = false;
statusBar.setText("Written to " +currentFile);
}



catch (Exception e2) {



statusBar.setText("Error: Unable to write " + currentFile);
}
System.out.println("Save File: " + currentFile);
}
}




class SaveActionAdapter implements ActionListener

{
TextEditorFrame adaptee1;


SaveActionAdapter(TextEditorFrame adaptee1)

{
this.adaptee1 = adaptee1;
}


public void actionPerformed(ActionEvent e)

{
adaptee1.SaveActionPerformed(e);
}
}

class MyDocumentListener implements DocumentListener {


public void insertUpdate(DocumentEvent e) {
changed(e);
}
public void removeUpdate(DocumentEvent e) {
changed(e);
}

public void changedUpdate(DocumentEvent e) {
}

public void changed(DocumentEvent e) {
change = true;
}
}

class CutActionAdapter implements ActionListener
{
TextEditorFrame adaptee;

CutActionAdapter(TextEditorFrame adaptee)
{
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e)
{
adaptee.CutActionPerformed(e);
}
}

public void CutActionPerformed(ActionEvent e)
{
jTextArea.getSelectionStart();
jTextArea.getSelectionEnd();
jTextArea.cut();
repaint();

}

class CopyActionAdapter implements ActionListener
{
TextEditorFrame adaptee;

CopyActionAdapter(TextEditorFrame adaptee)
{
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e)
{
adaptee.CopyActionPerformed(e);
}
}

public void CopyActionPerformed(ActionEvent e)
{
jTextArea.copy();
repaint();
}

class PasteActionAdapter implements ActionListener
{
TextEditorFrame adaptee;

PasteActionAdapter(TextEditorFrame adaptee)
{
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e)
{
adaptee.PasteActionPerformed(e);
}
}

public void PasteActionPerformed(ActionEvent e)
{
jTextArea.paste();
repaint();
}

class SelectAllActionAdapter implements ActionListener
{
TextEditorFrame adaptee;

SelectAllActionAdapter(TextEditorFrame adaptee)
{
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e)
{
adaptee.SelectAllActionPerformed(e);
}
}

public void SelectAllActionPerformed(ActionEvent e)
{
jTextArea.selectAll();
repaint();
}

public boolean getChange() {
return change;
}

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

public class AboutTextEditor extends JDialog implements ActionListener
{

String programName = "TextEditor";
String comments = "A simple text editor using Swing";
String version = "1.0";
String copyright = "Copyright (c) 2002";
String author = "maccamlc";

JButton buttonOK = new JButton();
JPanel contentPane;
JPanel buttonPane = new JPanel();
BorderLayout borderLayout = new BorderLayout();
FlowLayout buttonLayout = new FlowLayout();
ImageIcon aboutIcon;
JLabel graphicLabel = new JLabel();
JPanel infoPane = new JPanel();
GridLayout infoLayout = new GridLayout(0,1);


AboutTextEditor(Frame parent)
{
super(parent);

setTitle("about editor");
setResizable(false);

contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(borderLayout);
buttonPane.setLayout(buttonLayout);

try {
aboutIcon = new ImageIcon(TextEditorFrame.
class.getResource("about.gif"));
graphicLabel.setIcon(aboutIcon);
}


catch (Exception e) {


graphicLabel.setText("Text Editor");
}

graphicLabel.setBorder(
BorderFactory.createEmptyBorder(10,10,10,10));
buttonOK.setText("OK");
buttonOK.addActionListener(this);

buttonPane.add(buttonOK);

infoPane.setLayout(infoLayout);
infoPane.add(new JLabel(programName));
infoPane.add(new JLabel(comments));
infoPane.add(new JLabel("Version: "+version));
infoPane.add(new JLabel("Author: "+author));
infoPane.add(new JLabel(copyright));
infoPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

contentPane.add(buttonPane, borderLayout.SOUTH);
contentPane.add(graphicLabel, borderLayout.WEST);
contentPane.add(infoPane, borderLayout.CENTER);

pack();
}

public void actionPerformed(ActionEvent e)
{
dispose();
}


}
 
To clarify - when you click the "x" on the gui, the programme exits, and you are left with the dos prompt. And you want this same action to happen when the user selects File/Exit ?

Ben

PS - small point - in your imports, you import javax.swing.UIManager, but then import it again with javax.swing.*;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top