Join 149,479 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,486 people online right now. Registration is fast and FREE... Join Now!
I am trying to make a program that will display music and sort it. I have need to be able to sort using the menu submenu(ie. click on edit>sort>then by artist, record label, etc. No searching. I tried using parts of sample we did in the book but isn't working. I get searchinput box then I goto sort
//construct instance of FavMusic public FavMusic() { super("Favorite Music"); }
//create the menu system public JMenuBar createMenuBar() { //create an instance of the the menu JMenuBar mnuBar = new JMenuBar(); setJMenuBar(mnuBar);
//construct and populate the file menu JMenu mnuFile = new JMenu("File", true); mnuFile.setMnemonic(KeyEvent.VK_F); mnuFile.setDisplayedMnemonicIndex(0); mnuBar.add(mnuFile);
JMenuItem mnuFileExit = new JMenuItem("Exit"); mnuFileExit.setMnemonic(KeyEvent.VK_X); mnuFileExit.setDisplayedMnemonicIndex(1); mnuFile.add(mnuFileExit); mnuFileExit.setActionCommand("Exit"); mnuFileExit.addActionListener(this);
//construct and populate the Edit menu JMenu mnuEdit = new JMenu("Edit", true); mnuEdit.setMnemonic(KeyEvent.VK_E); mnuEdit.setDisplayedMnemonicIndex(0); mnuBar.add(mnuEdit);
JMenuItem mnuEditInsert = new JMenuItem("Insert New Music"); mnuEditInsert.setMnemonic(KeyEvent.VK_I); mnuEditInsert.setDisplayedMnemonicIndex(0); mnuEdit.add(mnuEditInsert); mnuEditInsert.setActionCommand("Insert"); mnuEditInsert.addActionListener(this);
JMenu mnuEditSort = new JMenu("Sort"); mnuEditSort.setMnemonic(KeyEvent.VK_R); mnuEditSort.setDisplayedMnemonicIndex(3); mnuEdit.add(mnuEditSort);
JMenuItem mnuEditSortByrecordLabel = new JMenuItem("by Record Label"); mnuEditSortByrecordLabel.setMnemonic(KeyEvent.VK_T); mnuEditSortByrecordLabel.setDisplayedMnemonicIndex(3); mnuEditSort.add(mnuEditSortByrecordLabel); mnuEditSortByrecordLabel.setActionCommand("recordLabel"); mnuEditSortByrecordLabel.addActionListener(this);
return mnuBar; }
//create the content pane public Container createContentPane() { //create the JTextPane and center panel JPanel centerPanel = new JPanel(); setTabsAndStyles(textPane); textPane = addTextToTextPane(); JScrollPane scrollPane = new JScrollPane(textPane); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setPreferredSize(new Dimension(500, 200)); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setPreferredSize(new Dimension(500, 200)); centerPanel.add(scrollPane);
//create container and set attributes Container c = getContentPane(); c.setLayout(new BorderLayout(1,5)); //c.add(northPanel, BorderLayout.NORTH); c.add(centerPanel, BorderLayout.CENTER);
return c; }
//method to create tabe stops and set font style protected void setTabsAndStyles(JTextPane textPane) { //create Tab Stops TabStop[] tabs = new TabStop[2]; tabs[0] = new TabStop(200, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE); tabs[1] = new TabStop(350, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE); TabSet tabset = new TabSet(tabs);
Style s = textPane.addStyle("italic", regular); StyleConstants.setItalic(s, true);
s = textPane.addStyle("bold", regular); StyleConstants.setBold(s, true);
s = textPane.addStyle("large", regular); StyleConstants.setFontSize(s, 16); }
//method to add new text to the JTextpane public JTextPane addTextToTextPane() { Document doc = textPane.getDocument(); try { //clear previous text doc.remove(0, doc.getLength());
//user clicks the sort by combo box if (arg == "Sort") { switch(e.getSelectedIndex()) { case 0: sort(artistName); break; case 1: sort(genre); break; case 2: sort(greatestHit); break; } }
//user clicks exit on the file menu if (arg == "Exit") System.exit(0);
//user clicks Insert New FavMusic on the edit menu if (arg == "Insert") { //accept new data String newartistName = JOptionPane.showInputDialog(null, "Please enter the movie's new Artist"); String newGenre = JOptionPane.showInputDialog(null, "Please enter the genre for " + newartistName); String newgreatestHit = JOptionPane.showInputDialog(null, "Please enter the Greatest Hit for for " + newartistName); String newrecordLabel = JOptionPane.showInputDialog(null, "Please enter the Record Label for for " + newartistName);
//user clicks artistName on the search submenu //if (arg == "artistName") // sort(arg, artistName);
//user clicks genre on the search submenu //if (arg == "genre") // sort(arg, genre);
//user clicks greatestHit on the search submenu //if (arg == "greatestHit") // sort(arg, greatestHit);
//user clicks recordLabel on the search submenu //if (arg == "recordLabel") // sort(arg, recordLabel); }
//method to enlarge an array by 1 public String[] enlargeArray(String[] currentArray) { String[] newArray = new String[currentArray.length + 1]; for (int i = 0; i < currentArray.length; i++) newArray[i] = currentArray[i]; return newArray; }
//method to sort arrays public void sort(String tempArray[]) { //loop to control number of passes for (int pass = 1; pass <tempArray.length; pass++) { for (int element = 0; element < tempArray.length - 1; element ++) if (tempArray[element].compareTo(tempArray[element + 1]) > 0) { swap (artistName, element, element + 1); swap (genre, element, element + 1); swap (greatestHit, element, element + 1); swap (recordLabel, element, element + 1); } } addTextToTextPane(); }
//method to swap two elements of an array public void swap(String swapArray[], int first, int second) { String hold; //temporary area for swap hold = swapArray[first]; swapArray[first] = swapArray[second]; swapArray[second] = hold; }
public void search(String searchField, String searchArray[]) { try { Document doc = textPane.getDocument(); //assign text to document object doc.remove(0, doc.getLength()); //clear previous text
//prompt user for search data String search = JOptionPane.showInputDialog(null, "Please enter the " + searchField); boolean found = false;
//search arrays for (int i = 0; i < artistName.length; i++) { if (search.compareTo(searchArray[i]) == 0) { doc.insertString(doc.getLength(), artistName[i] + "\t", textPane.getStyle ("bold")); doc.insertString(doc.getLength(), genre[i] + "\t", textPane.getStyle("italic")); doc.insertString(doc.getLength(), greatestHit[i] + "\n", textPane.getStyle("regular")); doc.insertString(doc.getLength(), recordLabel[i] + "\n", textPane.getStyle("regular")); found = true; } } if (found == false) { JOptionPane.showMessageDialog(null, "Your search produced no results.", "no results found", JOptionPane.INFORMATION_MESSAGE); sort(artistName); } } catch (BadLocationException ble) { System.err.println("Couldn't insert text."); } }
//main method executes at run time public static void main(String arg[]) { JFrame.setDefaultLookAndFeelDecorated(true); FavMusic f = new FavMusic(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setJMenuBar(f.createMenuBar()); f.setContentPane(f.createContentPane()); f.setSize(600, 275); f.setVisible(true); } }
The menu items show on the menu bar fine. I'm not sure what you're talking about (newbie...sorry). The add mnubar is last for each section I adding
QUOTE(dbrine @ 8 May, 2007 - 05:17 PM)
I am trying to make a program that will display music and sort it. I have need to be able to sort using the menu submenu(ie. click on edit>sort>then by artist, record label, etc. No searching. I tried using parts of sample we did in the book but isn't working. I get searchinput box then I goto sort
//construct instance of FavMusic public FavMusic() { super("Favorite Music"); }
//create the menu system public JMenuBar createMenuBar() { //create an instance of the the menu JMenuBar mnuBar = new JMenuBar(); setJMenuBar(mnuBar);
//construct and populate the file menu JMenu mnuFile = new JMenu("File", true); mnuFile.setMnemonic(KeyEvent.VK_F); mnuFile.setDisplayedMnemonicIndex(0); mnuBar.add(mnuFile);
JMenuItem mnuFileExit = new JMenuItem("Exit"); mnuFileExit.setMnemonic(KeyEvent.VK_X); mnuFileExit.setDisplayedMnemonicIndex(1); mnuFile.add(mnuFileExit); mnuFileExit.setActionCommand("Exit"); mnuFileExit.addActionListener(this);
//construct and populate the Edit menu JMenu mnuEdit = new JMenu("Edit", true); mnuEdit.setMnemonic(KeyEvent.VK_E); mnuEdit.setDisplayedMnemonicIndex(0); mnuBar.add(mnuEdit);
JMenuItem mnuEditInsert = new JMenuItem("Insert New Music"); mnuEditInsert.setMnemonic(KeyEvent.VK_I); mnuEditInsert.setDisplayedMnemonicIndex(0); mnuEdit.add(mnuEditInsert); mnuEditInsert.setActionCommand("Insert"); mnuEditInsert.addActionListener(this);
JMenu mnuEditSort = new JMenu("Sort"); mnuEditSort.setMnemonic(KeyEvent.VK_R); mnuEditSort.setDisplayedMnemonicIndex(3); mnuEdit.add(mnuEditSort);
JMenuItem mnuEditSortByrecordLabel = new JMenuItem("by Record Label"); mnuEditSortByrecordLabel.setMnemonic(KeyEvent.VK_T); mnuEditSortByrecordLabel.setDisplayedMnemonicIndex(3); mnuEditSort.add(mnuEditSortByrecordLabel); mnuEditSortByrecordLabel.setActionCommand("recordLabel"); mnuEditSortByrecordLabel.addActionListener(this);
return mnuBar; }
//create the content pane public Container createContentPane() { //create the JTextPane and center panel JPanel centerPanel = new JPanel(); setTabsAndStyles(textPane); textPane = addTextToTextPane(); JScrollPane scrollPane = new JScrollPane(textPane); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setPreferredSize(new Dimension(500, 200)); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setPreferredSize(new Dimension(500, 200)); centerPanel.add(scrollPane);
//create container and set attributes Container c = getContentPane(); c.setLayout(new BorderLayout(1,5)); //c.add(northPanel, BorderLayout.NORTH); c.add(centerPanel, BorderLayout.CENTER);
return c; }
//method to create tabe stops and set font style protected void setTabsAndStyles(JTextPane textPane) { //create Tab Stops TabStop[] tabs = new TabStop[2]; tabs[0] = new TabStop(200, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE); tabs[1] = new TabStop(350, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE); TabSet tabset = new TabSet(tabs);
Style s = textPane.addStyle("italic", regular); StyleConstants.setItalic(s, true);
s = textPane.addStyle("bold", regular); StyleConstants.setBold(s, true);
s = textPane.addStyle("large", regular); StyleConstants.setFontSize(s, 16); }
//method to add new text to the JTextpane public JTextPane addTextToTextPane() { Document doc = textPane.getDocument(); try { //clear previous text doc.remove(0, doc.getLength());
//user clicks the sort by combo box if (arg == "Sort") { switch(e.getSelectedIndex()) { case 0: sort(artistName); break; case 1: sort(genre); break; case 2: sort(greatestHit); break; } }
//user clicks exit on the file menu if (arg == "Exit") System.exit(0);
//user clicks Insert New FavMusic on the edit menu if (arg == "Insert") { //accept new data String newartistName = JOptionPane.showInputDialog(null, "Please enter the movie's new Artist"); String newGenre = JOptionPane.showInputDialog(null, "Please enter the genre for " + newartistName); String newgreatestHit = JOptionPane.showInputDialog(null, "Please enter the Greatest Hit for for " + newartistName); String newrecordLabel = JOptionPane.showInputDialog(null, "Please enter the Record Label for for " + newartistName);
//user clicks artistName on the search submenu //if (arg == "artistName") // sort(arg, artistName);
//user clicks genre on the search submenu //if (arg == "genre") // sort(arg, genre);
//user clicks greatestHit on the search submenu //if (arg == "greatestHit") // sort(arg, greatestHit);
//user clicks recordLabel on the search submenu //if (arg == "recordLabel") // sort(arg, recordLabel); }
//method to enlarge an array by 1 public String[] enlargeArray(String[] currentArray) { String[] newArray = new String[currentArray.length + 1]; for (int i = 0; i < currentArray.length; i++) newArray[i] = currentArray[i]; return newArray; }
//method to sort arrays public void sort(String tempArray[]) { //loop to control number of passes for (int pass = 1; pass <tempArray.length; pass++) { for (int element = 0; element < tempArray.length - 1; element ++) if (tempArray[element].compareTo(tempArray[element + 1]) > 0) { swap (artistName, element, element + 1); swap (genre, element, element + 1); swap (greatestHit, element, element + 1); swap (recordLabel, element, element + 1); } } addTextToTextPane(); }
//method to swap two elements of an array public void swap(String swapArray[], int first, int second) { String hold; //temporary area for swap hold = swapArray[first]; swapArray[first] = swapArray[second]; swapArray[second] = hold; }
public void search(String searchField, String searchArray[]) { try { Document doc = textPane.getDocument(); //assign text to document object doc.remove(0, doc.getLength()); //clear previous text
//prompt user for search data String search = JOptionPane.showInputDialog(null, "Please enter the " + searchField); boolean found = false;
//search arrays for (int i = 0; i < artistName.length; i++) { if (search.compareTo(searchArray[i]) == 0) { doc.insertString(doc.getLength(), artistName[i] + "\t", textPane.getStyle ("bold")); doc.insertString(doc.getLength(), genre[i] + "\t", textPane.getStyle("italic")); doc.insertString(doc.getLength(), greatestHit[i] + "\n", textPane.getStyle("regular")); doc.insertString(doc.getLength(), recordLabel[i] + "\n", textPane.getStyle("regular")); found = true; } } if (found == false) { JOptionPane.showMessageDialog(null, "Your search produced no results.", "no results found", JOptionPane.INFORMATION_MESSAGE); sort(artistName); } } catch (BadLocationException ble) { System.err.println("Couldn't insert text."); } }
//main method executes at run time public static void main(String arg[]) { JFrame.setDefaultLookAndFeelDecorated(true); FavMusic f = new FavMusic(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setJMenuBar(f.createMenuBar()); f.setContentPane(f.createContentPane()); f.setSize(600, 275); f.setVisible(true); } }
ok, i misunderstood, i have run the code, and had to fix some errors, in fact it will not even compile, especially since: switch(e.getSelectedIndex()) this is not possible, as the method getSelectedIndex() applies to a listbox or jList, not an ActionEvent. You will need to change how this switch statement is determining the sort items, perhaps add them to an array, or some other method. Or make the edit menu global, using the getSubElements() method which will return an array of the sub elements
//construct instance of FavMusic public FavMusic() { super("Favorite Music"); }
//create the menu system public JMenuBar createMenuBar() { //create an instance of the the menu JMenuBar mnuBar = new JMenuBar(); setJMenuBar(mnuBar);
//construct and populate the file menu JMenu mnuFile = new JMenu("File", true); mnuFile.setMnemonic(KeyEvent.VK_F); mnuFile.setDisplayedMnemonicIndex(0); mnuBar.add(mnuFile);
JMenuItem mnuFileExit = new JMenuItem("Exit"); mnuFileExit.setMnemonic(KeyEvent.VK_X); mnuFileExit.setDisplayedMnemonicIndex(1); mnuFile.add(mnuFileExit); mnuFileExit.setActionCommand("Exit"); mnuFileExit.addActionListener(this);
//construct and populate the Edit menu JMenu mnuEdit = new JMenu("Edit", true); mnuEdit.setMnemonic(KeyEvent.VK_E); mnuEdit.setDisplayedMnemonicIndex(0); mnuBar.add(mnuEdit);
JMenuItem mnuEditInsert = new JMenuItem("Insert New Music"); mnuEditInsert.setMnemonic(KeyEvent.VK_I); mnuEditInsert.setDisplayedMnemonicIndex(0); mnuEdit.add(mnuEditInsert); mnuEditInsert.setActionCommand("Insert"); mnuEditInsert.addActionListener(this);
JMenu mnuEditSort = new JMenu("Sort"); mnuEditSort.setMnemonic(KeyEvent.VK_R); mnuEditSort.setDisplayedMnemonicIndex(3); mnuEdit.add(mnuEditSort);
JMenuItem mnuEditSortByrecordLabel = new JMenuItem("by Record Label"); mnuEditSortByrecordLabel.setMnemonic(KeyEvent.VK_T); mnuEditSortByrecordLabel.setDisplayedMnemonicIndex(3); mnuEditSort.add(mnuEditSortByrecordLabel); mnuEditSortByrecordLabel.setActionCommand("recordLabel"); mnuEditSortByrecordLabel.addActionListener(this);
return mnuBar; }
//create the content pane public Container createContentPane() { //create the JTextPane and center panel JPanel centerPanel = new JPanel(); setTabsAndStyles(textPane); textPane = addTextToTextPane(); JScrollPane scrollPane = new JScrollPane(textPane); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setPreferredSize(new Dimension(500, 200)); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setPreferredSize(new Dimension(500, 200)); centerPanel.add(scrollPane);
//create container and set attributes Container c = getContentPane(); c.setLayout(new BorderLayout(1,5)); //c.add(northPanel, BorderLayout.NORTH); c.add(centerPanel, BorderLayout.CENTER);
return c; }
//method to create tabe stops and set font style &n