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 7669243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:33:04+00:00 2026-05-31T15:33:04+00:00

I have a JTable, associated with a DefaultTableModel, in a JPanel with a SpringLayout

  • 0

I have a JTable, associated with a DefaultTableModel, in a JPanel with a SpringLayout which is in a JScrollPane.

When I modify the structure of the DefaultTableModel with the method below the JTable is refreshed but not the JScrollPane. I have to apply a second time this method to refresh the JScrollPane.

public void updateProjectView() {
    SwingProjectViewerController spvc = SwingProjectViewerController.
            getInstance();
    this.projectTitle.setText(spvc.getAcronym() + " : " + spvc.getTitle());
    Object[][] tableContent =
            spvc.getCriteria(CriteriaPreselectionController.getInstance().
            getCriteriaPreselection());
    Object[] columnsName = new Object[]{"Col1", "Col2"};
    this.tableModel.setDataVector(tableContent, columnsName);
    this.tableModel.fireTableStructureChanged();
} 

Any help much appreciated!

  • 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-05-31T15:33:05+00:00Added an answer on May 31, 2026 at 3:33 pm

    Here is a test program that shows that when a JTable is held by a JScrollPane and the DefaultTableModel’s DataVector is changed via setDataVector(...) it is not necessary to call fireTableDataChanged() on the model for the JTable’s data to change correctly and be viewed correctly.

    The program GUI looks like the images below.
    Before Data Changed:
    enter image description here

    After Data Changed:
    enter image description here

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    
    @SuppressWarnings("serial")
    public class ScrollPaneRefresh extends JPanel {
       private static final int PREF_W = 600;
       private static final int PREF_H = 200;
       private Integer[][] initialData = {
             {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 0},
             {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 0},
             {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 0}
             };
       private Integer[][] newData = {
             {1, 2}, {3, 4}
             };
       private String[] columnNames = {"Col1", "Col2"};
       private TablePanel gregsPanel = new TablePanel("With fireTableDataChanged", initialData, columnNames);
       private TablePanel myPanel = new TablePanel("Without fireTableDataChanged", initialData, columnNames);
    
       public ScrollPaneRefresh() {
          gregsPanel.setButtonAction(new AbstractAction("Change Table Data") {
             private boolean changeToNewData = true;
    
             @Override
             public void actionPerformed(ActionEvent evt) {
                if (changeToNewData) {
                   gregsPanel.setTableModelDataVector(newData, columnNames);
                } else {
                   gregsPanel.setTableModelDataVector(initialData, columnNames);
                }
                gregsPanel.fireTableDataChanged();
                changeToNewData = !changeToNewData;
             }
          });
          myPanel.setButtonAction(new AbstractAction("Change Table Data") {
             private boolean changeToNewData = true;
    
             @Override
             public void actionPerformed(ActionEvent evt) {
                if (changeToNewData) {
                   myPanel.setTableModelDataVector(newData, columnNames);
                } else {
                   myPanel.setTableModelDataVector(initialData, columnNames);
                }
                // myPanel.getScrollPane().getViewport().revalidate();
                changeToNewData = !changeToNewData;
             }
          });
    
          setLayout(new GridLayout(1, 0));
          add(gregsPanel.getMainPanel());
          add(myPanel.getMainPanel());
       }
    
       @Override // so scrollbars will show
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       private static void createAndShowGui() {
          ScrollPaneRefresh mainPanel = new ScrollPaneRefresh();
    
          JFrame frame = new JFrame("ScrollPaneRefresh");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    class TablePanel {
       private JPanel mainPanel = new JPanel();
       private DefaultTableModel dm;
       private JTable table = new JTable();
       private JButton changeTableBtn = new JButton();
       private JScrollPane scrollpane = new JScrollPane(table);
    
       public TablePanel(String title, Object[][] data, Object[] columnNames) {
          dm = new DefaultTableModel(data, columnNames);
          table.setModel(dm);
          JPanel btnPanel = new JPanel();
          btnPanel.add(changeTableBtn);
    
          mainPanel.setBorder(BorderFactory.createTitledBorder(title));
          mainPanel.setLayout(new BorderLayout(5, 5));
          mainPanel.add(scrollpane, BorderLayout.CENTER);
          mainPanel.add(btnPanel, BorderLayout.PAGE_END);
       }
    
       public void setButtonAction(Action action) {
          changeTableBtn.setAction(action);
       }
    
       public void setTableModelDataVector(Object[][] data, Object[] columnNames) {
          dm.setDataVector(data, columnNames);
       }
    
       public void fireTableDataChanged() {
          dm.fireTableDataChanged();
       }
    
       public JScrollPane getScrollPane() {
          return scrollpane;
       }
    
       public JComponent getMainPanel() {
          return mainPanel;
       }
    }
    

    The deficiency of my example above is that it does not reproduce the original poster’s problem, and I think that is due to us use of SpringLayout, which he does not fully describe or show code:

    I have a JTable, associated with a DefaultTableModel, in a JPanel with a SpringLayout which is in a JScrollPane.

    For more and better help, the OP is going to have to create his own sscce similar (or simpler) than what I’ve posted above. Either that, or solve his problem by not using SpringLayout to hold the JTable but rather either add it directly to the JScrollPane’s viewport view or add it to a BorderLayout using JPanel (taking care to also add the JTable’s header) and add that to the JScrollPane’s viewport view.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Jtable on which I called the method table1.setAutoCreateRowSorter(true); . So this
I have a JTable using a custom DefaultTableModel which has some Booleans in the
I have a JTable which is 9 by 3 (27 cells), but if the
I have a JTable with a JScrollPane. There is something i do not understand
I have a JTable for which I have made a table model. But on
I have a JTable inside a JScrollPane. After calling my function which should populate
I have a JTable which is loaded from a data-structure using table model.The data-structure
I have a JTable whose associated TableModel could be initially empty. Therefore, it currently
I have a JTable that is within a JScrollPane. Rows are added to the
I have a JTable inside of a JScrollPane . I am creating a custom

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.