The Artima Developer Community
Sponsored Link

Java Answers Forum
cannot connect file dialog

1 reply on 1 page. Most recent reply: Feb 17, 2003 6:20 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
kanak

Posts: 14
Nickname: vava
Registered: Jan, 2003

cannot connect file dialog Posted: Feb 16, 2003 7:36 AM
Reply to this message Reply
Advertisement
how could i connect a file dialog to another programme in which if i click a menu item i must open a file dialog an d when i press open or save in it i should be able to save it.

could i get a programme for making a simple notepad.

Please help me


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: cannot connect file dialog Posted: Feb 17, 2003 6:20 PM
Reply to this message Reply

/* SimpleTextEditor.java
*/

import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;


/** SimpleTextEditor is a very basic text editor application which supports
* File Open, File Save, and the basic text editor functions for cut, copy
* and paste and interfaces with the system clipboard.
*/
public class SimpleTextEditor implements ActionListener{

JFrame frame;
JTextArea editor;
//clipboard
Clipboard clipboard;
// clipboardstring is the Transferrable object used to store
// String text data for clear, cut, copy, and paste operations
StringSelection clipboardstring;
boolean debug = false; //set to true for debugging messages
Font editorfont = new Font("Monospaced",Font.PLAIN,12);
int tabsize = 5;


/**
*/
public static void main(String[] args){

SimpleTextEditor simpletexteditor = new SimpleTextEditor();
simpletexteditor.init();

}

/** Initializes the button controls, a textarea for an editor to display
* or enter text, and adds the components to a frame window and displays
* it. A clipboard object is set up so that this application can interface
* with other applications for cut copy and paste operations provided this
* is allowed by the SecuityManager if active. If the security manager
* does not allow it, it creates a clipboard object which will only support
* cut, copy,and paste operations from this application.
*
*/
public void init(){
SecurityManager securitymanager = System.getSecurityManager();
if (securitymanager != null){
try{
securitymanager.checkSystemClipboardAccess();
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
if (debug) System.out.println("Using system clipboard");
}catch (SecurityException se){
showExceptionErrorMessage("SystemClipboardAccess is denied. Using application clipboard.");
clipboard = new Clipboard("SimpleTextEditor");
System.out.println("SystemClipboardAccess is denied.");
System.out.println("SecurityException: " + se.getMessage());
if (debug) System.out.println("Using application clipboard");

}
}else{
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
if (debug) System.out.println("Using system clipboard");
}

frame = new JFrame("SimpleTextEditor");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//button controls
JPanel controlpanel = new JPanel();
JButton openbutton = new JButton("Open A File");
JButton writebutton = new JButton("Save To File");
JButton clearbutton = new JButton("Clear");
JButton cutbutton = new JButton("Cut");
JButton copybutton = new JButton("Copy");
JButton pastebutton = new JButton("Paste");
JButton exitbutton = new JButton("Exit");
exitbutton.addActionListener(this);
openbutton.addActionListener(this);
writebutton.addActionListener(this);
clearbutton.addActionListener(this);
cutbutton.addActionListener(this);
copybutton.addActionListener(this);
pastebutton.addActionListener(this);
controlpanel.add(writebutton);
controlpanel.add(openbutton);
controlpanel.add(clearbutton);
controlpanel.add(cutbutton);
controlpanel.add(copybutton);
controlpanel.add(pastebutton);
controlpanel.add(exitbutton);
frame.getContentPane().add(controlpanel,"North");
//editor
editor = new JTextArea(new PlainDocument(),"",30,60); //initializes a JTextArea with width 60 and height 30
editor.setFont(editorfont);
editor.setTabSize(tabsize);
JScrollPane scrollpane = new JScrollPane(editor);
frame.getContentPane().add(scrollpane,"Center");
frame.pack();
frame.show();
}

/** Brings up a simple FileSave dialog for the user to enter a file name,
* and creates a file based on that name in the selected directory.
*/
public File getAFileForSave(){
File file = null;
File currentdirectory = new File(".");
JFileChooser filechooser = new JFileChooser(currentdirectory);
int replycode = filechooser.showSaveDialog(frame);
if (replycode == JFileChooser.APPROVE_OPTION){
file = filechooser.getSelectedFile();
}
return file;

}

/** Brings up as imple file open dialog and returns the file selected by the user.
*/
public File getAFileToOpen(){
File file = null;
File currentdirectory = new File(".");
JFileChooser filechooser = new JFileChooser(currentdirectory);
int replycode = filechooser.showOpenDialog(frame);
if (replycode == JFileChooser.APPROVE_OPTION){
file = filechooser.getSelectedFile();
}
return file;
}

/** Writes the string to the input file.
*/
public void writeStringToFile(File file, String s){

try{
FileWriter filewriter = new FileWriter(file);
StringReader stringreader = new StringReader(s);
BufferedReader bufferedreader = new BufferedReader(stringreader);
String lineread = "";
while ((lineread = bufferedreader.readLine()) != null){
filewriter.write(lineread + "\r\n");
}
filewriter.close();
}catch (FileNotFoundException fnfe){
System.err.println("FileNotFoundException: " + fnfe.getMessage());
showExceptionErrorMessage("FileNotFoundException: " + fnfe.getMessage());
}catch (IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
showExceptionErrorMessage("IOException: " + ioe.getMessage());
}


}

/** Appends the contents of the input file to the text editor.
*/
public void writeFileToEditor(File file){

try{
FileReader filereader = new FileReader(file);
BufferedReader bufferedreader = new BufferedReader(filereader);
String lineread = "";
while ((lineread = bufferedreader.readLine()) != null){
editor.append(lineread + "\n");
}
filereader.close();
}catch (FileNotFoundException fnfe){
System.err.println("FileNotFoundException: " + fnfe.getMessage());
showExceptionErrorMessage("FileNotFoundException: " + fnfe.getMessage());
}catch (IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
showExceptionErrorMessage("IOException: " + ioe.getMessage());
}

}

/** Provides actions to user input.
*/
public void actionPerformed(ActionEvent actionevent){
String actioncommand = actionevent.getActionCommand();
if (actioncommand.compareTo("Exit") == 0) {
System.exit(0);
}else if (actioncommand.compareTo("Clear") == 0) {
clear();
}else if (actioncommand.compareTo("Cut") == 0) {
cut();
}else if (actioncommand.compareTo("Copy") == 0) {
copy();
}else if (actioncommand.compareTo("Paste") == 0) {
paste();
}else if (actioncommand.compareTo("Open A File") == 0) {
File f = getAFileToOpen();
writeFileToEditor(f);
}else if (actioncommand.compareTo("Save To File") == 0) {
String s = editor.getText();
File f = getAFileForSave();
writeStringToFile(f,s);
showMessage("Contents of the Text Editor saved to file " + f.getName());
}
}

/** Conveniently displays a message to the user and waits for
* confirmation.
*/
public void showMessage(String s){
JOptionPane.showMessageDialog(frame,s);
}

/** Conveniently displays an exception error message to the user
* and waits for confirmation.
*/
public void showExceptionErrorMessage(String s){
JOptionPane.showMessageDialog(frame,s,s,JOptionPane.ERROR_MESSAGE);
}


/** clear() puts all editor text into the clipboardstring Transferrable
* object and into the clipboard in case the user wants to paste it
* back later in, then clears the editor. Text is also placed in the
* system clipboard so it can be pasted into another application.
*/
private void clear(){

String selectedtext = editor.getText();
if (selectedtext != null){
clipboardstring = new StringSelection(selectedtext);
clipboard.setContents(clipboardstring,clipboardstring);
}
editor.setText("");
}

/** cut deletes selected text and stores the text into the clipboardstring
* Transferrable object and into the clipboard.
*/
private void cut(){
String selectedtext = editor.getSelectedText();
if (selectedtext != null){
clipboardstring = new StringSelection(selectedtext);
clipboard.setContents(clipboardstring,clipboardstring);

int startmark = editor.getSelectionStart();
int endmark = editor.getSelectionEnd();
editor.replaceRange("",startmark,endmark);
}
}

/** copy() stores the selected text from the editor into the
* clipboardstring Transferrable object and into the clipboard.
*/
private void copy(){
String selectedtext = editor.getSelectedText();
if (selectedtext != null){
clipboardstring = new StringSelection(selectedtext);
clipboard.setContents(clipboardstring,clipboardstring);

}
}

/** paste() inserts the string stored from clear, copy or cut at
* the current caret position.
*/
private void paste(){
Transferable clipboarddata = clipboard.getContents(clipboardstring);
try{
String text = (String) clipboarddata .getTransferData(DataFlavor.stringFlavor);
editor.insert(text,editor.getCaretPosition());
}catch(Exception e){
System.err.println("Exception: " + e.getMessage());
showExceptionErrorMessage("Exception: " + e.getMessage());
}
}
}


Flat View: This topic has 1 reply on 1 page
Topic: NoClassDefFoundError Previous Topic   Next Topic Topic: Syncing Thinkdb with Access Using Java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use