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
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
add a comment |
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
add a comment |
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
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
java swing user-interface jtextarea
edited Mar 6 at 16:14
Markus Steppberger
258113
258113
asked Mar 6 at 13:33
BobbyBobby
178111
178111
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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);
add a comment |
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);
Up voted for "Setting scroll pane size is not necessary". I missed that one.
– c0der
Mar 6 at 14:52
add a comment |
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());
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
add a comment |
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);
add a comment |
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);
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);
edited Mar 6 at 14:00
answered Mar 6 at 13:50
c0derc0der
9,13151846
9,13151846
add a comment |
add a comment |
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);
Up voted for "Setting scroll pane size is not necessary". I missed that one.
– c0der
Mar 6 at 14:52
add a comment |
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);
Up voted for "Setting scroll pane size is not necessary". I missed that one.
– c0der
Mar 6 at 14:52
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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());
add a comment |
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());
add a comment |
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());
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());
answered Mar 6 at 14:29
Mr. PolywhirlMr. Polywhirl
17.4k84991
17.4k84991
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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