Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8062893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:49:48+00:00 2026-06-05T10:49:48+00:00

I have a JPanel where I add a JScrollPane (see class below). I added

  • 0

I have a JPanel where I add a JScrollPane (see class below). I added a JTextField to verify that everything works (instead of the JScrollPane) and the text field was added to the Panel. When I try to add my JScrollPane nothing (white background) is shown. How can I fix that?

protected JPanel createContentPanel() {
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());

    final String[] columnNames = { "Test1", "Test2", "Test3" };
    final String[][] data = {
            { "foo1", "foo2", "foo3" },
            { "bar1", "bar2", "bar3" },
            { "bar1", "bar2", "bar3" },
            { "bar1", "bar2", "bar3" },
            { "bar1", "bar2", "bar3" },
            { "bar1", "bar2", "bar3" },
            { "bar1", "bar2", "bar3" },
            { "bar1", "bar2", "bar3" }
    };

    ScrollableTable scrollableTablePane = new ScrollableTable(data, columnNames);
    panel.add(scrollableTablePane, BorderLayout.CENTER);
    return panel;
}


public class ScrollableTable extends JScrollPane {

    public ScrollableTable(String[][] aData, String[] aColumnNames) {
        super();

        TableModel model = getTableModel(aData, aColumnNames);
        JTable overviewTable = new JTable(model);
        overviewTable.setGridColor(new Color(240, 240, 240));
        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
        overviewTable.setRowSorter(sorter);

        this.add(overviewTable);
    }

    private TableModel getTableModel(String[][] aData, String[] aColumnNames) {
        TableModel model = new DefaultTableModel(aData, aColumnNames) {
            public Class<?> getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };
        return model;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:49:50+00:00Added an answer on June 5, 2026 at 10:49 am

    To set the displayed component of a JScrollPane, use JScrollPane#setViewportView(Component) rather than add(Component).

    public ScrollableTable(String[][] aData, String[] aColumnNames) {
        super();
    
        TableModel model = getTableModel(aData, aColumnNames);
        JTable overviewTable = new JTable(model);
        overviewTable.setGridColor(new Color(240, 240, 240));
        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
        overviewTable.setRowSorter(sorter);
    
        this.setViewportView(overviewTable);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two JPanels that needs to add a JPanel into it, however only
I have a java class that extends from JPanel. The basic purpose of extending
I have a JScrollPane with a JPanel that implements Scrollable as its viewport view.
I have a JPanel that contains a JScrollPane that contains a JTable. Inside my
I have a JPanel that holds a JScrollPane that holds a JPanel as such:
I have a class which extends a JPanel. Code below: public class Test extends
I have a JFrame with JScrollPane in it. I have JPanel inside a scrollPane.
I have a JPanel that has a button in it. The location of the
I have a JPanel that encapsulates two JPanels, one on top of the other.
I have a JPanel with several levels of child components, also with a JScrollPane.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.