Java – jtextarea auto wrap resize
So, I'm at JPanel (@ r_104_ 2419@Layout )Jtextarea. On I also have @ R_ 104_ 2419 @ the stuffer fills the rest of the JPanel I need my jtextarea to start with a single line height (I can manage it), and expand and reduce it when needed
Auto wrap is enabled. I only need to adjust the height of a new line when adding / deleting it
I try to use documentlistener and getlinecount (), but it doesn't recognize wordrap newlines
If possible, I want to avoid messing up the font
Moreover, there is no scroll panel Jtextarea must always be fully displayed
Solution
Jtextarea has quite special side effects. Under appropriate conditions, it can grow by itself When I tried to set up a simple two-line text editor (limited character length per line, up to two lines), I came across this
Basically, given the right layout manager, this component can grow on its own - it actually makes sense, but it surprises me
In addition, you may want to use componentlistener to monitor when components change size, if this is of interest to you
public class TestTextArea extends JFrame { public TestTextArea() { setLayout(new GridBagLayout()); JTextArea textArea = new JTextArea(); textArea.setColumns(10); textArea.setRows(1); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); add(textArea); setSize(200,200); setLocationRelativeTo(null); setDefaultCloSEOperation(EXIT_ON_CLOSE); setVisible(true); textArea.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent ce) { System.out.println("I've changed size"); } }); } /** * @param args the command line arguments */ public static void main(String[] args) { new TestTextArea(); } }