Nothing, really. I modified your example to use.
Nothing, really. I modified your example to use " texts = new ArrayList(); Dimension d = ta. GetPreferredSize(); String text = ta.getText(); String line = ""; for (int I = 0; I StringWidth(line + c) Add(line); for (String s : texts) { System.out.
Println("line: " + s); } System.out. Println("line count: " + ta.getLineCount()); System.out. Println("preferred: " + d); System.out.
Println("bounds1: " + fm. GetStringBounds(text1, null)); FontRenderContext frc = new FontRenderContext(null, false, false); TextLayout layout = new TextLayout(text1, ta.getFont(), frc); System.out. Println("layout1: " + layout.getBounds()); System.out.
Println("bounds2: " + fm. GetStringBounds(text2, null)); layout = new TextLayout(text2, ta.getFont(), frc); System.out. Println("layout2: " + layout.getBounds()); } }.
Thanks for this example and your time (+1). It was helpful in establishing, after some preliminary tests that taking control over the text setting of the area might be the only way to go. :) Please check my EDIT2.
– Boro May 14 at 10:37 EDIT2: Like mine, your text area's preferred width matches the widest line's width. EDIT3: I see matching lines on Mac OS X. – trashgod May 14 at 17:15.
One thing you can do is use FontMetrics. I wrote some code for splitting JTextAreas up at certain line numbers. The setup code looked like: Graphics2D g = (Graphics2D) g2; FontMetrics m = g.getFontMetrics(); int lineHeight = m.getHeight(); This will tell you how tall a line of text is.
Unfortunately, letters have different widths in most fonts. But, you can use the following code to determine the width of a String. Int width = m.
GetStringBounds("Some String", g).getWidth(); I know this doesn't fully answer your question, but I hope it helps. If you aren't using word wrap, here is the general algorithm you could use: (in the paint component method) String text = getText(). Split("\n"); String newText = ""; for (String line: text) { newText = line + "| " + line.length() + "\n"; } setText(newText); That's the general idea.
Not sure how well it would work out. Let me know if you try it.
Jjnguy Thanks for the input. Yea I was considering to use the FontMetrics. Thought I am not sure how then the string width relates to number of columns a JTextArea has.
– Boro May 12 at 14:57 @Boro, this would be much simpler if wordwrap were false. Is there a reason word wrap needs to be on? – Justin 'jjnguy' Nelson May 12 at 15:01 @jjnguy no not really.
I just thought a solution including wordwrap would have been a more general. But if you have a solution without wordwrap then go ahead I would like to check it out. – Boro May 12 at 15:05 @Boro, check out my edit.
– Justin 'jjnguy' Nelson May 12 at 15:12 @Boro, actually, I think that might cause infinite recursion...shoot. – Justin 'jjnguy' Nelson May 12 at 15:15.
I've seen people using TextLayout for something like this.
After a quick look. This looks promising . Thanks for that.Do you happen to have also a particular example in mind?
– Boro May 12 at 15:07 @Boro: Here's one stackoverflow. Com/questions/2275259/…. – Catalina Island May 12 at 15:24 I like @camickr's answer, but I'll throw in a second example of TextLayout.
– trashgod May 12 at 18:31 thanks for your time (+1). I will first try using FontMetric, then try TextLayout, but I am guessing similar algorithm will be used in each case. Please correct if I am wrong.
– Boro May 12 at 20:25 @Boro: I'm not sure, but I figured TextLayout used FontMetrics. – Catalina Island May 127 at 15:35.
Not sure if this helps but you need to set the width of the text area so that the view knows when to wrap the text. Once you set the size you can determine the preferred height. When you know the preferred height you can use the font metrice line height to determine the total number of lines including the wrapped lines if any.
Import java.awt. *; import javax.swing. *; public class TextAreaPreferredHeight extends JFrame { public TextAreaPreferredHeight() { JTextArea textArea = new JTextArea(); textArea.
SetText("one two three four five six seven eight nine ten"); textArea. SetLineWrap( true ); textArea. SetWrapStyleWord( true ); FontMetrics fm = textArea.
GetFontMetrics( textArea.getFont() ); int height = fm.getHeight(); System.out. Println("000: " + textArea. GetPreferredSize()); textArea.
SetSize(100, 1); System.out. Println("100: " + textArea. GetPreferredSize()); System.out.
Println("lines : " + textArea. GetPreferredSize(). Height / height); textArea.
SetSize(200, 1); System.out. Println("200: " + textArea. GetPreferredSize()); System.out.
Println("lines : " + textArea. GetPreferredSize(). Height / height); textArea.
SetSize(300, 1); System.out. Println("300: " + textArea. GetPreferredSize()); System.out.
Println("lines : " + textArea. GetPreferredSize(). Height / height); add(textArea); pack(); setVisible(true); } public static void main(String args) { new TextAreaPreferredHeight(); } }.
1 for interesting example. Let me think about how to use it now. – Boro May 12 at 20:16.
Okay, I had written a program that you could load in an image, and it would convert it to ascii art. I wanted it to automatically make the text area the right aspect ration based on the image that was input. However, I could never get it to work quite right.
I gave up and remarked out my attempts, here is the snippet of what I tried. What I ended up doing was just having a textarea that sometimes didn't fill all the way. //Graphics fontG = this.
TextBox4.CreateGraphics(); //fontG. PageUnit = GraphicsUnit. Point; //SizeF fontSize = fontG.
MeasureString(RowData, this. TextBox4. Font,(SizeF) this.
TextBox4. ClientSize); sb.AppendLine(); RowData = ""; //fontH += fontSize. Height + 1.2F; //fontW = (int) fontSize.Width.
Thanks @Doug Chamberlain the code of yours looks as the approach suggested by jjnguy. – Boro May 12 at 15:09.
There was a similar question for android yesterday. The way I see it need to be solved is by an iterative approach: Get JTextArea width Use the FontMetrics to get the width of a string, as jjnguy suggested split your string in words. Start measuring the witdh of a string adding one word at a time until you reach the area width.
Save that number. Once you reached it, start a new iteration, adding one word at a time (beginning with the number saved). The numbers of lines will be the number of iterations.
Unfortunately, row length will depend on the font and the particular string (not every character as the same width). You can count the number of characters in the words in each iteration, and return an array of lengths or a length average. This is the related android question: How to find android TextView number of characters per line?
Great tips. I was thinking to approach it in such a way. Let me try it out.(+1) – Boro May 12 at 20:23.
I found that counting the number of lines by text analysis only led to nothing. Instead my solution was calculating it from the preferred size of the text area ... import java.awt. Color; import java.awt.
Dimension; import java.awt. Font; import javax.swing. JFrame; import javax.swing.
JPanel; import javax.swing. JScrollPane; import javax.swing. JTextArea; import javax.swing.event.
CaretEvent; import javax.swing.event. CaretListener; public class LineNumbering extends JFrame { private static final long serialVersionUID = 1L; private static final Font fixedFont = new Font("Monospaced", Font. PLAIN, 12); private static JTextArea jta; private static JTextArea lines; private static String lineSeparator = "\n"; private static int numRows = 10; private static int numCols = 30; public LineNumbering() { super("Line Numbering Example"); } public static void createAndShowGUI() { JFrame frame = new LineNumbering(); frame.
SetDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); JScrollPane jsp = new JScrollPane(); jta = new JTextArea(numRows, numCols); jta. SetFont(fixedFont); jta.
SetLineWrap(true); jta. SetWrapStyleWord(true); lines = new JTextArea(numRows, 3); lines. SetEditable(false); lines.
SetFocusable(false); lines. SetEnabled(false); lines. SetFont(fixedFont); lines.
SetBackground(Color. LIGHT_GRAY); lines. SetDisabledTextColor(Color.
BLACK); // do initial line numbering for (int I = 1; I * Reset line numbers on caret event. Since the total number of * lines is calculated from preferred size of text area, we do this * on an event that occurred after repainting of the text area. * * (non-Javadoc) * * @see javax.swing.event.
CaretListener#caretUpdate(javax.swing.event. CaretEvent) */ @Override public void caretUpdate(CaretEvent e) { int totalLines = getTotalLinesInView(); System.out. Println("totalLines : " + totalLines); if (totalLines >= numRows) { lines.
SetText(getText()); } } } jta. AddCaretListener(new DebugCaretListener()); jsp.getViewport(). Add(jta); jsp.
SetRowHeaderView(lines); jsp. SetVerticalScrollBarPolicy(JScrollPane. VERTICAL_SCROLLBAR_NEVER); jsp.
SetHorizontalScrollBarPolicy(JScrollPane. HORIZONTAL_SCROLLBAR_NEVER); JPanel textPanel = new JPanel(); textPanel. Add(jsp); JPanel contentPanel = new JPanel(); contentPanel.
Add(textPanel); frame. SetContentPane(contentPanel); contentPanel. SetOpaque(true); frame.pack(); frame.
SetPreferredSize(new Dimension(500, 500)); frame. SetVisible(true); } public static void main(String args) { javax.swing.SwingUtilities. InvokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }.
I.k.e. Nice code sample. Thanks.
– Boro Sep 23 at 8:12.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.