JScrollPane not working with JTextArea

Hello,

I have a very simple class generated in Idea.  It has a JPanel, containing a JScrollPane which has the JTextArea set as it's view port.  This class implements ActionListener so that events can be fired to it to insert text into the JTextArea.  The problem that I have is that no matter what I do, the JTextArea will not show it's scroll bar when the text goes past the viewable area in the JTextArea.  I've done quite a bit of looking around on this and found somebody who said that I shouldn't set the MinimumSize and the PreferredSize on the JTextArea.  Well, the problem that I have is that Idea inserts those values automatically and I haven't found a way to switch them off to test to see if that's the problem.  My questions are:

1.  Is there a way to tell idea to stop auto inserting those fields via the setupUI method?
2.  Given the code below, does anybody see something that jumps out that may explain what is going on?  

Thanks,
Thomas

public class OutputView implements ActionListener {     private JPanel panel1;     private JTextArea outputTextArea;     private JScrollPane scrollPane;     private void createUIComponents()     {     }     public void actionPerformed(ActionEvent e)     {         final String message = e.getActionCommand();         // Append formatted message to textarea using the Swing Thread.         SwingUtilities.invokeLater(new Runnable()                {                 public void run()                {                  outputTextArea.append(message);            }          });     }     { // GUI initializer generated by IntelliJ IDEA GUI Designer // >>> IMPORTANT!! <<< // DO NOT EDIT OR ADD ANY CODE HERE!         $$$setupUI$$$();     }     /**      * Method generated by IntelliJ IDEA GUI Designer      * >>> IMPORTANT!! <<<      * DO NOT edit this method OR call it in your code!      *      * @noinspection ALL      */     private void $$$setupUI$$$()     {         createUIComponents();         panel1 = new JPanel();         panel1.setLayout(new BorderLayout(0, 0));         scrollPane = new JScrollPane();         scrollPane.setEnabled(true);         scrollPane.setVerticalScrollBarPolicy(20);         panel1.add(scrollPane, BorderLayout.CENTER);         outputTextArea.setEditable(false);         outputTextArea.setLineWrap(true);         outputTextArea.setMinimumSize(new Dimension(-1, -1));         outputTextArea.setPreferredSize(new Dimension(-1, -1));         outputTextArea.setRows(0);         scrollPane.setViewportView(outputTextArea);     }     /**      * @noinspection ALL      */     public JComponent $$$getRootComponent$$$()     {         return panel1;     } }

1 comment

As a test, since the GUI is very simple I created it manually (code below).  I took out the setMinimumSize and setPreferredSize and it now works perfectly.  So I guess the question is, using Idea's GUI builder, how do you create a JTextArea with a JScrollPane that works?  Or more correctly, how do you build the JTextArea without the setMinimumSize and setPreferredSize being called?


public class TestView implements ActionListener {     private JPanel panel1;     private JTextArea outputTextArea;     private JScrollPane scrollPane;     public TestView()     {         panel1 = new JPanel();         panel1.setLayout(new BorderLayout(0, 0));         scrollPane = new JScrollPane();         scrollPane.setEnabled(true);         scrollPane.setVerticalScrollBarPolicy(20);         panel1.add(scrollPane, BorderLayout.CENTER);         outputTextArea = new JTextArea();         outputTextArea.setEditable(false);         outputTextArea.setLineWrap(true);         outputTextArea.setRows(0);         scrollPane.setViewportView(outputTextArea);     }     public void actionPerformed(ActionEvent e)     {         final String message = e.getActionCommand();         // Append formatted message to textarea using the Swing Thread.         SwingUtilities.invokeLater(new Runnable()                {                 public void run()                {                  outputTextArea.append(message);            }          });     }

0

Please sign in to leave a comment.