Results 1 to 7 of 7
  1. #1
    Timmins is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default JTable sizing issues

    my program has a button which should delete a left-to-rght row in a table. however the problem is that JTables are not dynamically sized therefore in order to "update" the table i need to create a new table to match the now smaller size
    the problem is that by creating a new table each time a user is deleted the Jframe is filled with the previous tables
    how can i make my table dynamically sized to match the amount of rows needed?

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    have you tried yourContainer.pack() ?

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    how can i make my table dynamically sized to match the amount of rows needed?
    Why do you want to do that? The normal design approach is to pick a maximum size for the number of rows to display. The table is added to a scrollpane and the scrollpane is added to the frame. Then as you add/remove rows, the scrollbar will appear/disappear.

    Users don't like seeing the size of window continually changing.

  4. #4
    Timmins is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    what i want is to make sure there is no blank space at the bottom of the table.
    container.pack(); isn't working cannot find variable
    and i still need to implement a scrollbar for that table...

    either way i can't figure out a way to change the row size without calling a new table each time

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    container.pack(); isn't working cannot find variable
    Given that you don't know that the pack() method is used on a frame and not a panel, this tells me you are a beginner GUI programmer.

    what i want is to make sure there is no blank space at the bottom of the table.
    As I said, that is NOT the way a UI is designed. Users don't want components shrinking and growing. Create the table at a reasonable size and let the scrollbars to the rest.

    To make the table look better you can try using:

    Java Code:
    table.setFillsViewportHeight(true);
    Now the table will have a white background instead of the gray background.

  6. #6
    Timmins is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    the pack idea was given to me by the above poster but thats besides the point
    i guess i need to implement a jScrollpane to see if that works.
    EDIT: fixed the problem below me
    problem is when ever i try that the JScrollPane(table) is just a sliver in size at the bottom of the frame
    Java Code:
    JScrollPane scrollbar = new JScrollPane( table );
            scrollbar.setPreferredSize(new Dimension(100, 200));
            scrollbar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            scrollbar.setHorizontalScrollBarPolicy(JScrollPane .HORIZONTAL_SCROLLBAR_ALWAYS);
                    
            scrollbar.add(table);
            frame.add(scrollbar, BorderLayout.SOUTH);
    however nothing is appearing within the scrollpane
    Last edited by Timmins; 04-02-2011 at 02:31 AM.

  7. #7
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Recommend you post an SSCCE demonstrating the problem. This help you break the problem down, and help us reproduce the problem at hand to help you. FWIW don't use add() to add components to a JScrollPane - use setViewportView(Component view) if you wish to set the view (the constructor does this by default - so no need for the add call)

    And suggested reading:
    How to Use Scroll Panes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    Last edited by doWhile; 04-02-2011 at 02:43 AM.

Similar Threads

  1. Problem with JScrollPane/client sizing
    By kennyman94 in forum AWT / Swing
    Replies: 1
    Last Post: 03-08-2011, 12:18 AM
  2. Panel sizing
    By sjs in forum AWT / Swing
    Replies: 5
    Last Post: 10-11-2010, 03:13 AM
  3. Replies: 3
    Last Post: 02-06-2010, 03:28 PM
  4. Trouble with Buffer Sizing
    By Jeff in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 01:43 PM
  5. Help needed with sizing components
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 11-20-2007, 04:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •