Results 1 to 5 of 5
  1. #1
    kbro3 is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default Adding JTable to empty JScrollPane

    Hi All,

    Having an issue, where I'm unable to add a table to a scrollpane if the scrollpane is already displayed.

    Basically I have a search dialog which has an empty scrollpane, and I want to populate it with a Jtable of results once search is completed.

    A snippet (example data, columns):

    Java Code:
    String[][] data = {{"aaa"}, {"bbb}};
    String[] col = {"col1", "col2"};
    DefaultTableModel model = new DefaultTableModel(data,col);
    
    tableView = new JTable(model);
    
    tableScroller.add(tableView); //JScrollPane, already instantiated
    tableView.setFillsViewportHeight(true);
    I feel like i'm missing something simple....


    Thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You don't add a component to the JScrollPane itself but rather you add it to the JScrollPane's JViewport. So try instead:

    Java Code:
    tableScroller.getViewport().add(tableView);

    And let us know if this helps.

  3. #3
    kbro3 is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default

    Worked like a charm, thanks Fubarable. I'm curious to know, when instantiating a scrollpane, you can add directly to it. Is it because the constructor scrollpanel constructor can take a table instance as a parameter and add it directly to viewport?

    Thanks again!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by kbro3 View Post
    Worked like a charm, thanks Fubarable. I'm curious to know, when instantiating a scrollpane, you can add directly to it. Is it because the constructor scrollpanel constructor can take a table instance as a parameter and add it directly to viewport?

    Thanks again!
    Yes. The constructor will automatically add the component to the viewport.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    And then there's JScrollPane#setViewportView (and companion methods setRowHeaderView / setColumnHeaderView)

    db

Similar Threads

  1. Adding jscrollpane when extending jpanel
    By phil128 in forum AWT / Swing
    Replies: 4
    Last Post: 01-18-2011, 11:30 PM
  2. Help adding JScrollBar/JScrollPane to JTextPane
    By crikey in forum AWT / Swing
    Replies: 7
    Last Post: 08-30-2010, 12:37 AM
  3. Problem with updating empty JTable
    By byubi in forum AWT / Swing
    Replies: 1
    Last Post: 05-15-2010, 08:31 AM
  4. how to add JScrollPane in JTable
    By kiki2009 in forum AWT / Swing
    Replies: 12
    Last Post: 03-31-2010, 01:54 PM
  5. Write empty Jtable columns to disk
    By ocean in forum New To Java
    Replies: 9
    Last Post: 12-03-2009, 03:02 PM

Posting Permissions

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