Populating a JEditorPane
Hello,
I am recieving information from a RAT server that I have built.
The information appears in the CMD window using:
in =new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
...
String str,s ="";
while ((str = in.readLine()) !=null)
{
s = s + str +"\n";
}
System.out.println(s);
I now want this (output) to be in a JEditorPane instead, inside my GUI.
How would I get the string(s) into a JEditorPane?
This is what I have so far:
publicvoid process
{
...
String str,s ="";
while ((str = in.readLine()) !=null)
{
s = s + str +"\n";
}
System.out.println(s);
jep =new JEditorPane("text/html","");
browserScroll =new JScrollPane(jep);
jep.setText(s);
}
This is the first lines of my code which show the rest of the JEditorPane:
publicclass RatClientimplements ActionListener
{
JTextField host =null;
JTextField port =null;
JTextField cmd =null;
JEditorPane jep =null;
JScrollPane browserScroll =null;
public Component createComponents()
{
JLabel hostLabel =new JLabel("Enter host: ", SwingConstants.LEFT);
host =new JTextField("192.168.0.4");
JLabel portLabel =new JLabel("Enter port: ", SwingConstants.LEFT);
port =new JTextField("80");
JLabel cmdLabel =new JLabel("Enter command: ", SwingConstants.LEFT);
cmd =new JTextField("E:\\Test.exe");
JButton button =new JButton("Connect");
button.addActionListener(this);
JPanel pane =new JPanel(new GridLayout(0, 2));
pane.add(jep);
pane.add(browserScroll);
pane.add(hostLabel);
pane.add(host);
pane.add(portLabel);
pane.add(port);
pane.add(cmdLabel);
pane.add(cmd);
pane.add(button);
pane.setBorder(BorderFactory.createEmptyBorder(180, 30, 30, 90));
return pane;
}
Anymore code needed - just say.
Cheers
[3600 byte] By [
John4938a] at [2007-10-3 4:28:44]

Next time I will post in swing forum.
I had a look at the read() in the API and im not sure how to use it.
I have done what you said although I need to get the variable s to the CreateComponents() method to fill the JEditorPane and I do not know how to do this - maybe if i use this.s = s; and put the appropriate method underneath although I am not sure.
This is my code:
import java.net.*;
import javax.swing.text.html.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
* Requires RatServer to be running
* set path="C:\Program Files\Java\jdk1.5.0_08\bin"
*/
public class RatClient implements ActionListener
{
JTextField host = null;
JTextField port = null;
JTextField cmd = null;
JEditorPane jep = null;
public Component createComponents()
{
jep = new JEditorPane(read(String s));
JLabel hostLabel = new JLabel("Enter host: ", SwingConstants.LEFT);
host = new JTextField("192.168.0.4");
JLabel portLabel = new JLabel("Enter port: ", SwingConstants.LEFT);
port = new JTextField("80");
JLabel cmdLabel = new JLabel("Enter command: ", SwingConstants.LEFT);
cmd = new JTextField("E:\\Test.exe");
JButton button = new JButton("Connect");
button.addActionListener(this);
JPanel pane = new JPanel(new GridLayout(0, 2));
pane.add(jep);
//pane.add(browserScroll);
pane.add(hostLabel);
pane.add(host);
pane.add(portLabel);
pane.add(port);
pane.add(cmdLabel);
pane.add(cmd);
pane.add(button);
pane.setBorder(BorderFactory.createEmptyBorder(180, 30, 30, 90));
return pane;
}
public void actionPerformed(ActionEvent e)
{
String host2 = host.getText();
String port2 = port.getText();
String command = cmd.getText();
try
{
process(host2, port2, command);
}
catch(IOException ioe)
{
System.out.println(ioe);
System.exit(1);
}
}
private static void Gui()
{
//Create and set up the window.
JFrame frame = new JFrame("RatClient");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RatClient app = new RatClient();
Component contents = app.createComponents();
frame.getContentPane().add(contents);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public void process(String host2, String port2, String command) throws IOException
{
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try
{
echoSocket = new Socket(host2, Integer.parseInt(port2)); // Socket is host and port combined
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host: " + host2);
System.exit(1);
}
catch (IOException e)
{
System.out.println("Couldn't get I/O for the connection to: "+ host2);
System.exit(1);
}
//-SEND ANYTHING TO THE SERVER HERE-
//TO SEND TO SERVER: write to 'out'
//TO READ FROM SERVER: read from 'in'
out.println(command); //send it to the server
String str,s ="";
while ((str = in.readLine()) != null)
{
s = s + str + "\n";
}
System.out.println(s);
//-END SENDING TO SERVER
out.close();
in.close();
echoSocket.close();
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
Gui();
}
});
}
}
Thanks