emotki w JTextPane

0

Wie ktoś może jak można wyświetlać emotki w JTextPane po wpisaniu np. :) ?

0

Użyj HTML'a

0

a może mi ktoś powiedzieć jak w JTextPane przechodzić do nowej linii bo cały czas mi się nadpisuje.
Próbowałem +"\n" , +"\r\n" i +"\r"
Próbowałem też tak
final static String newline = "\n";
i później +newline ale też nie dało rady

tekst dodaję w ten sposób oknopo.setText("<html>"+text+"\r\n"+"
</html>");

0

Jak masz "czysty" tekst (bez HTML-a) to tak:

oknopo.setText(oknopo.getText()+"\n"+tekst);

co IMHO jest oczywiste.
jeśli używasz HTML-a, to tak:

String s=oknopo.getText();
oknopo.setText(s.substring(0,s.length()-7)+"<br>"+tekst+"</html>");
0

Może się do czegoś przyda:

import javax.swing.JFrame;
import javax.swing.JTextPane;
import java.awt.BorderLayout;

import javax.swing.text.Document;
import javax.swing.text.Style;
import javax.swing.text.StyleContext;
import javax.swing.text.StyleConstants;

import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.StyleSheet;
import javax.swing.text.BadLocationException;
import javax.swing.text.html.HTML;

import java.io.IOException;

public class Main extends JFrame {
	public Main() {
		super("TextPane test");
		setSize(400,300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getContentPane().setLayout(new BorderLayout());

		TextPaneTest tpt = new TextPaneTest("Hello World!");
		tpt.append("\n\nCośtam cośtam");
		getContentPane().add(BorderLayout.CENTER, tpt);


		setVisible(true);
	}

	public static void main(String[] args) {
		new Main();
	}
}

class TextPaneTest extends JTextPane {
	private HTMLEditorKit htmlEditor = null;
	private HTMLDocument htmlDoc = null;
	private StyleSheet css = null;

	public TextPaneTest() {
		super();
		this.setEditable(false);
		this.setContentType("text/html");
		this.setText("<html><head></head><body></body></html>");
		init();
	}

	public TextPaneTest(String text) {
		super();
		this.setEditable(false);
		this.setContentType("text/html");
		this.setText("<html><head></head><body></body></html>");
		init();
		append(text);
	}



	public void append(String text) {
		Document doc = this.getDocument();
		try {
			htmlEditor.insertHTML(htmlDoc, doc.getEndPosition().getOffset()-1, filter(text), 0,1, null);
			goToEnd();
		}catch(BadLocationException e) {
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}
	}

	private void goToEnd() {
		this.selectAll();
		int h = this.getSelectionEnd();
		this.select(0,h);
		this.select(h,h);
	}

	private String filter(String text) {
		if(text == null) return "";
		text = text.replaceAll("\\<","&lt;");
		text = text.replaceAll("\\>","&gt;");
		text = text.replaceAll("\n","<br/>");
		return text;
	}


	private void init() {
		this.htmlEditor = new HTMLEditorKit();
		this.htmlDoc = (HTMLDocument)htmlEditor.createDefaultDocument();
		this.css = htmlEditor.getStyleSheet();


		css.addRule("body { background-color: #000000;");
		css.addRule("body { color: #FFFFFF;");
		css.addRule("body { font-family: verdana, tahoma, sans-serif;");
		css.addRule("body { font-size: 14px;");
		css.addRule("body { font-style: normal;");
		css.addRule("body { font-weight: bold;");


		this.setDocument(htmlDoc);
	}
}
0

dzięki za pomoc ;-)
a może jeszcze mi to ktoś wyjaśnić
mam strumień wyjściowy do serwera i niby powinno wysyłać dane ale tego nie robi

OutputStream out = socket.getOutputStream();

.......

                            String data = oknotxt.getText(); 
			data= ""+id+":"+text;
			out.write(data.getBytes());
			out.write("\r\n".getBytes());
			out.flush();

Czy to powinno być tak?

1 użytkowników online, w tym zalogowanych: 0, gości: 1