Basically I have mainclass which has frame in it, and calls "mainTabbedScreens" class for tabbedPane and this class in turn calls "analysisPage" class where I have JTextArea for which I want to set JScrollPane.
I had looked into two examples Example:1 and Example:2 to fix my problem but I couldn't, please let me know where I am going wrong!
analysis class: (So far I have tried!)
public class analysisPage {
private JPanel panel1;
private JTextField txtGraphPage;
private JTextArea textArea;
private Component scroll;
public analysisPage() {
createPageScreen1();
}
// function for panel - page - 1
private void createPageScreen1() {
panel1 = new JPanel();
panel1.setLayout(null);
//for title label
JLabel lblProcessingData = new JLabel("Processing data............................................");
lblProcessingData.setBounds(350, 5, 415, 10);
panel1.add(lblProcessingData);
String fileName = "loadFiles\\testFile.txt";
try {
textArea = new JTextArea();
textArea.setBounds(350, 50, 400, 378);
textArea.setBorder (new TitledBorder (new EtchedBorder(), fileName));
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setVisible(true);
FileReader reader = new FileReader(fileName);
BufferedReader br = new BufferedReader(reader);
textArea.read(br, null);
br.close();
textArea.requestFocus();
JScrollPane scroll = new JScrollPane(textArea);
scroll.setVerticalScrollBarPolicy (ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
} catch (Exception e) {
e.printStackTrace();
}
//panel1.add(scroll);
panel1.add(textArea);
panel1.setVisible(true);
//this is where trying to set the scroll for text area;
}
public JPanel getPanel1() {
return panel1;
}
}
If I do
panel1.add(scroll);
get a NullPointerException error, but if I do
panel1.add(textArea);
I don't get an error but scroll is not set. Please give me the directions, Thanks.