JScrollPane for JTextBox

I was cre­at­ing a GUI using Java Swings.

I decid­ed to add a text box with ver­ti­cal scroll bar to it. I also have few more oth­er com­po­nents to be added and my text box with scroll bar comes after some of them.

So I start­ed adding one by one,

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

//now the turn of descrip­tion text box

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

I com­piled and ran the pro­gram. The out­put was weird.The descrip­tion­Textbox is com­ing with­out scroll­bar and as I con­tin­ue hit­ting ENTER in it, it is expand­ing and exceed­ing even the frame size.And my poor scroll­bar­ForDe­scrip­tion­Textbox is lying idly next to the descrip­tion­Textbox .

I was sur­prised why it is hap­pen­ing like this and kept look­ing at the code over and over again. I spent some­time even yes­ter­day night before going to bed.But I could­n’t get it.

Today morn­ing at 5:30 I just opened my lap­top to see it again. I found the mis­take I did in less than 2 min­utes.

Here is the catch:

Ini­tial­ly I said ,

JScroll­Pane scroll­bar­ForDe­scrip­tion­Textbox = new JScroll­Pane(descrip­tion­Textbox);

now that I cre­at­ed scroll bar for descrip­tion­Textbox,I should only say

panel.add(scrollbarForDescriptionTextbox);

instead, I said

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

which is a bloop­er! Though its OK for com­pil­er, I did­n’t get my expect­ed out­put.


Comments

Leave a Reply

%d