How do I wrap text in a jTextPane? - java

I'm currently using a jTextArea, and that has a setLineWrap() method to indicate whether text should be wrapped or not. Do jTextPanes have an equivalent to this? I know it can wrap words, but I need it to wrap characters.
For example, "oooooooooooo..." should be wrapped and displayed as two or more lines of o's instead of one long line with a scroll bar. So is this possible with jTextPanes?

JTextPanes word wrap automatically, you have to set the size.
jTextPane.setSize(someValue);
The size should be set to something small.
But this doesn't help you because it doesn't do that with characters... Try using <br> maybe? Or check this link: http://java-sl.com/wrap.html

Related

How to know the line number when you select on line on a JTextArea? [JAVA]

I have done a GUI in Java with a JTextArea. It is filled with the content of a file.
When I select words with the mouse on the textarea, a new frame pops up on which I do some operations on the selected words. To do these operations, I need to know the line number of the selected text...
Does someone know how to get the line number?
(I look to some methods on the classes JTextArea and MouseListener, but i dont know how to do that...)
Thanks ;)
Check out the Text Utilities. The getLineAtCaret() method is close to what you need. It uses the offset of the caret to get the line number. In you case you will need to use the start offset of the selected text.

How to set word wrap for JComboBox?

I want to display multiple-line text for each item in combobox. Words should go to the next line automatically if the exceed the width of combobox. How to achieve this?
String meanings;// array which contains pretty long sentences
JComboBox<String> meaningsComboBox=new JComboBox<String>(meanings);
Maybe wrapping each value into html will help you(this trick work with JLabels), example here.
In another way, I think, you need make custom renderrer, example here

is that possible to built a simple text editor in java without using JTextArea?

I have questions according to my final project in my IT Faculty.. I have to make A simple text editor (like notepad) without using JTextArea (GUI Java), I have to make my own JTextArea..
Idea/Topic= Own Text Editor
Algorithms= doesnt have any idea yet
Data Structure= Rope(Member of Binary Trees)
Requirement= Typing without JTextArea...
Is that possible to make that? Can a panel (or something else like that) has a listener or make a listener to do that?
simply thinking, First step I just want to try for typing on a panel, absolutely algorithms needed but i dont get right algorithms so far.. Is that possible?
My thinking is, for text that im typing is stored to a tree.. Is that Rope(Member of Binary Trees) for my data Structure?
Thanks for helping...
It's possible.
Use as you mentioned JPanel attaching KeyListener and MouseListener.
You need a model (Document). For simplest case it could be e.g StringBuilder where you can add content and show the StringBuilder content.
You need caret position (int field) to indicate where the edit should happen.
You need Font field to keep all the font info to be used in the editor.
Override paintComponent() and use FontMetrics to measure the text and calculate necessary width/height. For the start I would create a text area without line wrap.
Implement viewToModel/modelToView methods. They should calculate position of caret for given x,y and x,y for given caret position.

How to make the content of a SmartGWT label not trim spaces?

Pretty self-explanatory. Whenever I set the contents of a label, any spaces at the beginning or end are removed. I've looked at the API and found little to help me. setExtraSpaces() will add spacing to the top and bottom, which I don't want. Something like setLeft doesn't seem to add any actual space -- perhaps some other layout is conflicting to cause that not to happen but even then it seems like a bit of a work around. So, basically, I just want to know if there's any way to make the content exactly what I put in?
From the JavaDoc:
setContents
public void setContents(String contents)
The contents of a canvas or label widget. **Any HTML string is acceptable**.

Selecting specified text in an HTML formatted JEditorPane

I am displaying text in a Java JEditorPane using HTML to fomrat the text. I am also designing a search function that finds text in the JEditorPane selects the text and then scrolls to it. My problem is creating an algorithim that will actually specify the beginning and ending position for the selection.
If I simply retrieve the text using myeditorpane.getText(), then find the search string in the result, the wrong selection start and end positions are calculated with the wrong text being selected (the tags are throwing the calculation off). I tried removing the html tags by executing a replace all function text.().replaceAll("\<.*?>","") before searching for the text (this replace all removes all text in between the tags) but still the wrong selection points are calculated (although I'm getting close :-)).
Does anyone have an easy way to do this?
Thanks,
Elliott
You probably want to be working with the underlying Document, rather than the raw text, as suggested in this HighlightExample.
You need to find the start location of the text. I guess something like:
int offset = editorPane().getDocument().getText().indexof(...);
Then to scroll you can use:
editorPane.scrollRectToVisible( editorPane.viewToModel(offset) );
Read up on Text and New Lines for more info.

Categories