JScrollPane for JTextBox

I was creating a GUI using Java Swings.

I decided to add a text box with vertical scroll bar to it. I also have few more other components to be added and my text box with scroll bar comes after some of them.

So I started adding one by one,

panel.add(dateLabel);
panel.add(dateTextbox);
…..

//now the turn of description text box

panel.add(descriptionTextbox);
panel.add(scrollbarForDescriptionTextbox);
……

I compiled and ran the program. The output was weird.The descriptionTextbox is coming without scrollbar and as I continue hitting ENTER in it, it is expanding and exceeding even the frame size.And my poor scrollbarForDescriptionTextbox is lying idly next to the descriptionTextbox .

I was surprised why it is happening like this and kept looking at the code over and over again. I spent sometime even yesterday night before going to bed.But I couldn’t get it.

Today morning at 5:30 I just opened my laptop to see it again. I found the mistake I did in less than 2 minutes.

Here is the catch:

Initially I said ,

JScrollPane scrollbarForDescriptionTextbox = new JScrollPane(descriptionTextbox);

now that I created scroll bar for descriptionTextbox,I should only say

panel.add(scrollbarForDescriptionTextbox);

instead, I said

panel.add(descriptionTextbox);
panel.add(scrollbarForDescriptionTextbox);

which is a blooper! Though its OK for compiler, I didn’t get my expected output.

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s