Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: First Go: I don't think my code is arranged properly.

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question First Go: I don't think my code is arranged properly.

    Hey guys, this is my First go at writing a program with Swing and my experience with java is severely limited. I am writing a program which will hopefully have basic file operation, modification of data within those files, search through an arraylist and import zip files. I have just started and don't think the structure i have made so far is right; any tips or advice would be great I tried writing the action event for my new or save button within it's own class but i haven't done it right and can't see where it is wrong or what i have done wrong. So yea thanks very much


    package menuInterface;

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import javax.swing.*;

    public class MovieManagerInterface implements ActionListener {
    ArrayList<String> list = new ArrayList<String>();
    String newfiletitle;
    JMenuBar menuBar;
    JTextArea output;
    JMenu filemenu, editmenu, viewmenu, searchmenu, importmenu, helpmenu,
    savemenu, subsearchmenu;
    JMenuItem newitem, openitem, saveitem, saveasitem, movieinfoitem,
    nextmovieitem, previousmovieitem, movieindexitem, modmovieitem,
    remmovieitem, addmovieitem, searchxtitleitem, filteritem,
    prevsearchitem, resetsearchitem, topmovieitem, importitem,
    importIMDBitem, importAltitem, helpitem, clearprefitem,
    showprefitem, aboutitem;
    JScrollPane scrollPane;
    JFrame frame, dialog;
    JFileChooser filechooser = new JFileChooser();

    private static void createWindow() {// top level container

    // Create and set up the window.
    JFrame frame = new JFrame("My Movie Manager");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    // Create and set up the content pane.
    MovieManagerInterface toplvl = new MovieManagerInterface();
    frame.setJMenuBar(toplvl.createMenuBar());
    frame.setContentPane(toplvl.contentPane());

    // Window Size
    frame.setSize(720, 576);
    frame.setVisible(true);
    }

    public JMenuBar createMenuBar() {

    // Create the menu bar.
    menuBar = new JMenuBar();

    // File Menu
    filemenu = new JMenu("File");
    menuBar.add(filemenu);
    filemenu.getAccessibleContext().setAccessibleDescr iption(
    "Contains the Buttons for the file commands");

    // File menu items
    newitem = new JMenuItem("New", KeyEvent.VK_N);
    newitem.setAccelerator(KeyStroke.getKeyStroke(KeyE vent.VK_N,
    ActionEvent.CTRL_MASK));
    newitem.addActionListener(NewSave());
    filemenu.add(newitem);// adds the item to the file menu

    openitem = new JMenuItem("Open", KeyEvent.VK_O);
    openitem.setAccelerator(KeyStroke.getKeyStroke(Key Event.VK_O,
    ActionEvent.CTRL_MASK));
    openitem.addActionListener(this);
    filemenu.add(openitem);// adds the item to the file menu
    filemenu.addSeparator(); // Separator to make it look pretty
    // Save Sub-menu
    savemenu = new JMenu("Save");
    saveitem = new JMenuItem("Save", KeyEvent.VK_S);
    saveitem.setAccelerator(KeyStroke.getKeyStroke(Key Event.VK_S,
    ActionEvent.CTRL_MASK));

    // create a save function which checks to see if the get filename
    // function returns a value if not execute the save as function
    saveitem.addActionListener(this);// find location of action listener
    savemenu.add(saveitem);

    saveasitem = new JMenuItem("Save As", KeyEvent.VK_S | KeyEvent.VK_SHIFT);
    saveasitem.addActionListener(this);
    saveasitem.setAccelerator(KeyStroke.getKeyStroke(K eyEvent.VK_S
    | KeyEvent.VK_SHIFT, ActionEvent.CTRL_MASK));
    savemenu.add(saveasitem);
    filemenu.add(savemenu);
    // View Menu

    viewmenu = new JMenu("View");
    viewmenu.getAccessibleContext().setAccessibleDescr iption(
    "Contains the View information");
    menuBar.add(viewmenu);
    movieinfoitem = new JMenuItem("Display Current Movie Information");
    movieinfoitem.addActionListener(this);
    viewmenu.add(movieinfoitem);
    nextmovieitem = new JMenuItem("Next Movie");
    nextmovieitem.addActionListener(this);
    viewmenu.add(nextmovieitem);
    previousmovieitem = new JMenuItem("Previous Movie");
    previousmovieitem.addActionListener(this);
    viewmenu.add(previousmovieitem);
    movieindexitem = new JMenuItem("Movie Index");// print the movie name
    movieindexitem.addActionListener(this);
    viewmenu.add(movieindexitem);

    // Edit menu
    editmenu = new JMenu("Edit");
    editmenu.getAccessibleContext().setAccessibleDescr iption(
    "Contains the Edit information");
    menuBar.add(editmenu);
    modmovieitem = new JMenuItem("Modify Current Movie");
    modmovieitem.addActionListener(this);
    editmenu.add(modmovieitem);
    remmovieitem = new JMenuItem("Remove Current Movie");
    remmovieitem.addActionListener(this);
    editmenu.add(remmovieitem);
    addmovieitem = new JMenuItem("Add Movie");
    addmovieitem.addActionListener(this);
    editmenu.add(addmovieitem);

    // Search Menu
    searchmenu = new JMenu("Search");
    searchmenu.getAccessibleContext().setAccessibleDes cription(
    "Contains the Search information");
    menuBar.add(searchmenu);
    subsearchmenu = new JMenu("Search..");
    searchxtitleitem = new JMenuItem("Search Title");
    searchxtitleitem.addActionListener(this);// find location of action
    // listener
    subsearchmenu.add(searchxtitleitem);
    filteritem = new JMenuItem("Filter TItle");
    filteritem.addActionListener(this);// find location of action listener
    subsearchmenu.add(filteritem);
    searchmenu.add(subsearchmenu);
    searchmenu.addSeparator();
    prevsearchitem = new JMenuItem("Previous Search Entry");
    prevsearchitem.addActionListener(this);
    searchmenu.add(prevsearchitem);
    resetsearchitem = new JMenuItem("Reset Search");
    resetsearchitem.addActionListener(this);
    searchmenu.add(resetsearchitem);
    // Import Menu
    importmenu = new JMenu("Import");
    importmenu.getAccessibleContext().setAccessibleDes cription(
    "Contains the Import information");
    menuBar.add(importmenu);
    topmovieitem = new JMenuItem("Top Ranked Movies");
    topmovieitem.addActionListener(this);
    importmenu.add(topmovieitem);
    importIMDBitem = new JMenuItem("Import IMDB file");
    importIMDBitem.addActionListener(this);
    importmenu.add(importIMDBitem);
    importAltitem = new JMenuItem("Import Alternate IMDB files");
    importAltitem.addActionListener(this);
    importmenu.add(importAltitem);

    helpmenu = new JMenu("Help");
    helpmenu.getAccessibleContext().setAccessibleDescr iption(
    "Contains the Help information");
    menuBar.add(helpmenu);
    helpitem = new JMenuItem("Help");
    helpitem.addActionListener(this);
    helpmenu.add(helpitem);
    clearprefitem = new JMenuItem("Clear Preferences");
    clearprefitem.addActionListener(this);
    helpmenu.add(clearprefitem);
    showprefitem = new JMenuItem("Show Preferences");
    showprefitem.addActionListener(this);
    helpmenu.add(showprefitem);
    aboutitem = new JMenuItem("About");// Say what the program is about
    aboutitem.addActionListener(this);
    helpmenu.add(aboutitem);
    return menuBar;
    }

    private ActionListener NewSave() {
    // TODO Auto-generated method stub
    return NewSave();
    }

    public JPanel contentPane() {

    JPanel contentPane = new JPanel(new BorderLayout());
    contentPane.setOpaque(false);

    int length = 100; // set to length of the document which you open

    // Create a scrolled text area.
    output = new JTextArea(length, 30);
    output.setEditable(false);
    scrollPane = new JScrollPane(output);

    // Add the text area to the content pane.
    contentPane.add(scrollPane, BorderLayout.CENTER);

    return contentPane;
    }


    @Override
    public void actionPerformed(ActionEvent a) {} //need to sort this out ed
    class NewSave {

    public void NewSavefunction(ActionEvent newfile) {

    if ((newfile.getSource() == newitem)
    | (newfile.getSource() == saveasitem)) {

    int value = filechooser.showSaveDialog(null);

    if (value == JFileChooser.APPROVE_OPTION) {
    File file = filechooser.getSelectedFile();
    try {
    file.createNewFile();// create file
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    if (newfile.getSource() == newitem)// if statement to
    // seperate new and
    // save as when creating
    // appropriate dialog box
    {
    JOptionPane.showMessageDialog(dialog,
    "Created file: " + file.getName() + ".\n",
    "My Movie Manager",
    JOptionPane.PLAIN_MESSAGE);
    } else {
    JOptionPane.showMessageDialog(dialog,
    "Saved file: " + file.getName() + ".\n",
    "My Movie Manager",
    JOptionPane.PLAIN_MESSAGE);
    }
    } else {

    JOptionPane.showMessageDialog(dialog,
    "File Error: Unable to complete task",
    "Warning", JOptionPane.WARNING_MESSAGE);
    }
    }
    }

    }
    }

    public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    createWindow();
    }
    });
    }

    }


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: First Go: I don't think my code is arranged properly.

    its hard to tell and look at your program that easy, please wrap your codes in code tags

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: First Go: I don't think my code is arranged properly.

    Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

Similar Threads

  1. Code compiles fine but doesn't run Properly
    By nadirkhan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 9th, 2011, 12:46 PM
  2. not looping properly, please help.
    By nakedtriple in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 28th, 2011, 08:31 AM
  3. Replies: 2
    Last Post: July 8th, 2011, 06:33 AM
  4. Code doesn't execute properly
    By fride360 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 12th, 2011, 12:36 PM
  5. if else statement not working properly
    By tina G in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 29th, 2010, 08:04 AM

Tags for this Thread