Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / June 2005

Tip: Looking for answers? Try searching our database.

JScollPane won't resise with GridBagLout

Thread view: 
Ben Kaufman - 08 Jun 2005 02:49 GMT
Is there a trick to get a JScrollPane (which holds a Jtable) to resize when the
window enclosing JFrame, using GridBagLayout  is narrowed?  It works fine with
default JFrame  layout manager (sliders come on scrollpane, narrows etc) but
with GridBag,  it freaks and the contents become unintelligible. I believe that
I have tried almost every variation of GridBag option but to no avail.

I'm doing this in netbeans but the produced code leads me to believe this is not
a netbeans issue.

Ben
Andrew Thompson - 08 Jun 2005 05:24 GMT
> I'm doing this in netbeans but the produced code leads me to believe this is not
> a netbeans issue.

Why not produce an SSCCE that proves it?
<http://www.physci.org/codes/sscce.jsp>

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Ben Kaufman - 08 Jun 2005 14:00 GMT
>> I'm doing this in netbeans but the produced code leads me to believe this is not
>> a netbeans issue.
>
>Why not produce an SSCCE that proves it?
><http://www.physci.org/codes/sscce.jsp>

Here it is.

Ben

public class ScrollTest extends javax.swing.JFrame {
   
   public ScrollTest()
   {

       java.awt.GridBagConstraints gridBagConstraints;

       jScrollPane1 = new javax.swing.JScrollPane();
       jTable1 = new javax.swing.JTable();

       getContentPane().setLayout(new java.awt.GridBagLayout());

       setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
       jTable1.setModel(new javax.swing.table.DefaultTableModel(
           new Object [][] {
               {null, null, null, null},
               {null, null, null, null},
               {null, null, null, null},
               {null, null, null, null}
           },
           new String [] {
               "Title 1", "Title 2", "Title 3", "Title 4"
           }
       ));
       jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
       jScrollPane1.setViewportView(jTable1);

       gridBagConstraints = new java.awt.GridBagConstraints();
       gridBagConstraints.gridx = 0;
       gridBagConstraints.gridy = 0;
       gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
       gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
       gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
       getContentPane().add(jScrollPane1, gridBagConstraints);

       pack();
   }

   public static void main(String args[]) {
       java.awt.EventQueue.invokeLater(new Runnable() {
           public void run() {
               new ScrollTest().setVisible(true);
           }
       });
   }
   
                 
   private javax.swing.JScrollPane jScrollPane1;
   private javax.swing.JTable jTable1;    
}
Andrew Thompson - 08 Jun 2005 15:03 GMT
>>> I'm doing this in netbeans but the produced code leads me to believe this is not
>>> a netbeans issue.
[quoted text clipped - 3 lines]
>
> Here it is.

OK.. here are my experiments..

<sscce>
public class ScrollTest extends javax.swing.JFrame {

 public ScrollTest()
 {
   jTable1 = new javax.swing.JTable();

   setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
   jTable1.setModel(new javax.swing.table.DefaultTableModel(
     new Object [][] {
       {null, null, null, null},
       {null, null, null, null},
       {null, null, null, null},
       {null, null, null, null}
     },
     new String [] {
       "Title 1", "Title 2", "Title 3", "Title 4"
     }
   ));
   jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);

   // The simple explanation?  If the contained components return
   // no sensible minimum size, GBL 'freaks'.  For the complicated
   // explanation, ask someone else, I rarely deal with GBL,
   //  ..for good reason!
   jScrollPane1 = new javax.swing.JScrollPane( jTable1 ) {
                public java.awt.Dimension getMinimumSize() {
                    return new java.awt.Dimension(200, 80);
                }
            };

   java.awt.GridBagConstraints gridBagConstraints;

   gridBagConstraints = new java.awt.GridBagConstraints();
   gridBagConstraints.gridx = 0;
   gridBagConstraints.gridy = 0;
   gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
   gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
   gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;

   getContentPane().setLayout(new java.awt.GridBagLayout());
   getContentPane().add(jScrollPane1, gridBagConstraints);

   pack();
 }

 public static void main(String args[]) {
   java.awt.EventQueue.invokeLater(new Runnable() {
     public void run() {
       new ScrollTest().setVisible(true);
     }
   });
 }

 private javax.swing.JScrollPane jScrollPane1;
 private javax.swing.JTable jTable1;
}
</sscce>

HTH

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Rogan Dawes - 09 Jun 2005 14:20 GMT
> OK.. here are my experiments..
[snip]

>     // The simple explanation?  If the contained components return
>     // no sensible minimum size, GBL 'freaks'.  For the complicated
[quoted text clipped - 5 lines]
>                 }
>             };

Is this a good way of setting the Minimum Size? How about a simple
jScrollPane1.setMinimumSize(new Dimension(200,80));

Seems unnecessary to override a getter, when there is a simple setter
which can be used?

Rogan
Signature

Rogan Dawes

*ALL* messages to discard@dawes.za.net will be dropped, and added
to my blacklist. Please respond to "nntp AT dawes DOT za DOT net"

Andrew Thompson - 09 Jun 2005 14:49 GMT
...
> Is this a good way of setting the Minimum Size?

No, it was merely intended as a lead-in to further discussions.

(looks around at lack of further discussion on the OP's part)
Oh well.. ;)

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Ben Kaufman - 09 Jun 2005 19:30 GMT
>...
>> Is this a good way of setting the Minimum Size?
[quoted text clipped - 3 lines]
>(looks around at lack of further discussion on the OP's part)
>Oh well.. ;)

You must have missed the messages  where Thomas pointed out the problem and I
thanked him.

Ben
Thomas Fritsch - 08 Jun 2005 15:44 GMT
>>>I'm doing this in netbeans but the produced code leads me to believe this is not
>>>a netbeans issue.
[quoted text clipped - 38 lines]
>         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
>         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
          // Set weightx and weighty to arbitrary positive values:
          gridBagConstraints.weightx = 0.1;
          gridBagConstraints.weighty = 0.1;
          // See the javadoc of GridBagContraints#weightx and
          // ...#weight for the reason why.
          // (Blame Sun for choosing 0.0 as the default weightx/y ;-) )

>         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
>         getContentPane().add(jScrollPane1, gridBagConstraints);
[quoted text clipped - 14 lines]
>     private javax.swing.JTable jTable1;    
> }

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

Ben Kaufman - 09 Jun 2005 03:46 GMT
>>>>I'm doing this in netbeans but the produced code leads me to believe this is not
>>>>a netbeans issue.
[quoted text clipped - 63 lines]
>>     private javax.swing.JTable jTable1;    
>> }

Thank you Thomas, that worked!

Ben


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.