Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Wordwrap on a JTextArea  RSS feed

 
john omeara
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
I am currently having problems with writing to a text area. In the code below i create a JTextArea which is appended to by other classes namely Tcpdump. Unfortuntaley when the first line is written to no more text is written to the JTextArea or that can be seen anyway. Have i set up wordwrap correctly or is there a way that when you call append on my textarea in the tcpdump class it adds a newline character.
Heres my code anyway
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import jpcap.*;
import java.io.*;
public class Serverside1 extends JFrame
{
private JFrame window;
private MenuBar bar;
private MenuItem start;
private Menu capture;
public JTextArea text;
public Tcpdump td;
public Serverside1()
{
// Create frame and panels for GUI
window = new JFrame("Network Packet Sniffer");
window.setSize(700,500);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
GridLayout gridLayout = new GridLayout(2,1);
panel.setLayout(gridLayout);
JTextArea text = new JTextArea("",18, 30);
text.setWrapStyleWord(true);
panel.add(text);
window.getContentPane().add(panel);
window.show();
bar = new MenuBar();
window.setMenuBar(bar);
start = new MenuItem("Start");
capture = new Menu("Capture");
capture.add(start);
bar.add(capture);
window.setVisible(true);
Tcpdump td = new Tcpdump();
try
{
td.printOutput(text);
}
catch(IOException e){
System.out.println("Bollox");}
}
public static void main(String[] args)throws java.io.IOException{
Serverside1 s = new Serverside1();
}
}
Many thanks
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First things first. You need to start using UBB Code tags to format your code when posting. It is very hard to read otherwise.
Next, without seeing your tcpdump class, it's hard to tell what's going on. Can you at least provide the method where you are adding text to your TextArea? You should be using the append() method of the textArea. I'm not sure if this is your problem or not. I am running some code of my own to see.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, you probably need to do a

This tells the Text Area to wrap. The method you used tells it to wrap the entire word instead of splitting words into 2 pieces.
 
john omeara
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your time Greg that seems to have done the trick.
john
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!