How to make user-resizable JTextArea?2019 Community Moderator ElectionHow to make JTextField as wide as the windowHow do I efficiently iterate over each entry in a Java Map?How do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?JTable Won't Show On JPanelDrag and Drop nodes in JTreeHow do I convert a String to an int in Java?Enabling scroll bars when JTextArea exceeds certain amount of linesScrollable JTextArea that adjusts its size on resizeActionListner and ActionPerformed returning action outside of classDynamic JTextArea in JScrollPane ends up under scroll bar

Are all players supposed to be able to see each others' character sheets?

Which country has more?

How do I increase the number of TTY consoles?

Are E natural minor and B harmonic minor related?

What is this tube in a jet engine's air intake?

Strange opamp's output impedance in spice

Under what conditions can the right to be silence be revoked in the USA?

Do black holes violate the conservation of mass?

How to educate team mate to take screenshots for bugs with out unwanted stuff

Are small insurances worth it?

How can a demon take control of a human body during REM sleep?

Is it possible to clone a polymorphic object without manually adding overridden clone method into each derived class in C++?

Can I negotiate a patent idea for a raise, under French law?

Trocar background-image com delay via jQuery

What do you call someone who likes to pick fights?

Idiom for feeling after taking risk and someone else being rewarded

Smooth vector fields on a surface modulo diffeomorphisms

Too soon for a plot twist?

PTIJ: Sport in the Torah

Giving a career talk in my old university, how prominently should I tell students my salary?

Locked Away- What am I?

How should I solve this integral with changing parameters?

Optimal Proportions for Flying Humans

Do Paladin Auras of Differing Oaths Stack?



How to make user-resizable JTextArea?



2019 Community Moderator ElectionHow to make JTextField as wide as the windowHow do I efficiently iterate over each entry in a Java Map?How do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?JTable Won't Show On JPanelDrag and Drop nodes in JTreeHow do I convert a String to an int in Java?Enabling scroll bars when JTextArea exceeds certain amount of linesScrollable JTextArea that adjusts its size on resizeActionListner and ActionPerformed returning action outside of classDynamic JTextArea in JScrollPane ends up under scroll bar










0















Seems like the only option is to set number of rows, but I need my text area to be resizable for user.
JScrollPane helps, but when there's a lot of text, I want to let user resize the area itself.



How do I do this? If I can use another class for this purpose, I'll be totally ok with it.



Simplified code is



import javax.swing.*;
import java.awt.*;

public class Problematic

public static void main(String[] args)
JFrame f = new JFrame("frame");
f.setLayout(new BorderLayout());

JPanel p1 = new JPanel();
JPanel p = new JPanel();

JButton button = new JButton("Whatever here");
JTextArea t2 = new JTextArea(5, 30);

JScrollPane scrollPane = new JScrollPane(t2);

scrollPane.setSize(600, 400);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

t2.setText("this is some random textnthat may go for many rowsnso it may look messy");

p1.add(button);
p.add(scrollPane);

f.add(p, BorderLayout.NORTH);
f.add(p1, BorderLayout.CENTER);

f.setSize(600, 500);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);











share|improve this question




























    0















    Seems like the only option is to set number of rows, but I need my text area to be resizable for user.
    JScrollPane helps, but when there's a lot of text, I want to let user resize the area itself.



    How do I do this? If I can use another class for this purpose, I'll be totally ok with it.



    Simplified code is



    import javax.swing.*;
    import java.awt.*;

    public class Problematic

    public static void main(String[] args)
    JFrame f = new JFrame("frame");
    f.setLayout(new BorderLayout());

    JPanel p1 = new JPanel();
    JPanel p = new JPanel();

    JButton button = new JButton("Whatever here");
    JTextArea t2 = new JTextArea(5, 30);

    JScrollPane scrollPane = new JScrollPane(t2);

    scrollPane.setSize(600, 400);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    t2.setText("this is some random textnthat may go for many rowsnso it may look messy");

    p1.add(button);
    p.add(scrollPane);

    f.add(p, BorderLayout.NORTH);
    f.add(p1, BorderLayout.CENTER);

    f.setSize(600, 500);
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.setVisible(true);











    share|improve this question


























      0












      0








      0








      Seems like the only option is to set number of rows, but I need my text area to be resizable for user.
      JScrollPane helps, but when there's a lot of text, I want to let user resize the area itself.



      How do I do this? If I can use another class for this purpose, I'll be totally ok with it.



      Simplified code is



      import javax.swing.*;
      import java.awt.*;

      public class Problematic

      public static void main(String[] args)
      JFrame f = new JFrame("frame");
      f.setLayout(new BorderLayout());

      JPanel p1 = new JPanel();
      JPanel p = new JPanel();

      JButton button = new JButton("Whatever here");
      JTextArea t2 = new JTextArea(5, 30);

      JScrollPane scrollPane = new JScrollPane(t2);

      scrollPane.setSize(600, 400);
      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

      t2.setText("this is some random textnthat may go for many rowsnso it may look messy");

      p1.add(button);
      p.add(scrollPane);

      f.add(p, BorderLayout.NORTH);
      f.add(p1, BorderLayout.CENTER);

      f.setSize(600, 500);
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.setVisible(true);











      share|improve this question
















      Seems like the only option is to set number of rows, but I need my text area to be resizable for user.
      JScrollPane helps, but when there's a lot of text, I want to let user resize the area itself.



      How do I do this? If I can use another class for this purpose, I'll be totally ok with it.



      Simplified code is



      import javax.swing.*;
      import java.awt.*;

      public class Problematic

      public static void main(String[] args)
      JFrame f = new JFrame("frame");
      f.setLayout(new BorderLayout());

      JPanel p1 = new JPanel();
      JPanel p = new JPanel();

      JButton button = new JButton("Whatever here");
      JTextArea t2 = new JTextArea(5, 30);

      JScrollPane scrollPane = new JScrollPane(t2);

      scrollPane.setSize(600, 400);
      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

      t2.setText("this is some random textnthat may go for many rowsnso it may look messy");

      p1.add(button);
      p.add(scrollPane);

      f.add(p, BorderLayout.NORTH);
      f.add(p1, BorderLayout.CENTER);

      f.setSize(600, 500);
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.setVisible(true);








      java swing user-interface jtextarea






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 6 at 16:14









      Markus Steppberger

      258113




      258113










      asked Mar 6 at 13:33









      BobbyBobby

      178111




      178111






















          3 Answers
          3






          active

          oldest

          votes


















          1














          As first improvement you can make some small layout changes that will make the JTextArea occupy the whole space and resize with the frame :



          import java.awt.BorderLayout;
          import java.awt.GridLayout;
          import javax.swing.JButton;
          import javax.swing.JFrame;
          import javax.swing.JPanel;
          import javax.swing.JScrollPane;
          import javax.swing.JTextArea;
          import javax.swing.WindowConstants;

          public class Problematic

          public static void main(String[] args)
          JFrame f = new JFrame("frame");
          f.setLayout(new BorderLayout());

          JPanel p = new JPanel(new GridLayout(1, 1)); //assign gridlayout so text area fills panel
          JTextArea t2 = new JTextArea(5, 30);
          t2.setText("this is some random textnthat may go for many rowsnso it may look messy");

          JScrollPane scrollPane = new JScrollPane(t2);
          scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
          p.add(scrollPane);
          f.add(p, BorderLayout.CENTER); //place text area panel in center position

          JPanel p1 = new JPanel();
          JButton button = new JButton("Whatever here");
          p1.add(button);
          f.add(p1, BorderLayout.PAGE_END);

          f.setSize(600, 500);
          f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          f.setVisible(true);




          for additional flexibility you can add a JSplitPane :



          public class Problematic 

          public static void main(String[] args)
          JFrame f = new JFrame("frame");
          f.getContentPane().setLayout(new BorderLayout());

          JPanel p1 = new JPanel();
          JButton button = new JButton("Whatever here");
          p1.add(button);

          JPanel p = new JPanel(new GridLayout(1, 1)); //assign gridlayout so text area fills panel
          JTextArea t2 = new JTextArea(5, 30);
          t2.setText("this is some random textnthat may go for many rowsnso it may look messy");

          JScrollPane scrollPane = new JScrollPane(t2); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
          p.add(scrollPane);

          JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, p,p1);
          f.getContentPane().add(splitPane, BorderLayout.CENTER);

          f.setSize(600, 500);
          f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          f.setVisible(true);







          share|improve this answer
































            2














            You can use JSplitPane to make various areas of a window resizable. Try below example. See my comments in the code as well.



            import javax.swing.*;
            import java.awt.*;

            public class Problematic

            public static void main(String[] args)
            JFrame f = new JFrame("frame");
            f.setLayout(new BorderLayout());

            JPanel p1 = new JPanel();
            JPanel p = new JPanel();
            // Set BorderLayout so that scroll pane fills the panel
            p.setLayout(new BorderLayout());

            JButton button = new JButton("Whatever here");
            JTextArea t2 = new JTextArea(5, 30);

            JScrollPane scrollPane = new JScrollPane(t2);

            // Setting scroll pane size is not necessary
            //scrollPane.setSize(600, 400);
            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

            t2.setText("this is some random textnthat may go for many rowsnso it may look messy");

            p1.add(button);
            p.add(scrollPane);

            // Use JSplitPane to make the panels resizable
            f.add(new JSplitPane(JSplitPane.VERTICAL_SPLIT, p, p1), BorderLayout.CENTER);

            f.setSize(600, 500);
            f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            f.setVisible(true);







            share|improve this answer























            • Up voted for "Setting scroll pane size is not necessary". I missed that one.

              – c0der
              Mar 6 at 14:52


















            1














            Here is c0der's implementation, but set-up for a more streamlined life-cycle. Each block of lines explains what is going on and when.



            import java.awt.*;
            import java.awt.event.*;
            import javax.swing.*;
            import javax.swing.text.JTextComponent;

            public class TextApp implements Runnable
            private static final String APP_NAME = "Text App";

            private JFrame frame;
            private JTextArea txtAra;
            private JButton button;

            // This is a generic action that handles clearing the text of a JTextComponent
            // It can also be a stand-alone class
            private static class ClearAction <T extends JTextComponent> implements ActionListener
            private T txtAra;

            public ClearAction(T txtAra)
            this.txtAra = txtAra;


            @Override
            public void actionPerformed(ActionEvent e)
            this.txtAra.setText("");



            public TextApp()
            // Initialize instance fields
            frame = new JFrame(APP_NAME);
            txtAra = new JTextArea(5, 30);
            button = new JButton("Clear Text");

            // Internal panels used for layout
            JPanel mainPanel = new JPanel(new GridLayout(1, 1));
            JScrollPane scrollPane = new JScrollPane(txtAra);
            JPanel buttonPanel = new JPanel();

            // Add components to containers
            frame.setLayout(new BorderLayout());
            frame.add(mainPanel, BorderLayout.CENTER);
            frame.add(buttonPanel, BorderLayout.PAGE_END);
            mainPanel.add(scrollPane);
            buttonPanel.add(button);

            // Additional setup
            scrollPane.setSize(600, 400);
            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

            // Add listeners
            button.addActionListener(new ClearAction(txtAra));


            @Override
            public void run()
            // Set text
            txtAra.setText("this is some random textnthat may go for many rowsnso it may look messy");

            // Prepare frame
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);


            public static void main(String[] args)
            SwingUtilities.invokeLater(new TextApp());







            share|improve this answer






















              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55024370%2fhow-to-make-user-resizable-jtextarea%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              As first improvement you can make some small layout changes that will make the JTextArea occupy the whole space and resize with the frame :



              import java.awt.BorderLayout;
              import java.awt.GridLayout;
              import javax.swing.JButton;
              import javax.swing.JFrame;
              import javax.swing.JPanel;
              import javax.swing.JScrollPane;
              import javax.swing.JTextArea;
              import javax.swing.WindowConstants;

              public class Problematic

              public static void main(String[] args)
              JFrame f = new JFrame("frame");
              f.setLayout(new BorderLayout());

              JPanel p = new JPanel(new GridLayout(1, 1)); //assign gridlayout so text area fills panel
              JTextArea t2 = new JTextArea(5, 30);
              t2.setText("this is some random textnthat may go for many rowsnso it may look messy");

              JScrollPane scrollPane = new JScrollPane(t2);
              scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
              p.add(scrollPane);
              f.add(p, BorderLayout.CENTER); //place text area panel in center position

              JPanel p1 = new JPanel();
              JButton button = new JButton("Whatever here");
              p1.add(button);
              f.add(p1, BorderLayout.PAGE_END);

              f.setSize(600, 500);
              f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
              f.setVisible(true);




              for additional flexibility you can add a JSplitPane :



              public class Problematic 

              public static void main(String[] args)
              JFrame f = new JFrame("frame");
              f.getContentPane().setLayout(new BorderLayout());

              JPanel p1 = new JPanel();
              JButton button = new JButton("Whatever here");
              p1.add(button);

              JPanel p = new JPanel(new GridLayout(1, 1)); //assign gridlayout so text area fills panel
              JTextArea t2 = new JTextArea(5, 30);
              t2.setText("this is some random textnthat may go for many rowsnso it may look messy");

              JScrollPane scrollPane = new JScrollPane(t2); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
              p.add(scrollPane);

              JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, p,p1);
              f.getContentPane().add(splitPane, BorderLayout.CENTER);

              f.setSize(600, 500);
              f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
              f.setVisible(true);







              share|improve this answer





























                1














                As first improvement you can make some small layout changes that will make the JTextArea occupy the whole space and resize with the frame :



                import java.awt.BorderLayout;
                import java.awt.GridLayout;
                import javax.swing.JButton;
                import javax.swing.JFrame;
                import javax.swing.JPanel;
                import javax.swing.JScrollPane;
                import javax.swing.JTextArea;
                import javax.swing.WindowConstants;

                public class Problematic

                public static void main(String[] args)
                JFrame f = new JFrame("frame");
                f.setLayout(new BorderLayout());

                JPanel p = new JPanel(new GridLayout(1, 1)); //assign gridlayout so text area fills panel
                JTextArea t2 = new JTextArea(5, 30);
                t2.setText("this is some random textnthat may go for many rowsnso it may look messy");

                JScrollPane scrollPane = new JScrollPane(t2);
                scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                p.add(scrollPane);
                f.add(p, BorderLayout.CENTER); //place text area panel in center position

                JPanel p1 = new JPanel();
                JButton button = new JButton("Whatever here");
                p1.add(button);
                f.add(p1, BorderLayout.PAGE_END);

                f.setSize(600, 500);
                f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                f.setVisible(true);




                for additional flexibility you can add a JSplitPane :



                public class Problematic 

                public static void main(String[] args)
                JFrame f = new JFrame("frame");
                f.getContentPane().setLayout(new BorderLayout());

                JPanel p1 = new JPanel();
                JButton button = new JButton("Whatever here");
                p1.add(button);

                JPanel p = new JPanel(new GridLayout(1, 1)); //assign gridlayout so text area fills panel
                JTextArea t2 = new JTextArea(5, 30);
                t2.setText("this is some random textnthat may go for many rowsnso it may look messy");

                JScrollPane scrollPane = new JScrollPane(t2); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                p.add(scrollPane);

                JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, p,p1);
                f.getContentPane().add(splitPane, BorderLayout.CENTER);

                f.setSize(600, 500);
                f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                f.setVisible(true);







                share|improve this answer



























                  1












                  1








                  1







                  As first improvement you can make some small layout changes that will make the JTextArea occupy the whole space and resize with the frame :



                  import java.awt.BorderLayout;
                  import java.awt.GridLayout;
                  import javax.swing.JButton;
                  import javax.swing.JFrame;
                  import javax.swing.JPanel;
                  import javax.swing.JScrollPane;
                  import javax.swing.JTextArea;
                  import javax.swing.WindowConstants;

                  public class Problematic

                  public static void main(String[] args)
                  JFrame f = new JFrame("frame");
                  f.setLayout(new BorderLayout());

                  JPanel p = new JPanel(new GridLayout(1, 1)); //assign gridlayout so text area fills panel
                  JTextArea t2 = new JTextArea(5, 30);
                  t2.setText("this is some random textnthat may go for many rowsnso it may look messy");

                  JScrollPane scrollPane = new JScrollPane(t2);
                  scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                  p.add(scrollPane);
                  f.add(p, BorderLayout.CENTER); //place text area panel in center position

                  JPanel p1 = new JPanel();
                  JButton button = new JButton("Whatever here");
                  p1.add(button);
                  f.add(p1, BorderLayout.PAGE_END);

                  f.setSize(600, 500);
                  f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                  f.setVisible(true);




                  for additional flexibility you can add a JSplitPane :



                  public class Problematic 

                  public static void main(String[] args)
                  JFrame f = new JFrame("frame");
                  f.getContentPane().setLayout(new BorderLayout());

                  JPanel p1 = new JPanel();
                  JButton button = new JButton("Whatever here");
                  p1.add(button);

                  JPanel p = new JPanel(new GridLayout(1, 1)); //assign gridlayout so text area fills panel
                  JTextArea t2 = new JTextArea(5, 30);
                  t2.setText("this is some random textnthat may go for many rowsnso it may look messy");

                  JScrollPane scrollPane = new JScrollPane(t2); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                  p.add(scrollPane);

                  JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, p,p1);
                  f.getContentPane().add(splitPane, BorderLayout.CENTER);

                  f.setSize(600, 500);
                  f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                  f.setVisible(true);







                  share|improve this answer















                  As first improvement you can make some small layout changes that will make the JTextArea occupy the whole space and resize with the frame :



                  import java.awt.BorderLayout;
                  import java.awt.GridLayout;
                  import javax.swing.JButton;
                  import javax.swing.JFrame;
                  import javax.swing.JPanel;
                  import javax.swing.JScrollPane;
                  import javax.swing.JTextArea;
                  import javax.swing.WindowConstants;

                  public class Problematic

                  public static void main(String[] args)
                  JFrame f = new JFrame("frame");
                  f.setLayout(new BorderLayout());

                  JPanel p = new JPanel(new GridLayout(1, 1)); //assign gridlayout so text area fills panel
                  JTextArea t2 = new JTextArea(5, 30);
                  t2.setText("this is some random textnthat may go for many rowsnso it may look messy");

                  JScrollPane scrollPane = new JScrollPane(t2);
                  scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                  p.add(scrollPane);
                  f.add(p, BorderLayout.CENTER); //place text area panel in center position

                  JPanel p1 = new JPanel();
                  JButton button = new JButton("Whatever here");
                  p1.add(button);
                  f.add(p1, BorderLayout.PAGE_END);

                  f.setSize(600, 500);
                  f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                  f.setVisible(true);




                  for additional flexibility you can add a JSplitPane :



                  public class Problematic 

                  public static void main(String[] args)
                  JFrame f = new JFrame("frame");
                  f.getContentPane().setLayout(new BorderLayout());

                  JPanel p1 = new JPanel();
                  JButton button = new JButton("Whatever here");
                  p1.add(button);

                  JPanel p = new JPanel(new GridLayout(1, 1)); //assign gridlayout so text area fills panel
                  JTextArea t2 = new JTextArea(5, 30);
                  t2.setText("this is some random textnthat may go for many rowsnso it may look messy");

                  JScrollPane scrollPane = new JScrollPane(t2); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                  p.add(scrollPane);

                  JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, p,p1);
                  f.getContentPane().add(splitPane, BorderLayout.CENTER);

                  f.setSize(600, 500);
                  f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                  f.setVisible(true);








                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 6 at 14:00

























                  answered Mar 6 at 13:50









                  c0derc0der

                  9,13151846




                  9,13151846























                      2














                      You can use JSplitPane to make various areas of a window resizable. Try below example. See my comments in the code as well.



                      import javax.swing.*;
                      import java.awt.*;

                      public class Problematic

                      public static void main(String[] args)
                      JFrame f = new JFrame("frame");
                      f.setLayout(new BorderLayout());

                      JPanel p1 = new JPanel();
                      JPanel p = new JPanel();
                      // Set BorderLayout so that scroll pane fills the panel
                      p.setLayout(new BorderLayout());

                      JButton button = new JButton("Whatever here");
                      JTextArea t2 = new JTextArea(5, 30);

                      JScrollPane scrollPane = new JScrollPane(t2);

                      // Setting scroll pane size is not necessary
                      //scrollPane.setSize(600, 400);
                      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

                      t2.setText("this is some random textnthat may go for many rowsnso it may look messy");

                      p1.add(button);
                      p.add(scrollPane);

                      // Use JSplitPane to make the panels resizable
                      f.add(new JSplitPane(JSplitPane.VERTICAL_SPLIT, p, p1), BorderLayout.CENTER);

                      f.setSize(600, 500);
                      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                      f.setVisible(true);







                      share|improve this answer























                      • Up voted for "Setting scroll pane size is not necessary". I missed that one.

                        – c0der
                        Mar 6 at 14:52















                      2














                      You can use JSplitPane to make various areas of a window resizable. Try below example. See my comments in the code as well.



                      import javax.swing.*;
                      import java.awt.*;

                      public class Problematic

                      public static void main(String[] args)
                      JFrame f = new JFrame("frame");
                      f.setLayout(new BorderLayout());

                      JPanel p1 = new JPanel();
                      JPanel p = new JPanel();
                      // Set BorderLayout so that scroll pane fills the panel
                      p.setLayout(new BorderLayout());

                      JButton button = new JButton("Whatever here");
                      JTextArea t2 = new JTextArea(5, 30);

                      JScrollPane scrollPane = new JScrollPane(t2);

                      // Setting scroll pane size is not necessary
                      //scrollPane.setSize(600, 400);
                      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

                      t2.setText("this is some random textnthat may go for many rowsnso it may look messy");

                      p1.add(button);
                      p.add(scrollPane);

                      // Use JSplitPane to make the panels resizable
                      f.add(new JSplitPane(JSplitPane.VERTICAL_SPLIT, p, p1), BorderLayout.CENTER);

                      f.setSize(600, 500);
                      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                      f.setVisible(true);







                      share|improve this answer























                      • Up voted for "Setting scroll pane size is not necessary". I missed that one.

                        – c0der
                        Mar 6 at 14:52













                      2












                      2








                      2







                      You can use JSplitPane to make various areas of a window resizable. Try below example. See my comments in the code as well.



                      import javax.swing.*;
                      import java.awt.*;

                      public class Problematic

                      public static void main(String[] args)
                      JFrame f = new JFrame("frame");
                      f.setLayout(new BorderLayout());

                      JPanel p1 = new JPanel();
                      JPanel p = new JPanel();
                      // Set BorderLayout so that scroll pane fills the panel
                      p.setLayout(new BorderLayout());

                      JButton button = new JButton("Whatever here");
                      JTextArea t2 = new JTextArea(5, 30);

                      JScrollPane scrollPane = new JScrollPane(t2);

                      // Setting scroll pane size is not necessary
                      //scrollPane.setSize(600, 400);
                      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

                      t2.setText("this is some random textnthat may go for many rowsnso it may look messy");

                      p1.add(button);
                      p.add(scrollPane);

                      // Use JSplitPane to make the panels resizable
                      f.add(new JSplitPane(JSplitPane.VERTICAL_SPLIT, p, p1), BorderLayout.CENTER);

                      f.setSize(600, 500);
                      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                      f.setVisible(true);







                      share|improve this answer













                      You can use JSplitPane to make various areas of a window resizable. Try below example. See my comments in the code as well.



                      import javax.swing.*;
                      import java.awt.*;

                      public class Problematic

                      public static void main(String[] args)
                      JFrame f = new JFrame("frame");
                      f.setLayout(new BorderLayout());

                      JPanel p1 = new JPanel();
                      JPanel p = new JPanel();
                      // Set BorderLayout so that scroll pane fills the panel
                      p.setLayout(new BorderLayout());

                      JButton button = new JButton("Whatever here");
                      JTextArea t2 = new JTextArea(5, 30);

                      JScrollPane scrollPane = new JScrollPane(t2);

                      // Setting scroll pane size is not necessary
                      //scrollPane.setSize(600, 400);
                      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

                      t2.setText("this is some random textnthat may go for many rowsnso it may look messy");

                      p1.add(button);
                      p.add(scrollPane);

                      // Use JSplitPane to make the panels resizable
                      f.add(new JSplitPane(JSplitPane.VERTICAL_SPLIT, p, p1), BorderLayout.CENTER);

                      f.setSize(600, 500);
                      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                      f.setVisible(true);








                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 6 at 13:53









                      Prasad KarunagodaPrasad Karunagoda

                      1,6331813




                      1,6331813












                      • Up voted for "Setting scroll pane size is not necessary". I missed that one.

                        – c0der
                        Mar 6 at 14:52

















                      • Up voted for "Setting scroll pane size is not necessary". I missed that one.

                        – c0der
                        Mar 6 at 14:52
















                      Up voted for "Setting scroll pane size is not necessary". I missed that one.

                      – c0der
                      Mar 6 at 14:52





                      Up voted for "Setting scroll pane size is not necessary". I missed that one.

                      – c0der
                      Mar 6 at 14:52











                      1














                      Here is c0der's implementation, but set-up for a more streamlined life-cycle. Each block of lines explains what is going on and when.



                      import java.awt.*;
                      import java.awt.event.*;
                      import javax.swing.*;
                      import javax.swing.text.JTextComponent;

                      public class TextApp implements Runnable
                      private static final String APP_NAME = "Text App";

                      private JFrame frame;
                      private JTextArea txtAra;
                      private JButton button;

                      // This is a generic action that handles clearing the text of a JTextComponent
                      // It can also be a stand-alone class
                      private static class ClearAction <T extends JTextComponent> implements ActionListener
                      private T txtAra;

                      public ClearAction(T txtAra)
                      this.txtAra = txtAra;


                      @Override
                      public void actionPerformed(ActionEvent e)
                      this.txtAra.setText("");



                      public TextApp()
                      // Initialize instance fields
                      frame = new JFrame(APP_NAME);
                      txtAra = new JTextArea(5, 30);
                      button = new JButton("Clear Text");

                      // Internal panels used for layout
                      JPanel mainPanel = new JPanel(new GridLayout(1, 1));
                      JScrollPane scrollPane = new JScrollPane(txtAra);
                      JPanel buttonPanel = new JPanel();

                      // Add components to containers
                      frame.setLayout(new BorderLayout());
                      frame.add(mainPanel, BorderLayout.CENTER);
                      frame.add(buttonPanel, BorderLayout.PAGE_END);
                      mainPanel.add(scrollPane);
                      buttonPanel.add(button);

                      // Additional setup
                      scrollPane.setSize(600, 400);
                      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

                      // Add listeners
                      button.addActionListener(new ClearAction(txtAra));


                      @Override
                      public void run()
                      // Set text
                      txtAra.setText("this is some random textnthat may go for many rowsnso it may look messy");

                      // Prepare frame
                      frame.pack();
                      frame.setLocationRelativeTo(null);
                      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      frame.setVisible(true);


                      public static void main(String[] args)
                      SwingUtilities.invokeLater(new TextApp());







                      share|improve this answer



























                        1














                        Here is c0der's implementation, but set-up for a more streamlined life-cycle. Each block of lines explains what is going on and when.



                        import java.awt.*;
                        import java.awt.event.*;
                        import javax.swing.*;
                        import javax.swing.text.JTextComponent;

                        public class TextApp implements Runnable
                        private static final String APP_NAME = "Text App";

                        private JFrame frame;
                        private JTextArea txtAra;
                        private JButton button;

                        // This is a generic action that handles clearing the text of a JTextComponent
                        // It can also be a stand-alone class
                        private static class ClearAction <T extends JTextComponent> implements ActionListener
                        private T txtAra;

                        public ClearAction(T txtAra)
                        this.txtAra = txtAra;


                        @Override
                        public void actionPerformed(ActionEvent e)
                        this.txtAra.setText("");



                        public TextApp()
                        // Initialize instance fields
                        frame = new JFrame(APP_NAME);
                        txtAra = new JTextArea(5, 30);
                        button = new JButton("Clear Text");

                        // Internal panels used for layout
                        JPanel mainPanel = new JPanel(new GridLayout(1, 1));
                        JScrollPane scrollPane = new JScrollPane(txtAra);
                        JPanel buttonPanel = new JPanel();

                        // Add components to containers
                        frame.setLayout(new BorderLayout());
                        frame.add(mainPanel, BorderLayout.CENTER);
                        frame.add(buttonPanel, BorderLayout.PAGE_END);
                        mainPanel.add(scrollPane);
                        buttonPanel.add(button);

                        // Additional setup
                        scrollPane.setSize(600, 400);
                        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

                        // Add listeners
                        button.addActionListener(new ClearAction(txtAra));


                        @Override
                        public void run()
                        // Set text
                        txtAra.setText("this is some random textnthat may go for many rowsnso it may look messy");

                        // Prepare frame
                        frame.pack();
                        frame.setLocationRelativeTo(null);
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame.setVisible(true);


                        public static void main(String[] args)
                        SwingUtilities.invokeLater(new TextApp());







                        share|improve this answer

























                          1












                          1








                          1







                          Here is c0der's implementation, but set-up for a more streamlined life-cycle. Each block of lines explains what is going on and when.



                          import java.awt.*;
                          import java.awt.event.*;
                          import javax.swing.*;
                          import javax.swing.text.JTextComponent;

                          public class TextApp implements Runnable
                          private static final String APP_NAME = "Text App";

                          private JFrame frame;
                          private JTextArea txtAra;
                          private JButton button;

                          // This is a generic action that handles clearing the text of a JTextComponent
                          // It can also be a stand-alone class
                          private static class ClearAction <T extends JTextComponent> implements ActionListener
                          private T txtAra;

                          public ClearAction(T txtAra)
                          this.txtAra = txtAra;


                          @Override
                          public void actionPerformed(ActionEvent e)
                          this.txtAra.setText("");



                          public TextApp()
                          // Initialize instance fields
                          frame = new JFrame(APP_NAME);
                          txtAra = new JTextArea(5, 30);
                          button = new JButton("Clear Text");

                          // Internal panels used for layout
                          JPanel mainPanel = new JPanel(new GridLayout(1, 1));
                          JScrollPane scrollPane = new JScrollPane(txtAra);
                          JPanel buttonPanel = new JPanel();

                          // Add components to containers
                          frame.setLayout(new BorderLayout());
                          frame.add(mainPanel, BorderLayout.CENTER);
                          frame.add(buttonPanel, BorderLayout.PAGE_END);
                          mainPanel.add(scrollPane);
                          buttonPanel.add(button);

                          // Additional setup
                          scrollPane.setSize(600, 400);
                          scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

                          // Add listeners
                          button.addActionListener(new ClearAction(txtAra));


                          @Override
                          public void run()
                          // Set text
                          txtAra.setText("this is some random textnthat may go for many rowsnso it may look messy");

                          // Prepare frame
                          frame.pack();
                          frame.setLocationRelativeTo(null);
                          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                          frame.setVisible(true);


                          public static void main(String[] args)
                          SwingUtilities.invokeLater(new TextApp());







                          share|improve this answer













                          Here is c0der's implementation, but set-up for a more streamlined life-cycle. Each block of lines explains what is going on and when.



                          import java.awt.*;
                          import java.awt.event.*;
                          import javax.swing.*;
                          import javax.swing.text.JTextComponent;

                          public class TextApp implements Runnable
                          private static final String APP_NAME = "Text App";

                          private JFrame frame;
                          private JTextArea txtAra;
                          private JButton button;

                          // This is a generic action that handles clearing the text of a JTextComponent
                          // It can also be a stand-alone class
                          private static class ClearAction <T extends JTextComponent> implements ActionListener
                          private T txtAra;

                          public ClearAction(T txtAra)
                          this.txtAra = txtAra;


                          @Override
                          public void actionPerformed(ActionEvent e)
                          this.txtAra.setText("");



                          public TextApp()
                          // Initialize instance fields
                          frame = new JFrame(APP_NAME);
                          txtAra = new JTextArea(5, 30);
                          button = new JButton("Clear Text");

                          // Internal panels used for layout
                          JPanel mainPanel = new JPanel(new GridLayout(1, 1));
                          JScrollPane scrollPane = new JScrollPane(txtAra);
                          JPanel buttonPanel = new JPanel();

                          // Add components to containers
                          frame.setLayout(new BorderLayout());
                          frame.add(mainPanel, BorderLayout.CENTER);
                          frame.add(buttonPanel, BorderLayout.PAGE_END);
                          mainPanel.add(scrollPane);
                          buttonPanel.add(button);

                          // Additional setup
                          scrollPane.setSize(600, 400);
                          scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

                          // Add listeners
                          button.addActionListener(new ClearAction(txtAra));


                          @Override
                          public void run()
                          // Set text
                          txtAra.setText("this is some random textnthat may go for many rowsnso it may look messy");

                          // Prepare frame
                          frame.pack();
                          frame.setLocationRelativeTo(null);
                          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                          frame.setVisible(true);


                          public static void main(String[] args)
                          SwingUtilities.invokeLater(new TextApp());








                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 6 at 14:29









                          Mr. PolywhirlMr. Polywhirl

                          17.4k84991




                          17.4k84991



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55024370%2fhow-to-make-user-resizable-jtextarea%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

                              Алба-Юлія

                              Захаров Федір Захарович