Well, as suggested by other's answers here is what you should aim for (there are a LOT of things worth improving):
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import javax.swing.*;
import javax.swing.text.*;
public class YourMusic extends JFrame implements ActionListener {
//construct components
JTextPane textPane = new JTextPane();
//initalize data in arrays
ArrayList<SongData> songs = new ArrayList<SongData> ();
//construct an instance of YourMusic
public YourMusic() {
super("Your Music");
songs.add(new SongData("Eminem", "Eminem Show", "Rap", "Lose Yourself", "Interscope"));
songs.add(new SongData("Britney S", "Her Album", "Pop", "Toxic", "Britney Bith"));
songs.add(new SongData("Dre", "Cronic", "Hip-hop", "D.R.E", "Aftermath"));
}
//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");
mnuSortArtist.setMnemonic(KeyEvent.VK_E);
mnuSortArtist.setDisplayedMnemonicIndex(1);
mnuSort.add(mnuSortArtist);
mnuSortArtist.setActionCommand("artist");
mnuSortArtist.addActionListener(this);
JMenuItem mnuSortAlbum =new JMenuItem("by album");
mnuSortAlbum.setMnemonic(KeyEvent.VK_M);
mnuSortAlbum.setDisplayedMnemonicIndex(1);
mnuSort.add(mnuSortAlbum);
mnuSortAlbum.setActionCommand("album");
mnuSortAlbum.addActionListener(this);
JMenuItem mnuSortGenre = new JMenuItem("by Genre");
mnuSortGenre.setMnemonic(KeyEvent.VK_G);
mnuSortGenre.setDisplayedMnemonicIndex(1);
mnuSort.add(mnuSortGenre);
mnuSortGenre.setActionCommand("genre");
mnuSortGenre.addActionListener(this);
JMenuItem mnuSortHit =new JMenuItem("by Hit");
mnuSortHit.setMnemonic(KeyEvent.VK_H);
mnuSortHit.setDisplayedMnemonicIndex(1);
mnuSort.add(mnuSortHit);
mnuSortHit.setActionCommand("hit");
mnuSortHit.addActionListener(this);
JMenuItem mnuSortLable =new JMenuItem("by Lable");
mnuSortLable.setMnemonic(KeyEvent.VK_L);
mnuSortLable.setDisplayedMnemonicIndex(1);
mnuSort.add(mnuSortLable);
mnuSortLable.setActionCommand("lable");
mnuSortLable.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<songs.size();j++)
{
doc.insertString(doc.getLength(),songs.get(j).getArtist() + "\t",textPane.getStyle("bold"));
doc.insertString(doc.getLength(),songs.get(j).getAlbum() + "\t",textPane.getStyle("bold"));
doc.insertString(doc.getLength(),songs.get(j).getGenre() + "\t",textPane.getStyle("bold"));
doc.insertString(doc.getLength(),songs.get(j).getHit() + "\t",textPane.getStyle("italic"));
doc.insertString(doc.getLength(),songs.get(j).getLable() + "\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();
System.out.println(arg);
if (arg.equals("artist") || arg.equals("album") || arg.equals("genre") || arg.equals("hit") || arg.equals("lable")) {
this.sort(arg);
}
//user clicks exit on file menu
else if (arg.equals("Exit"))
System.exit(0);
//user clicks insert new dvd on edit menu
else 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
songs.add(new SongData(newArtist, newAlbum, newGenre, newHit, newLable));
}//end if
}//end action meth
//method to sort arrays
public void sort(final String criteria) {
Collections.sort(songs, new Comparator<SongData> () {
@Override
public int compare(SongData song1, SongData song2) {
switch (criteria) {
case "artist": return song1.getArtist().compareTo(song2.getArtist());
case "album": return song1.getAlbum().compareTo(song2.getAlbum());
case "genre": return song1.getGenre().compareTo(song2.getGenre());
case "hit": return song1.getHit().compareTo(song2.getHit());
case "lable": return song1.getLable().compareTo(song2.getLable());
default: return 0;
}
}
});
addTextToTextPane();
}//end of sort 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
Somewhat better, now, as far as SongData goes, I've kept it simple (getters and setters generated by eclipse and nothing else):
public class SongData {
private String artist, album, genre, hit, lable;
public SongData(String artist, String album, String genre, String hit, String lable) {
setArtist(artist);
setAlbum(album);
setGenre(genre);
setHit(hit);
setLable(lable);
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public String getHit() {
return hit;
}
public void setHit(String hit) {
this.hit = hit;
}
public String getLable() {
return lable;
}
public void setLable(String lable) {
this.lable = lable;
}
public String toString() {
String data = "";
data = getArtist() + ", " + getAlbum() + ", " + getGenre() + ", " + getHit() + ", " + getLable();
return data;
}
}
This works, but again, a LOT of fixes should be made, regarding null values, switch cases... But instead of dealing with multiple arrays you now have a list with a special object that's fairly easy to sort.