1

I've been trying to populate a JPanel (here reffered to as 'BulletinsJPanel') in a class reffered to as 'Home.java' with a textfield (here reffered to as 'readerSetTxtJTextField') when a JLabel is clicked. ReaderPanel is in another class called 'Reader.java' which is supposed to read the contents of a text file and populate a JTextField object with a line of text.

I'm using netbeans which doesn't show me any code error highlights.

I would really appreciate some help in getting the textfield 'readerSetTxtJTextField' to show. Thanks a lot in advance.

Heres my code:

// The class Home

package Panels;
public class Home extends javax.swing.JPanel {
    // Here's a method in the class 'Home.java' which should populate 'BulletinsJPanel' with the contents
    private void MouseClickedList(java.awt.event.MouseEvent evt) {
        BulletinsJPanel.add(Reader.readerMouseClickedList());
        BulletinsJPanel.revalidate();
        BulletinsJPanel.repaint();
    }
}

// Now the class Reader below

package Database;
public class Reader {
    public static JTextField readerSetTxtJTextField;

    public static JTextField readerMouseClickedList() {                                  
        try {
            // Our code
            String fileURL = "/D:/TestFile.txt/";
            List<String[]> matches = new ArrayList<String[]>();
            String finalText;

            FileInputStream fileInputStream = new FileInputStream(fileURL);
            DataInputStream dataInputStream = new DataInputStream(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dataInputStream));

            String stringLine;

            while((stringLine = bufferedReader.readLine()) != null) {
                String splittable = "[\\s]", splitLenth = "{955}";
                if(stringLine.startsWith("2013001")) {
                    String[] splits = stringLine.split(splittable + splitLenth );

                    matches.add(splits);
                }
            }
            dataInputStream.close();

            for(String[] items : matches) {
                int itemsLength = items.length;
                int i;
                for(i = 0; i <= itemsLength; i++) {
                    finalText = (items[i]);

                    readerSetTxtJTextField = new JTextField();
                    readerSetTxtJTextField.setText(finalText);
                }

            }

        } catch (Exception e) {
            // Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
        return readerSetTxtJTextField;
    }                                 

}
4

1 回答 1

2

作为对您的错误的快速修复,更改:

for(i = 0; i <= itemsLength; i++)

for(i = 0; i < itemsLength; i++)
于 2013-07-28T08:07:25.003 回答