Results 1 to 5 of 5
- 04-05-2010, 09:28 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
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.
Any ideas would be greatly appreciated!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()); } } }Last edited by justTina; 04-05-2010 at 09:31 PM. Reason: Remove clutter
-
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.
- 04-06-2010, 04:51 AM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
And you should be using:
textArea.append(....);
not
textArea.setText(...);
- 04-06-2010, 07:02 PM #4
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
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!
-
Similar Threads
-
Use multiple forms
By coco in forum Java AppletsReplies: 2Last Post: 06-14-2010, 05:14 AM -
Multiple Questions
By Kaito in forum New To JavaReplies: 4Last Post: 10-07-2009, 04:52 PM -
menubar in splitpane
By masa in forum AWT / SwingReplies: 1Last Post: 12-16-2008, 09:01 AM -
Swing SplitPane Example
By Java Tip in forum javax.swingReplies: 0Last Post: 06-27-2008, 07:46 PM -
how to place a divider of the splitpane?
By christina in forum New To JavaReplies: 1Last Post: 08-06-2007, 07:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks