• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wordwrap in GridLayout

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used BorderLayout (amongst others) and it works for me:
 
Sebastiao Fernandes
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic