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]
# 1
Swing related questions should be posted in the Swing forum.1) Create the JEditorPane component and add it to the GUI in your create components class.2) Use the read(...) method of JEditorPane to load the data.
camickra at 2007-7-14 22:31:43 > top of Java-index,Java Essentials,Java Programming...
# 2

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

John4938a at 2007-7-14 22:31:43 > top of Java-index,Java Essentials,Java Programming...
# 3

{

JTextField host = null;

JTextField port = null;

JTextField cmd = null;

JEditorPane jep = null;

BufferedReader in = null;

public Component createComponents()

{

jep = new JEditorPane();

try

{

jep.read(in, "");

}

catch(IOException ioe)

{

System.out.println(ioe);

System.exit(1);

}

I think i have made some progress in the above code. It compiles although when run it gives an error.

John4938a at 2007-7-14 22:31:43 > top of Java-index,Java Essentials,Java Programming...
# 4

> I had a look at the read() in the API and im not sure how to use it.

editorPane.read(inputStream, null);

> although I need to get the variable s to the CreateComponents() method to fill the JEditorPane

No you don't. The idea is to build the GUI with all the components. Then in your code you change the contents of the editor pane as required.

You don't need a variable "s". You read directly into the editor pane using the read method.

camickra at 2007-7-14 22:31:43 > top of Java-index,Java Essentials,Java Programming...
# 5
The code to load the data into the editor pane goes into your process method, not the createComponents method.That is it replaces your current code that builds a string.
camickra at 2007-7-14 22:31:43 > top of Java-index,Java Essentials,Java Programming...
# 6
Yes that worked, the JEditorPane needs to be resized and have a scroll barso I can see if there is anything in it.Thanks for the help
John4938a at 2007-7-14 22:31:43 > top of Java-index,Java Essentials,Java Programming...