• Post Reply Bookmark Topic Watch Topic
  • New Topic

JScrollPane in a GridBagLayout cell  RSS feed

 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using GridBagLayout. JButton-1 is in cell (0,0) and JButton-2 is in cell (1,0). JTable is in cells (0,1) and (1,1).

When wrap the JTable in a JScrollPane, the layout manager moves the JTable.

Why doesn�t the JTable stay in the second row when I use JScrollPane?
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marlene Miller:

Why doesn�t the JTable stay in the second row when I use JScrollPane?

Because it's the (play Twilight Zone music) GridBagZone.

No, seriously, it's because while you set the grid bag constraints for the JTable, you're not actually putting the JTable in the JPanel; you're actually putting the JScrollPane in the JPanel. Try setting the grid bag constraints on the JScrollPane instead of on the JTable.
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, I just realized that might have been a homework problem, for which we're not supposed to give direct answers. So if it was, please read the following so that you learn something to replace the learning opportunity I accidentally took away from you:

Even though you probably stared at this for a while without seeing it, notice that a second set of eyes spotted it pretty quick (on the first reading of the document, in fact). The lesson to be learned is that code reviews by another programmer are a good way to spot bugs. There, I feel less guilty now.

If you're actually working on production code, you can ignore the above.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if it was a homework assignment, I see nothing wrong with the answer you gave Warren. Marlene obviously tried to solve the problem but was stuck. Now if Marlene had said:

Can someone show me how to put a JTable in a ScrollPane on a JPanel using GridBagLayout? Some source code would be nice.

Then we would have a problem.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>> No, seriously, it's because while you set the grid bag constraints for the JTable, you're not actually putting the JTable in the JPanel; you're actually putting the JScrollPane in the JPanel.

Thank you very much. I understand.

>> Try setting the grid bag constraints on the JScrollPane instead of on the JTable.

Yes, that works.

>> Oops, I just realized that might have been a homework problem, for which we're not supposed to give direct answers.

I�ve been following the posts on the SCJD forum. I have not downloaded an assignment, because I don�t know enough. But I wrote a simple solution, so that I can understand the issues. Now I want to test it. Since I don�t have the database file provided by SUN, I have to make one and fill it. For this purpose, I am writing a utility with a GUI interface. One JTable will be for the metadata, another JTable for the data.

So I guess, in some sense, it is a homework assignment. I am a student without a teacher. It is related to the SCJD assignment, but it seems like a generic utility.

>> So if it was, please read the following so that you learn something to replace the learning opportunity I accidentally took away from you:

>> Even though you probably stared at this for a while without seeing it, notice that a second set of eyes spotted it pretty quick (on the first reading of the document, in fact). The lesson to be learned is that code reviews by another programmer are a good way to spot bugs. There, I feel less guilty now.

Thank you for the advice.

Marlene
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>> Marlene obviously tried to solve the problem but was stuck.

Yes, I was stuck. It's embarassing. The mistake is obvious. I gave up too soon. I decided it was some mysterious property of GridBagLayout.

It looks sort of like a school problem. That's because I extracted the essential parts and reduced them to the smallest thing that doesn't work. But somehow I didn't do/think enough.
[ September 15, 2004: Message edited by: Marlene Miller ]
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marlene Miller:

Yes, I was stuck. It's embarassing. The mistake is obvious.

95% of bugs are, once you find them. My last three bugs all involved typing '>' when I meant '<' or vice versa.

And I'd like to note that your original post stated the problem very clearly, and the code provided was very legible, making it a pleasure to help. I wish everyone asking questions on this site could be so clear.
[ September 15, 2004: Message edited by: Warren Dew ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Warren.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!