Solved

JTextArea resizes if setLineWrap() is set

Posted on 2002-07-30
6
1,093 Views
Last Modified: 2011-09-20
Wonder if someone could help me folks....

I have a JTextArea control that is inside a JScrollPane.  The JScrollPane control is inside a GridBagLayout panel.

What I want the control to support is word and line wrapping so I've set both properties as follows:

myTextArea.setLineWrap(true);
myTextArea.setWrapStyleWorld(true);

The problem I have is when the app is run, the JTextArea control quickly resizes itself just after its displayed.  This is annoying and something that I cannot have happen.

The constraints I've set and are required for the JSrollPane are:

GridBagConstraints.BOTH

Can someone help?  Thanks.
0
Comment
Question by:carlpaddick
6 Comments
 
LVL 3

Accepted Solution

by:
randyd earned 100 total points
ID: 7188807
you can mess with the layout manager and/or set the max size and preferred size of the component.

for instance, try to set the max size and see what happens when the grid bag compoenent is resized.  then set the gridbag to borderlayout and see the difference.

randy
0
 
LVL 92

Expert Comment

by:objects
ID: 7189313
Put your scrollpane in a borderlayout instead of gbl.
0
 
LVL 9

Expert Comment

by:yongsing
ID: 7189477
Put the JTextArea in a JPanel:

JPanel panel = new JPanel();
panel.add(myTextArea);

Then put the JPanel in the JScrollPane:

JScrollPane scrollPane = new JScrollPane(panel);

And finally add the JScrollPane to the panel which has a GridBagLayout.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 26

Expert Comment

by:ksivananth
ID: 7189759
Try by setting only the following,

    JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane( taNotes, JScrollPane.VERTICAL_SCROLLBAR_ASNEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );

textArea..setLineWrap( true );
scrollPane.setPreferredSize( new Dimension( 100, 70 ) );
0
 

Author Comment

by:carlpaddick
ID: 7190081
Guys,

Thanks for all your answers. Apologies for the delay with the reply.  I'm ahead of your time zones and had gone home from work yesterday.  I'm on leave today and tomorrow so when I get back on Friday, I'll try some of your suggestions out and let you know what works.

Thanks once again
0
 

Author Comment

by:carlpaddick
ID: 7196108
Randyd was the first to answer the question and his answer works perfectly:

myTextAreaScrollPane.setPreferredSize(new Dimension(x,y));

I therefore award him the points but my thanks go to the rest of you guys for helping.  You're all really helping my knowledge with Java language.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now