Вот простое решение для начала, в форме псевдокода, который компилируется и запускается, но не читает ни один файл. Надеюсь, что OOPuritans позволит вам использовать это. Перейдя этот порог, вы поймете, как использовать JTable, установить selectionModels в JList, использовать парадигму invokeLater и т. Д. Добрые пожелания, - M.S.
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Crucible extends JPanel implements ActionListener {
private JTextField tf = new JTextField();
private JList list = new JList (new String[] {"Theme", "Character Analysis",
"Significance", "Plot Enhancement", "Speaker"});
private final static String helpfulText =
"This is the GUI version of my aplication that analyzes\n" +
"the quotations received on crucibles and swings. ....",
bNames[] = {"Quit", "Run", "Help"};
public Crucible () {
super (new BorderLayout());
JPanel bp = new JPanel (new GridLayout (1,bNames.length));
for (int i = 0 ; i < bNames.length ; i++) {
JButton b = new JButton (bNames[i]);
b.addActionListener (this);
bp.add (b);
}
add (new JScrollPane (list), "Center");
add (bp, "South");
add (tf, "North");
}
private String getQuotes () {
int quoteSelection,
analysisSelection = list.getSelectedIndex(),
howMany = getLineCount ("CrucibleQuotations.txt");
if (analysisSelection < 0) {
JOptionPane.showMessageDialog (null, "Please select type of analysys",
"Crucible Quotes", JOptionPane.ERROR_MESSAGE);
return "";
}
// Create array based on how many quotes available in the text file
ListModel lm = list.getModel();
String[][] quotes = new String [howMany][1+lm.getSize()];
// Buffered Reader ...
// Populate the array and close the files
// catch IO, FileNotFound, etc Exceptions to return ""
// Some dummy data for now:
for (int i = 0 ; i < howMany ; i++) {
quotes[i][0] = "Quote [" + i + "]";
for (int j = 0 ; j < lm.getSize() ; j++)
quotes[i][j+1] = "(" + (String) lm.getElementAt (j) + ")";
}
// Next Step:
// Display the array with JTable in a JScrollPane,
// get quoteSelection as the selectedRow of JTable
// For now, ask for a quote index in a dialog
String qStr = JOptionPane.showInputDialog ("Please select quote index");
if (qStr == null)
return "";
try {
quoteSelection = Integer.parseInt (qStr) - 1;
if (quoteSelection < 0 || quoteSelection >= howMany)
return "";
return quotes[quoteSelection][0] + " " +
quotes[quoteSelection][analysisSelection];
} catch (NumberFormatException nfe) { }
return "";
}
private int getLineCount (String fileName) {
int howMany = 0;
try {
FileReader fr = new FileReader (fileName);
LineNumberReader ln = new LineNumberReader (fr);
while (ln.readLine() != null) {
howMany++;
}
} catch (FileNotFoundException fnfe) { fnfe.printStackTrace();
} catch (IOException ioex) { ioex.printStackTrace();
}
return howMany;
}
public void actionPerformed (ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals (bNames[0])) System.exit (0);
else if (cmd.equals (bNames[1])) tf.setText (getQuotes());
else
JOptionPane.showMessageDialog (null, helpfulText, "Swing Help",
JOptionPane.INFORMATION_MESSAGE);
}
public static void main (String[] args) {
JFrame cFrame = new JFrame ("Crucible");
cFrame.add (new Crucible());
cFrame.pack();
cFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
cFrame.setVisible (true);
}}