473,382 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Adding input?

Hey Everyone!

After using Devshed (possibly the worst forum I have ever encountered) I have come here with my questions and answers about Java, Python, VB, Basic C++, HTML, Game Maker, and Uni-Gasp. So, greetings!

On to the question: I have been working on a Text editor a little bit, and hit a dead halt. I can not figure out how to import text into my program. I have what I thought would work in my code, but it just doesn't seem to work! Every time I try to compile, it gives an error: "file is not public in java.awt.filedialog; cannot be accessed from outside package". Here is the code:
Expand|Select|Wrap|Line Numbers
  1. package connect4;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5. import javax.imageio.*;
  6. import java.io.File;
  7. import javax.swing.Popup;
  8. import java.io.*;
  9. import java.awt.FileDialog.*;
  10. import java.io.FileWriter;
  11. import java.awt.image.*;
  12. import java.awt.BorderLayout;
  13. public class TextEdit extends JFrame implements KeyListener, MouseListener{
  14. String b;
  15. JTextArea a;
  16.                 FileDialog obj = new FileDialog(this);
  17.     public TextEdit() throws Exception
  18.     {
  19.           try {
  20.                  UIManager.setLookAndFeel(
  21.                     UIManager.getSystemLookAndFeelClassName());
  22.                 } 
  23.                 catch(Exception q) {q.printStackTrace();}
  24.      // JOptionPane.showMessageDialog(null,"ad");
  25.  
  26.         setResizable(true);
  27.         setBounds(15,15,500,500);
  28.         a = new JTextArea("");
  29.         JScrollPane b = new JScrollPane(a);
  30.         a.setRows(50);
  31.         a.addKeyListener(this);
  32.         a.addMouseListener(this);
  33.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  34.        JMenuBar menubar = new JMenuBar();
  35.        setJMenuBar(menubar);
  36.         JMenu file = new JMenu("File");
  37.         JMenuItem save = new JMenuItem("Save");
  38.         JMenuItem link = new JMenuItem("New");
  39.         JMenuItem quit = new JMenuItem("Quit");
  40.         JMenuItem open = new JMenuItem("Open");
  41.         //file menu
  42.         save.setAccelerator(
  43.             KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK));
  44.             save.addActionListener(new ActionListener() {
  45.                 public void actionPerformed(ActionEvent e) {
  46.  
  47.                 obj.setMode(FileDialog.SAVE);
  48.                 String filename = "Untitled";
  49.                 obj.show();
  50.                 filename = obj.getDirectory()+obj.getFile();
  51.                 setTitle(filename);
  52.                     try {
  53.                         BufferedWriter bw = new BufferedWriter(new PrintWriter(new File(filename + ".txt")));
  54.                         bw.write(a.getText());
  55.                         bw.close();
  56.                         System.out.println(a.getText());
  57.                     }
  58.                     catch( Exception f) {f.printStackTrace();}
  59.                 }
  60.             });
  61.            //Here is where the problem is:
  62.             open.setAccelerator(
  63.             KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));
  64.             open.addActionListener(new ActionListener() {
  65.                 public void actionPerformed(ActionEvent e) {
  66.  
  67.                 obj.setMode(FileDialog.LOAD);
  68.                 String filename = "Untitled";
  69.                 obj.show();
  70.                 filename = obj.getDirectory()+obj.getFile();
  71.                 setTitle(filename);
  72.  
  73.                        BufferedReader objBrIn = new BufferedReader(new FileReader(obj.file));
  74.                         String strTemp;
  75.                         String strOut = "";
  76.                         try {
  77.                             while ((strTemp = objBrIn.readLine()) != null) {
  78.                                 strOut += (strTemp + newline);
  79.                             }
  80.                             a.setText(strOut);
  81.                     }
  82.                     catch( Exception f) {f.printStackTrace();}
  83.  
  84.                 }
  85.             });
  86.  
  87.  
  88.             quit.setAccelerator(
  89.             KeyStroke.getKeyStroke(KeyEvent.VK_Q, Event.CTRL_MASK));
  90.             quit.addActionListener(new ActionListener() {
  91.                 public void actionPerformed(ActionEvent e) {System.exit(0);
  92.                 }
  93.             });
  94.             //file menu
  95.             //edit menu
  96.                         link.setAccelerator(
  97.             KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK));
  98.             link.addActionListener(new ActionListener() {
  99.                 public void actionPerformed(ActionEvent e) {
  100.                     JOptionPane optionPane = new JOptionPane("Are You SURE? \n Unsaved Data WILL be lost.", JOptionPane.QUESTION_MESSAGE,JOptionPane.YES_NO_OPTION);
  101.                         if(optionPane!=null) {
  102.                      a.setText(""); 
  103.                     }
  104.                     }
  105.             });
  106.  
  107.                     menubar.add(file);
  108.                     file.add(save);
  109.                     file.add(quit);
  110.                     file.add(link);
  111.                     Container c = getContentPane();
  112.                     c.add(b);
  113.                            setVisible(true);
  114.  
  115.     }
  116.     public void keyPressed(KeyEvent e) {
  117.     }
  118.     public void keyReleased(KeyEvent e) {}
  119.     public void keyTyped(KeyEvent e) {}
  120.     public void mouseExited(MouseEvent e) {}
  121.     public void mouseEntered(MouseEvent e) {}
  122.     public void mousePressed(MouseEvent e) {}
  123.     public void mouseReleased(MouseEvent e) {}
  124.     public void mouseClicked(MouseEvent e) {
  125.         if(e.getButton() == MouseEvent.BUTTON3)
  126.         {
  127.             JPopupMenu p = new JPopupMenu();
  128.  
  129.             p.add("Useless!!");
  130.             p.show();
  131.                         p.setVisible(true);
  132.         }
  133.     }
  134.     public static void main(String[] args) throws Exception
  135.     {
  136.         new TextEdit();
  137.     }
  138. }
  139.  
Nov 9 '07 #1
10 1647
r035198x
13,262 8TB
Why don't you make your FileDialog public on line 16 and see what happens. Remember that if you don't specify an access modifier for an atrribute, protected access is assumed.
Nov 10 '07 #2
Sorry, but I am stupid :( How do you make it public?
Nov 12 '07 #3
r035198x
13,262 8TB
Sorry, but I am stupid :( How do you make it public?
prefix the declaration with the word public?
Nov 12 '07 #4
I assume you mean:
Expand|Select|Wrap|Line Numbers
  1.  public FileDialog obj = new FileDialog(this);
I didn't work. :(
Am I doing something wrong?
Nov 12 '07 #5
r035198x
13,262 8TB
I assume you mean:
Expand|Select|Wrap|Line Numbers
  1.  public FileDialog obj = new FileDialog(this);
I didn't work. :(
Am I doing something wrong?
When you say it didn't work you mean you got the same error message?
Nov 12 '07 #6
Yep. The same error message. It didn't seem to do anything.
Nov 12 '07 #7
JosAH
11,448 Expert 8TB
Yep. The same error message. It didn't seem to do anything.
You can't just stick in 'public' somewhere because someone told you so and hope
for the best. A public method must be public for a reason, i.e. it must've been designed
that way and maybe that method turns up to be part of an interface implemented by
your class.

For now your class doesn't even compile; you should read what the compiler has
told you and fix the text of your source code. Just typing in what other people suggest
you without you knowing *what* you're doing is not going to help you.

kind regards,

Jos
Nov 12 '07 #8
Thats kind of why I posted this question-because I have no idea what to do. By telling me that I did it wrong and should do it myself, you helped me in no way. I still do not know what to do to make it work.
Nov 12 '07 #9
Sorry about double post.

I tinkered with the code for a little while and got this:
Expand|Select|Wrap|Line Numbers
  1.             open.setAccelerator(
  2.             KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));
  3.             open.addActionListener(new ActionListener() {
  4.                 public void actionPerformed(ActionEvent e) {
  5.  
  6.                 obj.setMode(FileDialog.LOAD);
  7.                 String filename = "Untitled";
  8.                 obj.show();
  9.                 filename = obj.getDirectory()+obj.getFile();
  10.                 setTitle(filename);
  11.                    try {
  12.               BufferedReader reader = new BufferedReader (new InputStreamReader( new FileInputStream (new File (filename))));
  13.               String firstLine = reader.readLine ();
  14.               System.out.println ("The first line is: " + firstLine);
  15.               a.setText(firstLine);
  16.             }
  17.               catch( Exception f) {f.printStackTrace();}
  18.  
  19.  
  20.  
  21.                 }
  22.             });
Nov 12 '07 #10
r035198x
13,262 8TB
Sorry about double post.

I tinkered with the code for a little while and got this:
Expand|Select|Wrap|Line Numbers
  1.             open.setAccelerator(
  2.             KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));
  3.             open.addActionListener(new ActionListener() {
  4.                 public void actionPerformed(ActionEvent e) {
  5.  
  6.                 obj.setMode(FileDialog.LOAD);
  7.                 String filename = "Untitled";
  8.                 obj.show();
  9.                 filename = obj.getDirectory()+obj.getFile();
  10.                 setTitle(filename);
  11.                    try {
  12.               BufferedReader reader = new BufferedReader (new InputStreamReader( new FileInputStream (new File (filename))));
  13.               String firstLine = reader.readLine ();
  14.               System.out.println ("The first line is: " + firstLine);
  15.               a.setText(firstLine);
  16.             }
  17.               catch( Exception f) {f.printStackTrace();}
  18.  
  19.  
  20.  
  21.                 }
  22.             });
You are reading a text file using a FileInputStream? Yuck.
Nov 13 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Amir Hardon | last post by:
I'm new to DOM and can't figure out this thing: I'm trying to add a row to a table with a form field in one of it's cells, but if I'm appending the field to a form it gets out of the table. Can...
5
by: surrealtrauma | last post by:
the requirement is : Create a class called Rational (rational.h) for performing arithmetic with fractions. Write a program to test your class. Use Integer variables to represent the private data...
15
by: crjunk | last post by:
I have 4 TextBoxes on my form that I'm trying to add together to get a grand total. Here is the code I'm using: <SCRIPT LANGUAGE="JavaScript"> <!-- Beginning of JavaScript - function...
15
by: Stormkid | last post by:
Hey Gang, I'm trying to figure out the best way to add two times together of the format hh:mm:ss any suggestions would be great thanks Todd
9
by: Michelle | last post by:
I have a div that is initially empty. Clicking on a button will add some text boxes and other controls so the user can add additional records. In IE all works fine but in Netscape 7.0 when I add...
1
by: The Eclectic Electric | last post by:
I'd be very grateful if anyone could help me with this. From my limited knowledge of Javascript I don't think it is possible, but I'll punt anyway. I downloaded and very slightly adapted this...
4
tolkienarda
by: tolkienarda | last post by:
hi all I am working on a php driven database program for a literacy program, it will allow them to keep track of classes and students, the part i am strugling with is adding new classes, the...
5
by: Sansasoon | last post by:
Hi there, I am supposed to do a shop ... which I ve done so far. The only thing that I can't get working is adding up to get the result It would be really great if someone could help me (Sorry it...
6
nomad
by: nomad | last post by:
Hello Everyone: I'm having troubles with some coding and Idon't know where it is. I have delevopled a calender where the client can add events to a day. The code worked perfect on my computer but...
3
by: didi86 | last post by:
Please help me to adding multiple row at a time... // Last updated 2006-02-21 <script language="javascript"> function addRowToTable() { var tbl = document.getElementById('tblSample'); ...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.