Good Day All, I have another question: I have done this program to sort a list of artist and their biggest hits, etc. It has 4 parallel arrays that i have to sort alphabetically using the Sort Option in the menu bar. I am using Bubble sort, to sort the four arrays. I have done a similar sort in another program, with the difference that the sort was located in a JComboBox, then it worked perfectly. But now that the Sort option is in the menubar, i cannot seem to get my Bubble Sort to work. Can anyone please give me some ideas? Is the bubble sort not supposed to work irrespective of if you use it with a JComboBox or a menu item? Or perhaps I just don't understand Bubble Sort. Any other suggestions to sort parallel String Arrays would also be welcomed. Thank You.
I am attaching the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import javax.swing.*;
public class MusicArtists extends JFrame implements ActionListener
{
//Initialize data in arrays
String artist[] = {"Colbie Caillat", "Sara Bareilles", "Meatloaf", "Alannis Morissette"};
String genre[] = {"Pop", "Pop", "Rock", "Rock"};
String hit[] = {"Bubbly", "Love Song", "Bat out of Hell", "Ironic"};
String label[] = {"PCM Audio", "Epic", "Mercury", "RPG"};
//Construct components
JTextPane textPane = new JTextPane();
//Construct instance of Favorite Music Artists
public MusicArtists()
{
super("Favorite Music Artists");
}
//Creating menu system
public JMenuBar createMenuBar()
{
//Create instance of menu
JMenuBar mnuBar = new JMenuBar();
setJMenuBar(mnuBar);
//Construct and populate 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 Data");
mnuEditInsert.setMnemonic(KeyEvent.VK_I);
mnuEdit.setDisplayedMnemonicIndex(0);
mnuEdit.add(mnuEditInsert);
mnuEditInsert.setActionCommand("Insert");
mnuEditInsert.addActionListener(this);
JMenu mnuSortBy = new JMenu("Sort by", true);
mnuSortBy.setMnemonic(KeyEvent.VK_S);
mnuSortBy.setDisplayedMnemonicIndex(0);
mnuBar.add(mnuSortBy);
JMenuItem mnuSortByArtist = new JMenuItem("Artist");
mnuSortByArtist.setMnemonic(KeyEvent.VK_A);
mnuSortByArtist.setDisplayedMnemonicIndex(0);
mnuSortBy.add(mnuSortByArtist);
mnuSortBy.setActionCommand("Artist");
mnuSortBy.addActionListener(this);
JMenuItem mnuSortByGenre = new JMenuItem("Genre");
mnuSortByGenre.setMnemonic(KeyEvent.VK_G);
mnuSortByGenre.setDisplayedMnemonicIndex(0);
mnuSortBy.add(mnuSortByGenre);
mnuSortBy.setActionCommand("Genre");
mnuSortBy.addActionListener(this);
JMenuItem mnuSortByGreatestHit = new JMenuItem("Greatest Hit");
mnuSortByGreatestHit.setMnemonic(KeyEvent.VK_H);
mnuSortByGreatestHit.setDisplayedMnemonicIndex(9);
mnuSortBy.add(mnuSortByGreatestHit);
mnuSortBy.setActionCommand("Hit");
mnuSortBy.addActionListener(this);
JMenuItem mnuSortByRecordLabel = new JMenuItem("Record Label");
mnuSortByRecordLabel.setMnemonic(KeyEvent.VK_L);
mnuSortByRecordLabel.setDisplayedMnemonicIndex(7);
mnuSortBy.add(mnuSortByRecordLabel);
mnuSortBy.setActionCommand("Label");
mnuSortBy.addActionListener(this);
return mnuBar;
}
//Create content pane
public Container createContentPane()
{
//Create JTextPane & center panel
JPanel centerPanel = new JPanel();
setTabsAndStyles(textPane);
textPane = addTextToTextPane();
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(550,200));
centerPanel.add(scrollPane);
//Create Container and set attributes
Container c = getContentPane();
c.setLayout(new BorderLayout(10,10));
c.add(centerPanel, BorderLayout.CENTER);
return c;
}
//method to create tab stops and set font styles
protected void setTabsAndStyles(JTextPane textPane)
{
//create Tab Stops
TabStop[] tabs = new TabStop[3];
tabs[0] = new TabStop(150, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
tabs[1] = new TabStop(250, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
tabs[2] = 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, "Arial");
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, 12);
}
//Add text to JTextPane
public JTextPane addTextToTextPane()
{
Document doc = textPane.getDocument();
try
{
//clear previous text
doc.remove(0, doc.getLength());
//insert Artist
doc.insertString(0, "ARTIST\tGENRE\tGREATEST HIT\tRECORD LABEL\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(), genre[j] + "\t", textPane.getStyle("italic"));
doc.insertString(doc.getLength(), hit[j] + "\t", textPane.getStyle("regular"));
doc.insertString(doc.getLength(), label[j] + "\n", textPane.getStyle("regular"));
}
}
catch (BadLocationException ble)
{
System.err.println("Couldn't insert text.");
}
return textPane;
}
//Event to process user clicks
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
//user clicks Insert new data on the Edit menu
if (arg == "Insert")
{
//accept new data
String newArtist = JOptionPane.showInputDialog(null, "Please enter the Artist name.");
String newGenre = JOptionPane.showInputDialog(null, "Please enter the Genre");
String newHit = JOptionPane.showInputDialog(null, "Please enter " + newArtist + "'s Greatest Hit.");
String newLabel = JOptionPane.showInputDialog(null, "Please enter the Record Label.");
//enlarge arrays
artist = enlargeArray(artist);
genre = enlargeArray(genre);
hit = enlargeArray(hit);
label = enlargeArray(label);
//add new data to arrays
artist[artist.length-1] = newArtist;
genre[genre.length-1] = newGenre;
hit[hit.length-1] = newHit;
label[label.length-1] = newLabel;
//call sort method
sort(artist);
}
// user clicks Artist on Sort By menu
if (arg.equals("Artist"))
sort(artist);
// user clicks Genre on Sort By menu
if (arg.equals("Genre"))
sort(genre);
// user clicks Record Label on Sort By menu
if (arg.equals("Record Label"))
sort(label);
// user clicks Greatest Hit on Sort By menu
if (arg.equals("Greatest Hit"))
sort(hit);
}
//Method to enlarge 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 array
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(genre, element, element + 1);
swap(hit, element, element + 1);
swap(label, element, element + 1);
}
}
addTextToTextPane();
}
//Method to swap two element of an array
public void swap(String swapArray[], int first, int second)
{
String hold; //temporary holding area for swap
hold = swapArray[first];
swapArray[first] = swapArray[second];
swapArray[second] = hold;
}
//Main method execute at run time
public static void main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
MusicArtists f = new MusicArtists();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setJMenuBar(f.createMenuBar());
f.setContentPane(f.createContentPane());
f.setSize(600, 275);
f.setVisible(true);
}
}