Results 1 to 5 of 5
  1. #1
    justTina is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Question Multiple jtextareas in a splitpane?

    I am attempting to create a splitpane to display two different text streams side by side.

    I create a frame which contains a splitPane, which contains a jScrollPane in each side. Then I create a jTextArea for each of the jScrollPanes. Then I write a line of text to each jTextArea. As the code processes, I want to write text into one side or the other.

    The problem is the second jTextArea. The first time I write to it, everything is ok. The second time I attempt to write to it (called from another class), a nullpointerexception error returns. All writes to the first jTextArea are correct.

    Java Code:
    public class BuildFrame extends JFrame {
    
        private static JTextArea serverArea;
        private static JTextArea clientArea;
        
        public void createFrame() throws IOException {
    
            // Create the results frame
            JFrame MyFrame = new JFrame("My Frame");
            MyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            serverArea = new JTextArea();
            clientArea = new JTextArea();
    
            // Create the scroll panes to the text areas
            JScrollPane pScrollServer = new JScrollPane(serverArea,
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            JScrollPane pScrollClient = new JScrollPane(clientArea,
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    
            //Create a split pane with the two scroll panes in it.
            JSplitPane splitGUI = new JSplitPane(
                    JSplitPane.HORIZONTAL_SPLIT,
                    pScrollServer, pScrollClient);
    
            MyFrame.getContentPane().add(splitGUI);
            serverArea.setText(serverArea.getText() + "SERVER:\n");
            clientArea.setText(clientArea.getText() + "CLIENT:\n");
    
            MyFrame.pack();
            MyFrame.setVisible(true);
        }
    
        public static void writeServerOutput(String newLine) {
            try {
                serverArea.setText(serverArea.getText() + "\n" + newLine);
            } catch (Exception e) {
                System.out.println("Write to Server Text Area Error: " + e.getMessage());
            }
        }
    
        public static void writeClientOutput(String newLine) {
            try {
                clientArea.setText(clientArea.getText() + "\n" + newLine);
            } catch (Exception e) {
                System.out.println("Write to Client Text Area Error: " + e.getMessage());
            }
        }
    }
    Any ideas would be greatly appreciated!
    Last edited by justTina; 04-05-2010 at 09:31 PM. Reason: Remove clutter

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    First of all, don't declare your text areas as static. There's no reason to do this, and there's an increased risk for significant problems now and down the road if you do this.

    Also same for your static writeXxxxOutput methods, they should be instance (non-static).

    In fact, this issue may be a major contributor to your problem.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    And you should be using:

    textArea.append(....);

    not

    textArea.setText(...);

  4. #4
    justTina is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Smile

    Fubarable, thanks. You are absolutely correct. My code had bad form.

    Camickr, your suggestion was also correct. The append worked much better than setText.

    Thanks again for the quick assistance!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by justTina View Post
    Thanks again for the quick assistance!
    You're quite welcome! Please mark this thread as "Solved" if you can. Best of luck with your future coding!

Similar Threads

  1. Use multiple forms
    By coco in forum Java Applets
    Replies: 2
    Last Post: 06-14-2010, 05:14 AM
  2. Multiple Questions
    By Kaito in forum New To Java
    Replies: 4
    Last Post: 10-07-2009, 04:52 PM
  3. menubar in splitpane
    By masa in forum AWT / Swing
    Replies: 1
    Last Post: 12-16-2008, 09:01 AM
  4. Swing SplitPane Example
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:46 PM
  5. how to place a divider of the splitpane?
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •