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();
}
});
}
}