Page 1 of 2 12 LastLast
Results 1 to 20 of 31
  1. #1
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default what is wrong with my code???

    if i click at open, it shows the open dialog but if i choose a .txt file, nothing happen!!!!!

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    
    class notepad extends JFrame implements ActionListener {
    
    
        public static void main (String [] args) {
    
                 
     notepad frame = new notepad ();  
    
        }
    public notepad () {
     
    
                Container myPane = getContentPane();
            
            setVisible(true);
            setSize(500,500);
            setTitle("My Menu Class");
            
            JTextArea myTextArea = new JTextArea ();
            
            setLayout(null);
            myTextArea.setBounds(0,0,500,500); add(myTextArea);
    
            JMenuBar myMenu = new JMenuBar();
            setJMenuBar(myMenu);
            
            JMenu file = new JMenu("file");
            myMenu.add(file);
            
            
            JMenuItem new_ = new JMenuItem("New");
            file.add(new_);
            new_.addActionListener(this);
            
            JMenuItem open = new JMenuItem("Open");
            file.add(open);
            open.addActionListener(this);
                    
            JMenuItem saveAs = new JMenuItem("Save As");
            file.add(saveAs);
            saveAs.addActionListener(this);
            
            JMenuItem exit = new JMenuItem("Exit");
            file.add(exit);
            exit.addActionListener(this);
    
    
            
                setDefaultCloseOperation(EXIT_ON_CLOSE);
            }
    //===================================================
        public void actionPerformed(ActionEvent myEvent) {
            String file = myEvent.getActionCommand();
            String doc = " ";
            
            if (file.equals("Exit")){
                System.exit(0);
            }
            
            
            else if (file.equals("Open")){
                 notepad frame = new notepad ();   
    
                String filename = File.separator+"tmp";
                JFileChooser fc = new JFileChooser(new File(filename));
    
                // Show open dialog; this method does not return until the dialog is closed
                fc.showOpenDialog(frame);
                File selFile = fc.getSelectedFile();
                
            }
                
    
    
        }
    }
    Last edited by Fubarable; 07-27-2010 at 12:18 AM. Reason: Moderator edit: nevermind, I'll do it for you.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default

    but if i choose a .txt file, nothing happen
    What is your code supposed to do?
    I don't see anything in the code to "happen".

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default

    sorry i always do the same mistake :S

    this program is like a NotePAd

    Moderator Edit: left-justified code deleted. Again, please edit the code in your first post as I suggested above. All you need to do is change the final tag!
    Last edited by Fubarable; 07-27-2010 at 12:20 AM. Reason: moderator edit

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default

    Is your code all left adjusted like you have posted?

  6. #6
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default

    what do you mean?

  7. #7
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default

    ok i just got it
    no it is not

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default

    If you want something to happen in your code, you're going to have to add the code to do it.

  9. #9
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default

    i found many code, but i couldn't use any of them, always there is errors :S

    like this one
    Java Code:
                 public String openFile() throws FileNotFoundException, IOExveption{
                 String filename= "  ";
                 System.getProperty("user.dir");
                 int reply = chooser.showOpenDialog(null);
                 if(reply == JFileChooser.APPROVE_OPTION) {
                     doc = openFile(chooser.getSelectedFile().getAbsolutePath());
                    }
                    return doc;
                }
    or this one

    Java Code:
    public class OpenFileAction extends AbstractAction {
        JFrame frame;
        JFileChooser chooser;
    
        OpenFileAction(JFrame frame, JFileChooser chooser) {
            super("Open...");
            this.chooser = chooser;
            this.frame = frame;
        }
    
        public void actionPerformed(ActionEvent evt) {
            // Show dialog; this method does not return until dialog is closed
            chooser.showOpenDialog(frame);
    
            // Get the selected file
            File file = chooser.getSelectedFile();
        }
    };

  10. #10
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Java Code:
    Container myPane = getContentPane();
    You have defined myPane but you have never used it.
    As far I can see, try to add menu using myPane and for example BorderLayout.NORTH

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please see moderator edits above. Next time, please just edit the first post as suggested rather than reposting poorly formatted code.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default

    Instead of adding code to your current program, write a small program that is given a File (that is what you get from JFileChooser) and reads its contents into a container of some kind like an ArrayList or Vector or array. When you get that to work, you can add its code to the main program.

  13. #13
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Java Code:
    i found many code, but i couldn't use any of them, always there is errors
    try to use search engines not just for searching code, but for learning too. This pieces of code are great opportunity to learn more things and improve your knowledge.
    If your have problems with learning new technics post some questions about them we will be willing to help you.

  14. #14
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default

    ok why this is not workin???

    Java Code:
             else if (file.equals("Open")){
                 JFileChooser chooser= new JFileChooser();
                 public String openFile() throws FileNotFoundException, IOExveption {
                 String filename= "  ";
                 System.getProperty("user.dir");
                 int reply = chooser.showOpenDialog(null);
                 if(reply == JFileChooser.APPROVE_OPTION) {
                     doc = openFile(chooser.getSelectedFile().getAbsolutePath());
                    }
                    return doc;
                }}

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default

    What is it supposed to do?
    Do you get errors?
    Why do you think it is not working?

  16. #16
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default

    i think it is to open a file
    yes i got an error
    "illegal start of expression"

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default

    Where is the error message?

    If you are just copy and pasting code from other programs without understanding what the code does or how it works, you're wasting time for everyone.

    You need to understand how classes work and how to design and code a program. Perhaps some of the tutorials could help you.

  18. #18
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default

    the error message is in this line
    Java Code:
    public String openFile() throws FileNotFoundException, IOExveption {
    i didn't just copy and paste, i change somethings

    i'm really sorry for wasting your time but i really don't know

    even the tutorials, there are a lot of thing that is new for me and my english is a little bit poor

    i have a question, is opening a file something difficult or it is just that i'm doing it wrong???

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default

    Re Your English. Have you tried using Google translate? I'm curious if it would work for technical topics like we have on this forum.
    Type you responses in your language and have Google translate them to English.

    is opening a file something difficult
    No its not difficult to open and read a file.

    that i'm doing it wrong
    I don't recognize anything in the code that you posted that will open and read a file.

    Search on this forum for FileReader or BufferedReader for code samples.

  20. #20
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    I have done a few little improvements to your program.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    
    public class Notepad extends JFrame implements ActionListener {
    
    public static void main (String [] args) {
    	SwingUtilities.invokeLater(new Runnable() {
    		
    		@Override
    		public void run() {
    			Notepad frame = new Notepad ();
    			frame.showGraphicUserInterface();
    		}
    	});
    
    }
    
    public Notepad () {
    
    	Container myPane = getContentPane();
    	JTextArea myTextArea = new JTextArea ();
    	
    	setLayout(new BorderLayout());
    	myPane.add(myTextArea, BorderLayout.CENTER);
    	
    	JMenuBar myMenu = new JMenuBar();
    	setJMenuBar(myMenu);
    	
    	JMenu file = new JMenu("file");
    	myMenu.add(file);
    	
    	JMenuItem new_ = new JMenuItem("New");
    	file.add(new_);
    	new_.addActionListener(this);
    	
    	JMenuItem open = new JMenuItem("Open");
    	file.add(open);
    	open.addActionListener(this);
    	
    	JMenuItem saveAs = new JMenuItem("Save As");
    	file.add(saveAs);
    	saveAs.addActionListener(this);
    	
    	JMenuItem exit = new JMenuItem("Exit");
    	file.add(exit);
    	exit.addActionListener(this);
    }
    
    public void showGraphicUserInterface() {
    	setTitle("My Menu Class");
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
    	setVisible(true);
    	setSize(500,500);
    }
    
    public void actionPerformed(ActionEvent myEvent) {
    	String file = myEvent.getActionCommand();
    	String doc = " ";
    	
    		
    		if (file.equals("Exit")){
    			System.exit(0);
    		}
    		else if (file.equals("Open")){
    			Notepad frame = new Notepad (); 
    			
    			String filename = File.separator + "tmp";
    			JFileChooser fc = new JFileChooser(new File(filename));
    			
    			// Show open dialog; this method does not return until the dialog is closed
    			fc.showOpenDialog(frame);
    			File selFile = fc.getSelectedFile();
    		}
    	}
    }
    Here I have used threads to show graphic user interface:
    Java Code:
    	SwingUtilities.invokeLater(new Runnable() {
    		
    		@Override
    		public void run() {
    			Notepad frame = new Notepad ();
    			frame.showGraphicUserInterface();
    		}
    	});
    Second some things from notepad constructor were displaced to new function showGraphicsUserInterface() (you can rename it if you want).
    Java Code:
    public void showGraphicUserInterface() {
    	setTitle("My Menu Class");
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
    	setVisible(true);
    	setSize(500,500);
    }
    Then I used BorderLayout to put your menu to NORTH.
    Java Code:
    	myPane.add(myTextArea, BorderLayout.CENTER);
    So my goal is to help you to improve your programmer skills and learn more cool programming stuffs.
    Can you explain to us what you're trying to do in with the code in actionPerformed method?

Page 1 of 2 12 LastLast

Similar Threads

  1. What's going wrong with this code?
    By Suurisa in forum New To Java
    Replies: 5
    Last Post: 10-20-2009, 12:59 AM
  2. what's the wrong in this Code
    By the swan in forum AWT / Swing
    Replies: 1
    Last Post: 04-04-2009, 05:27 AM
  3. what's wrong with my code? please help me...
    By lovely23 in forum Java Applets
    Replies: 2
    Last Post: 03-01-2009, 02:33 PM
  4. What's wrong with this code?
    By Doctor Cactus in forum New To Java
    Replies: 4
    Last Post: 11-29-2008, 06:44 PM
  5. what is wrong with this code
    By masaka in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 09:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •