0

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.

Community
  • 1
  • 1
Java.beginner
  • 871
  • 2
  • 19
  • 37
  • 1) Avoid use of null layouts -- this marks your code as a newbies code because with any Swing experience at all, you'll find that null layouts, which look terrible on all platforms other than your own, lead to rigid GUI's that are extremely difficult to upgrade and enhance. Instead learn to use and use the layout managers as they will make your coding much easier, and your GUI's much more functional. 2) Similarly avoid setting any component's bounds. 3) That is especially true of JTextAreas. Calling setBounds on this almost guarantees that a JScrollPane won't work with it. – Hovercraft Full Of Eels Mar 09 '15 at 16:48
  • Thanks for the comments, will try to change the layout as you suggested! – Java.beginner Mar 09 '15 at 17:08

1 Answers1

1

You are adding the

private Component scroll; which is not initialized to the to panel1.

Try changing your code this way:

    try {
        ...

        JScrollPane scroll1 = new JScrollPane(textArea);
        scroll1.setVerticalScrollBarPolicy (ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        panel1.add(scroll1);
    } catch (Exception e) {
        e.printStackTrace();
    }

Or if you want scroll to be a member of the analysisPage class:

    try {
        ...

        scroll = new JScrollPane(textArea);
        scroll.setVerticalScrollBarPolicy (ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        panel1.add(scroll);
    } catch (Exception e) {
        e.printStackTrace();
    }

Anyway, please respect code conventions to make your code more readable.. Your analysisPage should be named AnalysisPage.

As @Hovercraft Full Of Eels suggested there are several other problems in your code besides your NullPointerException, please read his comment. It will help you to better understand Swing and write better code.

antonio
  • 18,044
  • 4
  • 45
  • 61
  • @Hovercraft Full Of Eels You are right... I didn't know how to get to the point of the problem without changing the full code. Edited. – antonio Mar 09 '15 at 16:50