Java – how to calculate the number of rows in jtextarea, including the number of rows caused by packaging?
I have a jtextarea. I have set word wrap and wrap style word to true I want to "wrap" the jtextarea to the minimum possible height of a given specified width
To do this, I plan to use... To calculate the height of the font
Font font = jTextArea.getFont(); FontMetrics fontMetrics = jTextArea.getFontMetrics(font); int lineHeight = fontMetrics.getAscent() + fontMetrics.getDescent();
... and multiply it by the number of rows used in jtextarea The problem is jtextarea Getlinecount() calculates the number of returned lines that ignore wrapping lines
How to calculate the number of rows used in jtextarea, including the number of rows caused by word wrap?
Here are some demo code to make this easier I have a monitor that prints the number of trips every time the window is resized At present, it always prints 1, but I want to compensate for the word wrapping and print out how many lines are actually used
Editor: I have included the solution to the problem in the following code The static countlines method gives the solution
package components; import java.awt.*; import java.awt.event.*; import java.awt.font.*; import java.text.*; import javax.swing.*; public class JTextAreaLineCountDemo extends JPanel { JTextArea textArea; public JTextAreaLineCountDemo() { super(new GridBagLayout()); String inputStr = "Lorem ipsum dolor sit amet,consectetur adipisicing elit,sed do eiusmo"; textArea = new JTextArea(inputStr); textArea.setEditable(false); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); // Add Components to this panel. GridBagConstraints c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; c.weighty = 1.0; add(textArea,c); addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent ce) { System.out.println("Line count: " + countLines(textArea)); } }); } private static int countLines(JTextArea textArea) { AttributedString text = new AttributedString(textArea.getText()); FontRenderContext frc = textArea.getFontMetrics(textArea.getFont()) .getFontRenderContext(); AttributedCharacterIterator charIt = text.getIterator(); LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(charIt,frc); float formatWidth = (float) textArea.getSize().width; lineMeasurer.setPosition(charIt.getBeginIndex()); int noLines = 0; while (lineMeasurer.getPosition() < charIt.getEndIndex()) { lineMeasurer.nextLayout(formatWidth); noLines++; } return noLines; } private static void createAndShowGUI() { JFrame frame = new JFrame("JTextAreaLineCountDemo"); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JTextAreaLineCountDemo()); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokelater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
Solution
You can use the linebreakmeasurer class