Ok i have a Swing app that I'd like to be able to sort my arrays. I have built the sort method I just need the condition trigger to work. For some reason I can't figure out how to get the sort by JMenuBar items to trigger it. As you can see in my code I have made attempts to get it to work to but can't seem to make it happen. The switch and if statements are commented out so the app will run. Now that everything else is working I would like to make the sort by feature work.Thanks in advance Guys!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class YourMusic extends JFrame implements ActionListener
{
//construct components
JTextPane textPane = new JTextPane();
//initalize data in arrays
String artist[]={"Eminem", "Britney S", "Dre" };
String album[]={"Eminem Show", "Her Album", "Cronic" };
String genre[]={"Rap", "Pop", "Hip-hop" };
String hit[]={"Lose Yourself", "Toxic", "D.R.E" };
String lable[]={"Interscope", "Britney Bitch", "Aftermath" };
//construct an instance of YourMusic
public YourMusic()
{
super("Your Music");
}
//create the menu system
public JMenuBar createMenuBar()
{
///create instance of menu bar
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_E);
mnuFileExit.setDisplayedMnemonicIndex(1);
//mnuBar.add(mnuFileExit);
mnuFile.add(mnuFileExit);
mnuFileExit.setActionCommand("Exit");
mnuFileExit.addActionListener(this);
//construct and pop 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 Song");
mnuEditInsert.setMnemonic(KeyEvent.VK_I);
mnuEditInsert.setDisplayedMnemonicIndex(1);
mnuEdit.add(mnuEditInsert);
mnuEditInsert.setActionCommand("insert");
mnuEditInsert.addActionListener(this);
//sort menu
JMenu mnuSort = new JMenu("Sort",true);
mnuSort.setMnemonic(KeyEvent.VK_S);
mnuSort.setDisplayedMnemonicIndex(0);
mnuBar.add(mnuSort);
//These are the items id like to sort by
JMenuItem mnuSortArtist =new JMenuItem("by Artist");
mnuSort.setMnemonic(KeyEvent.VK_E);
mnuSort.setDisplayedMnemonicIndex(1);
mnuSort.add(mnuSortArtist);
mnuSort.setActionCommand("artist");
mnuSort.addActionListener(this);
JMenuItem mnuSortAlbum =new JMenuItem("by album");
mnuSort.setMnemonic(KeyEvent.VK_M);
mnuSort.setDisplayedMnemonicIndex(1);
mnuSort.add(mnuSortAlbum);
mnuSort.setActionCommand("album");
mnuSort.addActionListener(this);
JMenuItem mnuSortGenre = new JMenuItem("by Genre");
mnuSort.setMnemonic(KeyEvent.VK_G);
mnuSort.setDisplayedMnemonicIndex(1);
mnuSort.add(mnuSortGenre);
mnuSort.setActionCommand("genre");
mnuSort.addActionListener(this);
JMenuItem mnuSortHit =new JMenuItem("by Hit");
mnuSort.setMnemonic(KeyEvent.VK_H);
mnuSort.setDisplayedMnemonicIndex(1);
mnuSort.add(mnuSortHit);
mnuSort.setActionCommand("hit");
mnuSort.addActionListener(this);
JMenuItem mnuSortLable =new JMenuItem("by Lable");
mnuSort.setMnemonic(KeyEvent.VK_L);
mnuSort.setDisplayedMnemonicIndex(1);
mnuSort.add(mnuSortLable);
mnuSort.setActionCommand("lable");
mnuSort.addActionListener(this);
return mnuBar;
}//End menu contructor
//Create the conetnt pane
public Container createContentPane()
{
//construct and populate the north panel
JPanel northPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
//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.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(600, 300));
centerPanel.add(scrollPane);
//create container and set attributes
Container c = getContentPane();
c.setLayout(new BorderLayout(10,10));
c.add(northPanel,BorderLayout.NORTH);
c.add(centerPanel,BorderLayout.CENTER);
return c;
}//end create container method
//method to create the tab stops and set fontstyles
protected void setTabsAndStyles(JTextPane textPane)
{
//create Tab Stops
TabStop[] tabs = new TabStop[4];
tabs[0] = new TabStop(100, TabStop.ALIGN_LEFT,TabStop.LEAD_NONE);
tabs[1] = new TabStop(200, TabStop.ALIGN_LEFT,TabStop.LEAD_NONE);
tabs[2] = new TabStop(300, TabStop.ALIGN_LEFT,TabStop.LEAD_NONE);
tabs[3] = new TabStop(400, 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("blod",regular);
StyleConstants.setBold(s,true);
s = textPane .addStyle("large",regular);
StyleConstants.setFontSize(s,16);
}//method to ad text to textpane
public JTextPane addTextToTextPane()
{
Document doc = textPane.getDocument();
try
{
//clear previous text
doc.remove(0, doc.getLength());
//Insert title
doc.insertString(0,"Artist\tAlbum\tGernre\tGreatest Hit\tRecord Lable\n",textPane.getStyle("large"));
//insert detail
for(int j=0;j<artist.length;j++)
{
doc.insertString(doc.getLength(),artist[j] + "\t",textPane.getStyle("bold"));
doc.insertString(doc.getLength(),album[j] + "\t",textPane.getStyle("bold"));
doc.insertString(doc.getLength(),genre[j] + "\t",textPane.getStyle("bold"));
doc.insertString(doc.getLength(),hit[j] + "\t",textPane.getStyle("italic"));
doc.insertString(doc.getLength(),lable[j] + "\n",textPane.getStyle("regular"));
}//end loop
} //end try
catch (BadLocationException ble)
{
System.err.println("Couldnlt Insert Text");
}//end catch
return textPane;
}///end addtexttotextpane method
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
//user clicks the sort
// if(e.getSource() == artist)
// {
/*/ switch(mnubar.getSelectedIndex())
{
case 0:
sort(artist);
break;
case 1:
sort(album);
break;
case 2:
sort(genre);
break;
case 3:
sort(hit);
break;
case 4:
sort(lable);
break;
/*/// }//end swictch
// } //end if
//user clicks exit on file menu
if (arg.equals("Exit"))
System.exit(0);
//user clicks insert new dvd on edit menu
if (arg.equals("insert"))
{
String newArtist = JOptionPane.showInputDialog(null, "Please enter the Artist");
String newAlbum = JOptionPane.showInputDialog(null, "Please enter the Album for" + newArtist);
String newGenre = JOptionPane.showInputDialog(null, "Please enter the Genre for" + newArtist);
String newHit = JOptionPane.showInputDialog(null, "Please enter the Hit for" + newArtist);
String newLable = JOptionPane.showInputDialog(null, "Please enter the Record Lable for " + newArtist);
//Enlarge arrays
artist = enlargeArray(artist);
album = enlargeArray(album);
genre = enlargeArray(genre);
hit = enlargeArray(hit);
lable = enlargeArray(lable);
//add to arrys
artist[artist.length-1] = newArtist;
album[album.length-1] = newAlbum;
genre[genre.length-1] = newGenre;
hit[hit.length-1] = newHit;
lable[lable.length-1] = newLable;
//call to sort method
sort(artist);
// mnuSort.setSelectedIndex(0);
}//end if
}//end action meth
//Method to enlarge an arry 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;
}//end enlarge arry method
//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(artist,element,element+1);
swap(album,element, element+1);
swap(genre,element, element+1);
swap(hit,element, element+1);
swap(lable,element, element+1);
}//end if
}//end of loop
addTextToTextPane();
}//end of sort method
//method to swap two elements of an array
public void swap(String swapArray[], int first, int second)
{
String hold;//temp area to hold for swap
hold = swapArray[first];
swapArray[first] = swapArray[second];
swapArray[second] = hold;
} //end swap method
public static void main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
YourMusic f =new YourMusic();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setJMenuBar(f.createMenuBar());
f.setContentPane(f.createContentPane());
f.setSize(800,700);
f.setVisible(true);
}//end main method
}//End class