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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:01:22+00:00 2026-06-03T16:01:22+00:00

My explanation below rambles, boiling down, is there a way I can add a

  • 0

My explanation below rambles, boiling down, is there a way I can add a Row without firing off an event, such that I can add multiple rows and fire an event to update all of them at once? Without having to add code to contain the table data in the custom model?

I have a custom TableModel which extends from DefaultTableModel so that I can use DefaultTableModel to keep track of data for me, whilst still having some custom methods of my own.

The issue is, I was thinking it might be faster for me to have an “addRows(String[][] val)” method, when I wish to add multiple rows. I could then fire a single event, probably fireTableDataChanged() to update the rows all at once. For example, my current method:

JTable table1 = new JTable(new dgvTableModel(new String[] {<values>},0, new String[] {<values>}));
table1.addRow(new String[] {<values here>});
table1.addRow(new String[] {<values here>});
table1.addRow(new String[] {<values here>});

I would then repeat the above as many times as necessary. The issue is, each of those will fire off a seperate event. It would be much faster (I think), if I could do this using my custom table model:

JTable table1 = new JTable(new dgvTableModel(new String[] {<values>},0, new String[] {<values>}));
table1.addRows(new String[][] {{<values1 here}, {values2 here}, . . .}});

and then in the table model:

public void addRows(String[][] values) {
   for (String[] vals : values)           
       super.addRow(vals);
   }
   fireTableDataChanged();
}

I can code this in easily. The issue is again, that “super.addRow(vals);” line will fire an event each time through. Is there a way, without adding code to have my model contain the table data itself, to prevent that event being fired each time I add a row? Such that it waits for the fireTableDataChanged() call in the addRows method?

For reference, the code for my custom table model:

import java.awt.Color;
import java.util.ArrayList;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;

public class dgvTableModel extends DefaultTableModel {
//private DataTable tableVals = new DataTable();
private ArrayList<Color> rowColors;

//private ArrayList<Object[]> data = new ArrayList<>();
//default constructor has no data to begin with.
private int[] editableColumnNames;

public dgvTableModel(String[] colNames, int rowCount)
{
    super(colNames, rowCount);
}

public dgvTableModel(String[] colNames, int rowCount, String[] editableColNames)
{
    super(colNames, rowCount);
    //this.tableVals.setColNames(colNames);
    if (editableColNames!=null && editableColNames.length >0)
    {
        editableColumnNames = new int[editableColNames.length];
        int count = 0;
        for (int i =0; i< editableColNames.length;i++)
        {
            for (String val : colNames)
            {
                if (val.equalsIgnoreCase(editableColNames[i]))
                {
                    editableColumnNames[count] = i;
                    count+=1;
                    break;
                }
            }
        }
    }
}

public dgvTableModel(String[] colNames, int rowCount, String[] editableColNames, boolean colorChanges)
{
    super(colNames, rowCount);
    Color defColor = UIManager.getDefaults().getColor("Table.background");
    if (editableColNames!=null && editableColNames.length >0)
    {
        editableColumnNames = new int[editableColNames.length];
        int count = 0;
        if (colorChanges)
        {
            rowColors = new ArrayList<>();
        }
        for (int i =0; i< colNames.length;i++)
        {
            if (colorChanges)
            {
                rowColors.add(defColor);
            }

            for (String val : editableColNames)
            {
                if (val.equalsIgnoreCase(colNames[i]))
                {
                    editableColumnNames[count] = i;
                    count+=1;
                    break;
                }
            }
        }
    }
    else if (colorChanges)
    {
        rowColors = new ArrayList<>();
        for (String val : colNames)
        {
            rowColors.add(defColor);
        }
    }
}

@Override
public boolean isCellEditable(int row, int column)
{
    if(editableColumnNames!=null && editableColumnNames.length >0)
    {
        for (int colID : editableColumnNames)
        {
            if (column==colID)
                return true;
        }
    }
    return false;
}

public void setRowColor(int row, Color c)
{
    rowColors.set(row, c);
    fireTableRowsUpdated(row,row);
}

public Color getRowColor(int row)
{
    return rowColors.get(row);
}

@Override
public Class getColumnClass(int column)
{
    return String.class;
}

@Override
public String getValueAt(int row, int column)
{
    return super.getValueAt(row, column).toString();
}
}

Surely firing one event to display every row, is faster than firing one event for each row?

  • 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-03T16:01:24+00:00Added an answer on June 3, 2026 at 4:01 pm

    ‘AbstractTableModel.fireTableDataChanged()’ is used to indicate to the model (and the JTable UI which is notified by the model) that all possible data in the table may have changed and needs to be checked. This can (with emphasis on can) be an expensive operation. If you know which rows have been added, just use the ‘AbstractTableModel.fireTableRowsInserted(int firstRow, int lastRow)’ method instead. This will ensure only the effect rows are seen as changed. Take a look at all the fire* methods in AbstractTableModel. You can really exercise fine grained control over which rows, cells, etc are seen as dirty.

    Then again what your doing might be premature optimalization. Unless you have fiftythousand records in your JTable this is probably not going to be noticable. But if you have a massive amount of records in your JTable you might be beter of lazy loading them anyway.

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

Sidebar

Related Questions

tl;dr; Even without my explanation one can look at the code below and the
Can anyone explain the below code can any one give some real time explanation
(can skip this part just an explanation of the code below. my problems are
How can I redirect in PHP with this setup below without getting header output
Can anybody, given example below, make to me an explanation how FLOAT works in
I'm sure there is a simple explanation why this is happening but can't seem
I have a problem for querying records into mysql explanation below The user could
There's a nice explanation here of how to use ggplot2 to create a scatterplot,
I followed the explanation on the sIFR wiki, but can't seem to get accented
I want to add explanation between cells. I'm experimenting with sections, trying to make

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.