File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Wordwrap in GridLayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Wordwrap in GridLayout" Watch "Wordwrap in GridLayout" New topic
Author

Wordwrap in GridLayout

Sebastiao Fernandes
Greenhorn

Joined: Aug 27, 2009
Posts: 13
Hi all,

(This may seem like an earlier question that I asked but it's not quite).

I need some help with word wrapping.

I am building a DB app in NetBeans v.6.5. I'm using the GUI designer for the most part and coding around it.

Here's what I'm trying to accomplish:

The user submits a school course's id. The DB is queried for the multiple choice test for that course and presents it to the user.
A test is composed of a series of questions (JTextArea) and their choices (JRadioButton). Not all questions have the same amount of questions and, of course, some questions are longer than others. Each question/choices group is in their own JPanel and all of them are in a single JPanel.

The length of the question is where I'm having the trouble. Right now I am placing the question inside a JTextArea. if the question is too long it word wraps (using JTextArea.LineWrap() and JTextArea.setWrapStyleWord()) but, the vertical size of the JTextArea doesn't change to make the subsequent lines visible.

I suspect GridLayout to be the culprit here. Since GridLayout makes all the cells equal in size I can't have the JTextArea displaying larger than the JRadioButtons. Is this correct?

With that in mind I've moved to GridBagLayout. Here I can have the first row (JTextArea) larger than the subsequent ones (JRadioButton) if necessary.

I'm having trouble with that also. Now what is happening is that, within each question/choices JPanel, everything is centered within the JPanel, rather than starting at the very end (left) of the JPanel. I've used GridBagConstraint.gridx to place things at 0, and I've used GridBagConstraint.fill = GridBagConstraint.HORIZONTAL, but that's still centering them.

I've also noticed that, even though the long questions are word-wrapping now, the JTextArea is still not sizing properly to accommodate the entire question.

Here's the relevant code:


I've been wrestling with this for some time now and I'm hoping I'm nearly there. I could really use some assistance with the JTextArea word-wrapping (either in GridLayout or GridBagLayout). Any help is greatly appreciated.

Thanks!
Sebastiao Fernandes
Greenhorn

Joined: Aug 27, 2009
Posts: 13
Nothing like solving it yourself.

The issue I was having with the alignment was GridBagConstraints.weightx

So, now I have all my components (JTextArea and JRadioButton) aligning to the left of JPanel.

I'm still having some trouble with the amount of vertical space that the JTextArea is taking. I'm still having some questions being cut off.

Any suggestions on what needs tweaking?

Thanks!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
For me and many others here, I find it much easier to figure out how to fix if I can work with a small program that compiles, runs and demonstrates the problem. Sometimes when I have the time, I create one of these myself, but it is always much easier if the OP does this part for me.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2153
    
    7
I suspect GridLayout to be the culprit here. Since GridLayout makes all the cells equal in size I can't have the JTextArea displaying larger than the JRadioButtons. Is this correct?

With that in mind I've moved to GridBagLayout


You are not forced to use a single layout manager. The easiest solution is usually a combination of panels with different layout managers to get the desired layout.
Sebastiao Fernandes
Greenhorn

Joined: Aug 27, 2009
Posts: 13
Ok.... I've put together a mock-up that demonstrates what's happening:



Where the problem is occurring is on question 8. It looks to me like the question is too long and there are too many choices so that things are being obscured. I'm not sure what to set and where to set it.

Thanks!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Ok.... I've put together a mock-up that demonstrates what's happening:

just so you understand...

the code you posted:

at the top of the code block, click 'view plain'
when the new window opens, click anywhere in the code and ctrl_a, then ctrl_c

now open another IDE (not netbeans), and paste the code (no package), save.
now try to compile it from the pasted code (40, 60, 80 errors?)

that is what you are presenting to anyone trying to help you.

so, if you really want a solution, it's in your interest to post painless code.
Sebastiao Fernandes
Greenhorn

Joined: Aug 27, 2009
Posts: 13
Yargh, sorry about that.

I've been trying to correct this. I'm finding two common errors on compilation:

Main.java:146: cannot find symbol
symbol : class GroupLayout
location: package org.jdesktop.layout
.add(titlePanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
^
Main.java:145: cannot find symbol
symbol : class LayoutStyle
location: package org.jdesktop.layout
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)


Some hunting revealed that GroupLayout and LayoutStyle are a part of javax.swing (I do not know what org.jdesktop.layout is, I assume some NetBeans thing).

At any rate, I got the same error when when I replaced org.jdesktop.layout with javax.swing

Other parts of the code uses javax.swing so I'm sure that it's not a CLASSPATH problem. But I have noticed that these classes are in Java SE 6. I don't know which version I am using and I'm not sure how to find out either.

Any suggestions on how to correct this so that I can back to my original problem?

Thanks for your patience and continued help!
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2153
    
    7
Any suggestions on how to correct this so that I can back to my original problem?

Learn how to use layout managers and write the code yourself so you understand whats going on and are not depenent on the code generated by the IDE.

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.
Sebastiao Fernandes
Greenhorn

Joined: Aug 27, 2009
Posts: 13
OK,

I did some reading and replaced the NetBeans-generated code with BorderLayout. The code compiles and runs on my command line (Mac, OS 10.4, Tiger) and displays the problem.

To refresh, the problem happening here is seen on question 8. Here, the length of the question and the number of options is causing part of the question to be obscured.

Here's the code:



I've tried some fiddling with the constraints in GridBagLayout but haven't had much success. I believe the JPanels need to be re-sized (rather than the JTextAreas)
based on the question length and/or choice count.

Any suggestions?

Thanks!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
I used BorderLayout (amongst others) and it works for me:
Sebastiao Fernandes
Greenhorn

Joined: Aug 27, 2009
Posts: 13
Hey Pete, thanks for the suggestions.

I employed your suggestions, using combination of Box/Border/Grid Layouts an everything is displaying properly.

I noticed that, even after a full change to those layout managers, I was still having panels/textareas not sizing properly.

The issue here was on lines 117/118 of my code, specifically

These two lines were, apparently, improperly sizing the components as well as autoscrolling down to the bottom of the scrollpane. Removing these lines fixed everything.

With that in mind, I don't believe there was anything wrong with using GridBagLayout, or even NetBean's GUI builder, as I had added those lines myself, though Pete's suggestion certainly is clearer and less complex than using GBL. I haven't bothered verifying that my old methods work since I like the elegance and simplicity of this method.

Thanks again!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wordwrap in GridLayout
 
Similar Threads
Trouble with repainting Jframe (i think)
JTextArea problems in GridbagLayout
Proper sizing for dynamic components
Swing, ActionEvent can't see button...
problem in rediredtin GUI to IO Streams