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

  • SEARCH
  • Home
  • 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 7879277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:51:56+00:00 2026-06-03T03:51:56+00:00

So I have a JTable inside a JScrollPane inside a frame that works. What

  • 0

So I have a JTable inside a JScrollPane inside a frame that works. What I want to do is to change the table that is being showed to another table that displays some other information and has more/less columns/rows. How do I do this?

I have tried stuff like putting another table in the ScrollPane but that doesn’t work and if I re-put the ScrollPane in the frame it disappears.

EDIT:

I think this is all the code that has to do with this problem. I simply call the funktions to change the view.

    Object[][] userData = new Object[50][6];
    userTable = new JTable(userData, new String[] { "Namn", "Adress", 
            "Telefon", "Personnummer", "PIN", "Antal cyklar" }) {
        public boolean isCellEditable(int rowIndex, int colIndex) {
            return false;
        }
    };
    userTable.setSelectionMode(0);
    userTable.getTableHeader().setReorderingAllowed(false);
    Object[][] bikeData = new Object[50][7];
    bikeTable = new JTable(bikeData, new String[] { "Ägare", "Streckkod", 
            "Färg", "Märke", "Ram-nummer", "Senast hämtad", "Senast lämnad" }) {
        public boolean isCellEditable(int rowIndex, int colIndex) {
            return false;
        }
    };
    bikeTable.setSelectionMode(0);
    bikeTable.getTableHeader().setReorderingAllowed(false);
    JScrollPane tablePane = new JScrollPane(bikeTable);
    frame.add(tablePane);

    frame.setVisible(true);
}

public void displayUsers(){
    tablePane.setViewportView(userTable);
}

public void displayBikes(){
    tablePane.setViewportView(bikeTable);
}
  • 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-03T03:51:57+00:00Added an answer on June 3, 2026 at 3:51 am

    Have you tried this:

    scrollPane.setViewportView(yourNewTable);
    

    This should replace the component displayed in the scroll pane. Of course you can always change the model of your JTable and its columns but that would not be my preference.

    EDIT: Here is a snippet demonstrating this:

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Vector;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.event.TableModelListener;
    import javax.swing.table.TableModel;
    
    public class Test {
    
        public static void main(String... args) {
            JFrame frame = new JFrame();
            final Vector<String> string = new Vector<String>();
            final JTable table = new JTable(getTableModel("First", 1));
            final JTable table2 = new JTable(getTableModel("Second", 3));
            final JButton click = new JButton("Click me");
            final JScrollPane scrollPane = new JScrollPane(table);
            click.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (table2.getParent() == null) {
                        scrollPane.setViewportView(table2);
                    } else {
                        scrollPane.setViewportView(table);
                    }
                }
            });
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(click, BorderLayout.EAST);
            panel.add(scrollPane);
            frame.getContentPane().add(panel);
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setVisible(true);
        }
    
        private static TableModel getTableModel(final String prefix, final int colCount) {
            return new TableModel() {
    
                @Override
                public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void removeTableModelListener(TableModelListener l) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    // TODO Auto-generated method stub
                    return false;
                }
    
                @Override
                public Object getValueAt(int rowIndex, int columnIndex) {
                    return prefix + "Hello cell (" + rowIndex + "," + columnIndex + ")";
                }
    
                @Override
                public int getRowCount() {
                    return 30;
                }
    
                @Override
                public String getColumnName(int columnIndex) {
                    return "Column " + columnIndex;
                }
    
                @Override
                public int getColumnCount() {
                    return colCount;
                }
    
                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return String.class;
                }
    
                @Override
                public void addTableModelListener(TableModelListener l) {
                    // TODO Auto-generated method stub
    
                }
            };
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JTable that I want to use to display some data (a
I have a JPanel that contains a JScrollPane that contains a JTable. Inside my
I have a JTable inside a JScrollPane. After calling my function which should populate
I have a JTable inside a JScrollPane. In one of the columns in the
I have a JTable inside of a JScrollPane . I am creating a custom
So I have a bunch of JTable s. Each JTable is inside a JScrollPane
I have the following problem: I have a JTable inside a JScrollPane, and the
If I have a jTable ( inside a jScrollPane ) with 1000+ columns. Is
I have a jTable which currently displays and allows editing of a database table,
I have a JTable inside a JScrollPane; How can I get the JTable? Thanks

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.