Adding input?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dudeishfish
    New Member
    • Nov 2007
    • 11

    #1

    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.filedi alog; cannot be accessed from outside package". Here is the code:
    Code:
    package connect4;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.imageio.*;
    import java.io.File;
    import javax.swing.Popup;
    import java.io.*;
    import java.awt.FileDialog.*;
    import java.io.FileWriter;
    import java.awt.image.*;
    import java.awt.BorderLayout;
    public class TextEdit extends JFrame implements KeyListener, MouseListener{
    String b;
    JTextArea a;
                    FileDialog obj = new FileDialog(this);
        public TextEdit() throws Exception
        {
              try {
                     UIManager.setLookAndFeel(
                        UIManager.getSystemLookAndFeelClassName());
                    } 
                    catch(Exception q) {q.printStackTrace();}
         // JOptionPane.showMessageDialog(null,"ad");
    
            setResizable(true);
            setBounds(15,15,500,500);
            a = new JTextArea("");
            JScrollPane b = new JScrollPane(a);
            a.setRows(50);
            a.addKeyListener(this);
            a.addMouseListener(this);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
           JMenuBar menubar = new JMenuBar();
           setJMenuBar(menubar);
            JMenu file = new JMenu("File");
            JMenuItem save = new JMenuItem("Save");
            JMenuItem link = new JMenuItem("New");
            JMenuItem quit = new JMenuItem("Quit");
            JMenuItem open = new JMenuItem("Open");
            //file menu
            save.setAccelerator(
                KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK));
                save.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
    
                    obj.setMode(FileDialog.SAVE);
                    String filename = "Untitled";
                    obj.show();
                    filename = obj.getDirectory()+obj.getFile();
                    setTitle(filename);
                        try {
                            BufferedWriter bw = new BufferedWriter(new PrintWriter(new File(filename + ".txt")));
                            bw.write(a.getText());
                            bw.close();
                            System.out.println(a.getText());
                        }
                        catch( Exception f) {f.printStackTrace();}
                    }
                });
               //Here is where the problem is:
               [B] open.setAccelerator(
                KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));
                open.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
    
                    obj.setMode(FileDialog.LOAD);
                    String filename = "Untitled";
                    obj.show();
                    filename = obj.getDirectory()+obj.getFile();
                    setTitle(filename);
                       
                           BufferedReader objBrIn = new BufferedReader(new FileReader(obj.file));
                            String strTemp;
                            String strOut = "";
                            try {
                                while ((strTemp = objBrIn.readLine()) != null) {
                                    strOut += (strTemp + newline);
                                }
                                a.setText(strOut);
                        }
                        catch( Exception f) {f.printStackTrace();}
                       
                    }
                });
    [/B]
    
            
                quit.setAccelerator(
                KeyStroke.getKeyStroke(KeyEvent.VK_Q, Event.CTRL_MASK));
                quit.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {System.exit(0);
                    }
                });
                //file menu
                //edit menu
                            link.setAccelerator(
                KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK));
                link.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        JOptionPane optionPane = new JOptionPane("Are You SURE? \n Unsaved Data WILL be lost.", JOptionPane.QUESTION_MESSAGE,JOptionPane.YES_NO_OPTION);
                            if(optionPane!=null) {
                         a.setText(""); 
                        }
                        }
                });
    
                        menubar.add(file);
                        file.add(save);
                        file.add(quit);
                        file.add(link);
                        Container c = getContentPane();
                        c.add(b);
                               setVisible(true);
    
        }
        public void keyPressed(KeyEvent e) {
        }
        public void keyReleased(KeyEvent e) {}
        public void keyTyped(KeyEvent e) {}
        public void mouseExited(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseClicked(MouseEvent e) {
            if(e.getButton() == MouseEvent.BUTTON3)
            {
                JPopupMenu p = new JPopupMenu();
    
                p.add("Useless!!");
                p.show();
                            p.setVisible(true);
            }
        }
        public static void main(String[] args) throws Exception
        {
            new TextEdit();
        }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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.

    Comment

    • dudeishfish
      New Member
      • Nov 2007
      • 11

      #3
      Sorry, but I am stupid :( How do you make it public?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by dudeishfish
        Sorry, but I am stupid :( How do you make it public?
        prefix the declaration with the word public?

        Comment

        • dudeishfish
          New Member
          • Nov 2007
          • 11

          #5
          I assume you mean:
          Code:
           [B]public[/B] FileDialog obj = new FileDialog(this);
          I didn't work. :(
          Am I doing something wrong?

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by dudeishfish
            I assume you mean:
            Code:
             [b]public[/b] 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?

            Comment

            • dudeishfish
              New Member
              • Nov 2007
              • 11

              #7
              Yep. The same error message. It didn't seem to do anything.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by dudeishfish
                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

                Comment

                • dudeishfish
                  New Member
                  • Nov 2007
                  • 11

                  #9
                  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.

                  Comment

                  • dudeishfish
                    New Member
                    • Nov 2007
                    • 11

                    #10
                    Sorry about double post.

                    I tinkered with the code for a little while and got this:
                    Code:
                                open.setAccelerator(
                                KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));
                                open.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent e) {
                    
                                    obj.setMode(FileDialog.LOAD);
                                    String filename = "Untitled";
                                    obj.show();
                                    filename = obj.getDirectory()+obj.getFile();
                                    setTitle(filename);
                                       try {
                                  BufferedReader reader = new BufferedReader (new InputStreamReader( new FileInputStream (new File (filename))));
                                  String firstLine = reader.readLine ();
                    			  System.out.println ("The first line is: " + firstLine);
                    			  a.setText(firstLine);
                                }
                                  catch( Exception f) {f.printStackTrace();}
                                  
                                       
                                    
                                    }
                                });

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by dudeishfish
                      Sorry about double post.

                      I tinkered with the code for a little while and got this:
                      Code:
                                  open.setAccelerator(
                                  KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));
                                  open.addActionListener(new ActionListener() {
                                      public void actionPerformed(ActionEvent e) {
                      
                                      obj.setMode(FileDialog.LOAD);
                                      String filename = "Untitled";
                                      obj.show();
                                      filename = obj.getDirectory()+obj.getFile();
                                      setTitle(filename);
                                         try {
                                    BufferedReader reader = new BufferedReader (new InputStreamReader( new FileInputStream (new File (filename))));
                                    String firstLine = reader.readLine ();
                      			  System.out.println ("The first line is: " + firstLine);
                      			  a.setText(firstLine);
                                  }
                                    catch( Exception f) {f.printStackTrace();}
                                    
                                         
                                      
                                      }
                                  });
                      You are reading a text file using a FileInputStream ? Yuck.

                      Comment

                      Working...