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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:50:00+00:00 2026-05-14T00:50:00+00:00

I have an empty JTable, absolutely nothing in it. I need to dynamically generate

  • 0

I have an empty JTable, absolutely nothing in it. I need to dynamically generate its table columns in a certain way. A simplified version for the code I have for my attempt:

@Action
public void AddCol() {
    for (int i = 0; i < 10; i++) {
        TableColumn c = new TableColumn(i);
        c.setHeaderValue(getColNam(i));
        table.getColumnModel().addColumn(c);
    }
}

But I’m getting an

Exception in thread “AWT-EventQueue-0” java.lang.ArrayIndexOutOfBoundsException: 0 >= 0

What am I doing wrong?

Here’s the complete stacktrace if it helps:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
        at java.util.Vector.elementAt(Vector.java:427)
        at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:632)
        at engine.Processor$UpdateTable.run(Processor.java:131)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
  • 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-14T00:50:01+00:00Added an answer on May 14, 2026 at 12:50 am

    I think you need to add the column to your table’s data model as well as its column model. The column model is updated when the data model changes so changing the data model should be sufficient. Here is an example:

    public class TableColumnAdd {
        private static DefaultTableModel tableModel;
        private static int columnNumber = 1;
    
        public static void main(String[] args) throws InterruptedException, InvocationTargetException {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    tableModel = new DefaultTableModel(new Object[] { "Initial Column" }, 5);
                    JTable table = new JTable(tableModel);
                    JFrame frame = new JFrame("Table Column Add");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setBounds(100, 100, 600, 300);
                    frame.add(new JScrollPane(table));
                    frame.setVisible(true);
                }
            });
    
            for (;;) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        tableModel.addColumn("Column #" + columnNumber++);
                    }
                });
                Thread.sleep(2000);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have dragged a empty asp.net table onto my webform. I generate all the
I have a MySQL table which has about 30 columns. One column has empty
I have a table and the columns on this table contains empty spaces for
I have a JTable whose associated TableModel could be initially empty. Therefore, it currently
I've got a set of database results where some of the columns have empty
I have this JTable having an AbstractTableModel as its model. The initial contents are
If interfaces have empty methods (implicit abstract method) then what is its use? Why
i need your help with an regex in Javascript, again. Sometimes i have empty
I want to bind mysql table content with JTable . I have connected sucessfully.
I have empty TableModel. When I set this model to JTable it hasn't rows.

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.