Welcome to Dream.In.Code
Become a Java Expert!

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!




using swing to sort music

 
Reply to this topicStart new topic

using swing to sort music

dbrine
8 May, 2007 - 04:17 PM
Post #1

New D.I.C Head
*

Joined: 25 Apr, 2007
Posts: 48


My Contributions
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



CODE

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class FavMusic extends JFrame implements ActionListener
{
    //construct components
    JTextPane textPane = new JTextPane();

    //initialize data in arrays
    String artistName[] = {"Metallica", "Guns N Roses", "Dr Dre", "Soul Asylum"};
    String genre[] = {"Heavy Metal", "Heavy Metal", "Rap", "Rock"};
    String greatestHit[] = {"One", "Don't Cry", "Chronic", "Run Away Train"};
    String recordLabel[] = {"Def Jams", "Sony", "DeathRow","Artista"};

    //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 mnuEditSortByartistName = new JMenuItem("by Artist");
            mnuEditSortByartistName.setMnemonic(KeyEvent.VK_T);
            mnuEditSortByartistName.setDisplayedMnemonicIndex(3);
            mnuEditSort.add(mnuEditSortByartistName);
            mnuEditSortByartistName.setActionCommand("artistName");
            mnuEditSortByartistName.addActionListener(this);

        JMenuItem mnuEditSortByGenre =  new JMenuItem("by Genre");
            mnuEditSortByGenre.setMnemonic(KeyEvent.VK_S);
            mnuEditSortByGenre.setDisplayedMnemonicIndex(3);
            mnuEditSort.add(mnuEditSortByGenre);
            mnuEditSortByGenre.setActionCommand("genre");
            mnuEditSortByGenre.addActionListener(this);

        JMenuItem mnuEditSortBygreatestHit = new JMenuItem("by Greatest Hit");
            mnuEditSortBygreatestHit.setMnemonic(KeyEvent.VK_Y);
            mnuEditSortBygreatestHit.setDisplayedMnemonicIndex(3);
            mnuEditSort.add(mnuEditSortBygreatestHit);
            mnuEditSortBygreatestHit.setActionCommand("greatestHit");
            mnuEditSortBygreatestHit.addActionListener(this);

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

            //set Tab Style
            StyleContext tabStyle = StyleContext.getDefaultStyleContext();
            AttributeSet aset =
                tabStyle.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabset);
            textPane.setParagraphAttributes(aset, false);

            //set font style
            Style fontStyle =
                StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

            Style regular = textPane.addStyle("regular", fontStyle);
            StyleConstants.setFontFamily(fontStyle, "SansSerif");

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

                //insert artistName
                doc.insertString(0, "Artist\tGenre\tGreatest Hit\tRecord Label\n", textPane.getStyle("large"));

                //insert detail
                for (int j = 0; j < artistName.length; j++)
                {
                    doc.insertString(doc.getLength(), artistName[j] + "\t",
                    textPane.getStyle("bold"));
                    doc.insertString(doc.getLength(), genre[j] + "\t", textPane.getStyle("italic"));
                    doc.insertString(doc.getLength(), greatestHit[j] + "\t", textPane.getStyle("regular"));
                    doc.insertString(doc.getLength(), recordLabel[j] + "\t", textPane.getStyle("regular"));
                }
            }
            catch (BadLocationException ble)
            {
                System.err.println("Couldn't insert text.");
            }
            return textPane;
        }
2
        //event to process user clicks
                public void actionPerformed(ActionEvent e)
                {
                    String arg = e.getActionCommand();

                    //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);

                        //enlarge array
                        artistName = enlargeArray(artistName);
                        genre = enlargeArray(genre);
                        greatestHit = enlargeArray(greatestHit);
                        recordLabel = enlargeArray(recordLabel);

                        //add new data to arrays
                        artistName[artistName.length-1] = newartistName;
                        genre[genre.length-1] = newGenre;
                        greatestHit[greatestHit.length-1] = newgreatestHit;
                        recordLabel[recordLabel.length-1] = newrecordLabel;

                        //call sort method
                        sort(artistName);
                        //fieldCombo.setSelectedIndex(0);
                    }

                    //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

                //display column artistName
                doc.insertString(0,"Artist\tGenre\tGreatest Hit\n", textPane.getStyle("large"));

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











User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Using Swing To Sort Music
8 May, 2007 - 05:16 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
you need to add the JMenuItems to the edit list, before adding the edit JMenu to the MenuBar.
User is offlineProfile CardPM
+Quote Post

dbrine
RE: Using Swing To Sort Music
8 May, 2007 - 05:21 PM
Post #3

New D.I.C Head
*

Joined: 25 Apr, 2007
Posts: 48


My Contributions
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



CODE

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class FavMusic extends JFrame implements ActionListener
{
    //construct components
    JTextPane textPane = new JTextPane();

    //initialize data in arrays
    String artistName[] = {"Metallica", "Guns N Roses", "Dr Dre", "Soul Asylum"};
    String genre[] = {"Heavy Metal", "Heavy Metal", "Rap", "Rock"};
    String greatestHit[] = {"One", "Don't Cry", "Chronic", "Run Away Train"};
    String recordLabel[] = {"Def Jams", "Sony", "DeathRow","Artista"};

    //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 mnuEditSortByartistName = new JMenuItem("by Artist");
            mnuEditSortByartistName.setMnemonic(KeyEvent.VK_T);
            mnuEditSortByartistName.setDisplayedMnemonicIndex(3);
            mnuEditSort.add(mnuEditSortByartistName);
            mnuEditSortByartistName.setActionCommand("artistName");
            mnuEditSortByartistName.addActionListener(this);

        JMenuItem mnuEditSortByGenre =  new JMenuItem("by Genre");
            mnuEditSortByGenre.setMnemonic(KeyEvent.VK_S);
            mnuEditSortByGenre.setDisplayedMnemonicIndex(3);
            mnuEditSort.add(mnuEditSortByGenre);
            mnuEditSortByGenre.setActionCommand("genre");
            mnuEditSortByGenre.addActionListener(this);

        JMenuItem mnuEditSortBygreatestHit = new JMenuItem("by Greatest Hit");
            mnuEditSortBygreatestHit.setMnemonic(KeyEvent.VK_Y);
            mnuEditSortBygreatestHit.setDisplayedMnemonicIndex(3);
            mnuEditSort.add(mnuEditSortBygreatestHit);
            mnuEditSortBygreatestHit.setActionCommand("greatestHit");
            mnuEditSortBygreatestHit.addActionListener(this);

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

            //set Tab Style
            StyleContext tabStyle = StyleContext.getDefaultStyleContext();
            AttributeSet aset =
                tabStyle.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabset);
            textPane.setParagraphAttributes(aset, false);

            //set font style
            Style fontStyle =
                StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

            Style regular = textPane.addStyle("regular", fontStyle);
            StyleConstants.setFontFamily(fontStyle, "SansSerif");

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

                //insert artistName
                doc.insertString(0, "Artist\tGenre\tGreatest Hit\tRecord Label\n", textPane.getStyle("large"));

                //insert detail
                for (int j = 0; j < artistName.length; j++)
                {
                    doc.insertString(doc.getLength(), artistName[j] + "\t",
                    textPane.getStyle("bold"));
                    doc.insertString(doc.getLength(), genre[j] + "\t", textPane.getStyle("italic"));
                    doc.insertString(doc.getLength(), greatestHit[j] + "\t", textPane.getStyle("regular"));
                    doc.insertString(doc.getLength(), recordLabel[j] + "\t", textPane.getStyle("regular"));
                }
            }
            catch (BadLocationException ble)
            {
                System.err.println("Couldn't insert text.");
            }
            return textPane;
        }
2
        //event to process user clicks
                public void actionPerformed(ActionEvent e)
                {
                    String arg = e.getActionCommand();

                    //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);

                        //enlarge array
                        artistName = enlargeArray(artistName);
                        genre = enlargeArray(genre);
                        greatestHit = enlargeArray(greatestHit);
                        recordLabel = enlargeArray(recordLabel);

                        //add new data to arrays
                        artistName[artistName.length-1] = newartistName;
                        genre[genre.length-1] = newGenre;
                        greatestHit[greatestHit.length-1] = newgreatestHit;
                        recordLabel[recordLabel.length-1] = newrecordLabel;

                        //call sort method
                        sort(artistName);
                        //fieldCombo.setSelectedIndex(0);
                    }

                    //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

                //display column artistName
                doc.insertString(0,"Artist\tGenre\tGreatest Hit\n", textPane.getStyle("large"));

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



User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Using Swing To Sort Music
8 May, 2007 - 05:40 PM
Post #4

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
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
User is offlineProfile CardPM
+Quote Post

dbrine
RE: Using Swing To Sort Music
16 May, 2007 - 02:33 PM
Post #5

New D.I.C Head
*

Joined: 25 Apr, 2007
Posts: 48


My Contributions
This is what I have now. However, I'm getting sort lang erros when I try to compile it???

CODE

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class FavMusic extends JFrame implements ActionListener
{
    //construct components
    JTextPane textPane = new JTextPane();

    //initialize data in arrays
    String artistName[] = {"Metallica", "Guns N Roses", "Dr Dre", "Soul Asylum"};
    String genre[] = {"Heavy Metal", "Heavy Metal", "Rap", "Rock"};
    String greatestHit[] = {"One", "Don't Cry", "Chronic", "Run Away Train"};
    String recordLabel[] = {"Def Jams", "Sony", "DeathRow","Artista"};

    //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 mnuEditSortByartistName = new JMenuItem("by Artist");
            mnuEditSortByartistName.setMnemonic(KeyEvent.VK_T);
            mnuEditSortByartistName.setDisplayedMnemonicIndex(3);
            mnuEditSort.add(mnuEditSortByartistName);
            mnuEditSortByartistName.setActionCommand("artistName");
            mnuEditSortByartistName.addActionListener(this);

        JMenuItem mnuEditSortByGenre =  new JMenuItem("by Genre");
            mnuEditSortByGenre.setMnemonic(KeyEvent.VK_S);
            mnuEditSortByGenre.setDisplayedMnemonicIndex(3);
            mnuEditSort.add(mnuEditSortByGenre);
            mnuEditSortByGenre.setActionCommand("genre");
            mnuEditSortByGenre.addActionListener(this);

        JMenuItem mnuEditSortBygreatestHit = new JMenuItem("by Greatest Hit");
            mnuEditSortBygreatestHit.setMnemonic(KeyEvent.VK_Y);
            mnuEditSortBygreatestHit.setDisplayedMnemonicIndex(3);
            mnuEditSort.add(mnuEditSortBygreatestHit);
            mnuEditSortBygreatestHit.setActionCommand("greatestHit");
            mnuEditSortBygreatestHit.addActionListener(this);

        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