Results 1 to 3 of 3
  1. #1
    thangli is offline Member
    Join Date
    Nov 2008
    Posts
    1
    Rep Power
    0

    Default Rewrite as a function so it can call from main.

    I need help to rewrite this code into a function so that I can call it from the main program. Please guide me how, I have tried in various ways and it could not work. Thanks.
    This one also has a little problem that I want user not be able to delete or copy the text when he open a file to read. Thanks In advances.
    ===
    public static void editor()
    {
    The below program should be in here, so the main can call. Thanks.
    }
    ===
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;

    import javax.swing.*;

    public class Editor
    extends JFrame
    implements ActionListener {
    public static void main(String[] s) { new Editor( ); }

    private JEditorPane textPane = new JEditorPane( );

    public Editor( ) {
    super("Editor v1.0");
    addWindowListener(new WindowAdapter( ) {
    public void windowClosing(WindowEvent e) { System.exit(0); }
    });
    Container content = getContentPane( );
    content.add(new JScrollPane(textPane), BorderLayout.CENTER);
    JMenu menu = new JMenu("File");
    menu.add(makeMenuItem("Open"));
    menu.add(makeMenuItem("Save"));
    menu.add(makeMenuItem("Quit"));
    JMenuBar menuBar = new JMenuBar( );
    menuBar.add(menu);
    setJMenuBar(menuBar);
    setSize(300, 300);
    setLocation(200, 200);
    setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand( );
    if (command.equals("Quit")) System.exit(0);
    else if (command.equals("Open")) loadFile( );
    //else if (command.equals("Save")) saveFile( );
    }

    private void loadFile ( ) {
    JFileChooser chooser = new JFileChooser( );
    int result = chooser.showOpenDialog(this);
    if (result == JFileChooser.CANCEL_OPTION) return;
    try {
    File file = chooser.getSelectedFile( );
    java.net.URL url = file.toURL( );
    textPane.setPage(url);
    }
    catch (Exception e) {
    textPane.setText("Could not load file: " + e);
    }
    }

    private void saveFile( ) {
    JFileChooser chooser = new JFileChooser( );
    chooser.showSaveDialog(this);
    // Save file data...
    }

    private JMenuItem makeMenuItem( String name ) {
    JMenuItem m = new JMenuItem( name );
    m.addActionListener( this );
    return m;
    }
    }
    ===
    Last edited by thangli; 11-28-2008 at 12:38 AM. Reason: Modify

  2. #2
    jralexander is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default

    you should repost this using the "code" "/code" just replace the "" with [] tags so it will be easier to read....
    Last edited by jralexander; 11-30-2008 at 06:06 AM.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default hhhmmm...

    I don't understand... there already is a main method:
    Java Code:
    public static void main(String[] s) { new Editor( ); }
    The main method is instantiating the Editor method.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. rewrite mousedown code
    By brahms666 in forum New To Java
    Replies: 0
    Last Post: 11-26-2008, 01:15 AM
  2. how to add main function
    By spratana in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 01:33 PM
  3. Replies: 1
    Last Post: 08-07-2007, 06:16 AM
  4. help with System.exit (1) function call
    By katie in forum Advanced Java
    Replies: 2
    Last Post: 08-06-2007, 08:03 PM

Posting Permissions

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