Resize portable word JTextArea

So I have JTextArea on JPanel (BoxLayout). I also have a Box filler that populates the rest of JPanel. I need my JTextArea to start at a single line height (I can do this), and also expand and shrink when necessary.

Word wrap is on, I just need to configure it when a new line is added / removed.

I tried with documentListener and getLineCount () but did not recognize the wordwrap-newlines lines.

I would like to avoid interference with fonts if possible.

AND, NO VIEWING THE PANELS. It is very important that JTextArea is displayed completely at all times.

+6
source share
1 answer

JTextArea has a rather specific side effect, in the right conditions it can grow on its own. I accidentally stumbled upon this by accident when I tried to create a simple two-line text editor (the length of limited characters per line, a maximum of two lines) ...

In principle, given the correct layout manager, this component can grow on its own - in fact, it makes sense, but it took me by surprise ...

I'm so smallLook at me grow

Now you can use the ComponentListener to monitor when a component changes size if it interests 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(); } } 
+13
source

Source: https://habr.com/ru/post/923202/


All Articles