• Post Reply Bookmark Topic Watch Topic
  • New Topic

displaying constantly generated text in JTextArea

 
Diego Bowen
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a process that is generating constant output and I'm trying to display it into a text area display... the "append" doesnt seem to work can I do this at all??? Thanks
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JTextArea.append() works fine for me:

Share some code with us to give us a better idea as to what you are up against.
[ October 08, 2003: Message edited by: Joe Ess ]
 
Ian Hamilton
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi can someone please help and tell me why I get a NPE when I append to the JTextArea.

The example in the thread above runs fine on my PC but my code below doesn't work and I can't spot any differences!!

Thanks

Ian


package JavaDev.TestGUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;


public class Window extends JFrame{

private final static String newLine = "\n";
private JTextArea log;

private Component createLogger(){

JPanel pane = new JPanel(new GridLayout(0, 1));

log = new JTextArea("Hola");
log.setEditable(false);
log.setLineWrap(true);
log.setWrapStyleWord(true);

JScrollPane scrollPane =
new JScrollPane(log,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

pane.add(log);

pane.setBorder(BorderFactory.createEmptyBorder(
30, //top
30, //left
10, //bottom
30) //right
);
pane.setPreferredSize(new Dimension(600, 600));
return pane;
}

public void writeToLog(String text){

log.setText(text + newLine);
//log.setCaretPosition(log.getDocument().getLength());
}

private Component createButtons(){

JPanel pane = new JPanel();

JButton cancelButton = new JButton("Cancel");
JButton buildButton = new JButton("Start Build..");

pane.add(cancelButton);
pane.add(buildButton);

/*pane.setBorder(BorderFactory.createEmptyBorder(
30, //top
30, //left
30, //bottom
30) //right
);*/

return pane;
}

private Component createFileChooser(){

JPanel pane = new JPanel(new GridLayout(0, 1));

JTextField field = new JTextField("Build Locaton: " + System.getProperty("user.home"));
field.setEditable(false);
JButton editButton = new JButton("Edit Build Location..");
pane.add(field);
pane.add(editButton);

/*pane.setBorder(BorderFactory.createEmptyBorder(
30, //top
30, //left
30, //bottom
30) //right
);*/

return pane;
}

private void createAndShowGUI(){

//Make sure we have nice windows decorations
JFrame.setDefaultLookAndFeelDecorated(true);
//WindowUtilities.setNativeLookAndFeel();

//Create and set up the window.
JFrame frame = new JFrame("MarketAxess Release Automation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Window win = new Window();
Component contents = win.createLogger();


frame.getContentPane().add(contents, BorderLayout.CENTER);

frame.add(createFileChooser(),BorderLayout.PAGE_START);

frame.add(createButtons(), BorderLayout.PAGE_END);

//Display the window.
frame.pack();
frame.setVisible(true);
}



public void launch() {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}



}
[ July 16, 2006: Message edited by: Ian Hamilton ]
 
Ian Hamilton
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok I solved the NPE by initializing the JTextArea in the constructor

public class Window extends JFrame{

private final static String newLine = "\n";
private JTextArea log;

public Window(){

log = new JTextArea();
}

etc


However the JTextArea.append() is still not working! Where am I going wrong??

public void writeToLog(String text){

log.append(text);
log.append(newLine);

//log.setCaretPosition(log.getDocument().getLength());
}

Many Thanks

Ian
 
Ian Hamilton
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok solved it! I found the bug and replaced this code;


with this code




still trying to fully understand where I went wrong so if someone could speed my discovery up by explaining exactly what the bug was I'd be very appreciative

I'm guessing it has something to do with the Windows win = new Window() call but if that is the case why did the JTextArea get displayed?

Thanks again

[ July 16, 2006: Message edited by: Ian Hamilton ]
[ July 16, 2006: Message edited by: Ian Hamilton ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!