I've been working on this project for about a week. It uses a comparator to sort a string of movies by Title, Date, or Company. The problem I am having now is how to get the sorted array to display back into the JTextPane.
I have tried setText(), repaint() and print(), all of which throw some error such as: String[][] cannot be converted to String or String[][] cannot be converted to long. if I run the program, and choose one of the sorts (sortByDate, sortByTitle, etc.) the program outputs a Windows batch file into the file where the codes are stored. I can open these batch files and see the sorted arrays, but I cannot seem to get them to display back into the JTextPane.
Any help in figuring out how to fix this would be greatly appreciated.
Here's my code:
import java.util.Arrays;
import java.util.Comparator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
class MenuExample extends Frame implements ActionListener
{
MenuBar mbar;
Menu menu, submenu;
MenuItem m1, m2, m3, m4, m5;
String[][] data = new String[][]
{
new String[] { "Casablanca", "Warner Brothers", "1942" },
new String[] { "Citizen Kane", "RKO Pictures", "1941" },
new String[] { "Singin' in the Rain", "MGM", "1952" },
new String[] { "the Wizard of Oz", "MGM", "1930" }
};
public MenuExample()
{
setTitle("AWT Menu");
setSize(800, 500);
setLayout(new FlowLayout());
setVisible(true);
setLocationRelativeTo(null);
mbar = new MenuBar();
menu = new Menu("Menu");
submenu = new Menu("Sub Menu");
m1 = new MenuItem("Sort by Title");
m1.setActionCommand("Title");
m1.addActionListener(this);
m2 = new MenuItem("Sort by Date");
m2.setActionCommand("Date");
m2.addActionListener(this);
m3 = new MenuItem("Sort by Company");
m3.setActionCommand("Company");
m3.addActionListener(this);
m4 = new MenuItem("Menu Item 4");
m5 = new MenuItem("Menu Item 5");
menu.add(m1);
menu.add(m2);
menu.add(m3);
submenu.add(m4);
submenu.add(m5);
menu.add(submenu);
mbar.add(menu);
setMenuBar(mbar);
JPanel textPanel = new JPanel();
JTextPane textArea = new JTextPane();
Dimension textAreaDimensions = new Dimension(500, 300);
textArea.setPreferredSize(textAreaDimensions);
JScrollPane scroll = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
Document doc = textArea.getDocument();
try
{
for(int i = 0; i < 4; i ++)
{
for(int j = 0; j < 3; j ++)
doc.insertString(doc.getLength(), data[i][j] + "\t", textArea.getStyle("bold"));
doc.insertString(doc.getLength(), "\n", textArea.getStyle("bold"));
}
}
catch (Exception e) {}
textPanel.add(scroll);
add(textPanel);
validate();
repaint();
}
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
if(arg.equals("Title"))
{
Arrays.sort(data, new sortByTitle());
}
if(arg.equals("Date"))
{
Arrays.sort(data, new sortByDate());
}
if(arg.equals("Company"))
{
Arrays.sort(data, new sortByCompany());
}
}
public static void main(String args[])
{
new MenuExample();
}
}
The methods sortByTitle, sortByDate, and sortByCompany are external methods and are written as such:
import java.util.Arrays;
import java.util.Comparator;
public class sortByTitle implements Comparator<String[]>
{
public int compare(final String[] entry1, final String[] entry2)
{
final String field1 = entry1[0];
final String field2 = entry2[0];
return field1.compareTo(field2);
}
}