I think you need to understand how GridLayout
works...
stackpanel.setLayout(new GridLayout(100, height));
Basic says, create me a grid which has 100 rows and height
number of columns...This is reserving space for each cell...
If I change it to...
stackpanel.setLayout(new GridLayout(code.length, 1));
I get...
But I'm only guess at what it is you're trying to achieve...
You should also avoid using setPreferredSize
where you can