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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:00:24+00:00 2026-06-15T11:00:24+00:00

I am just beginning to program using Java Swing. I am not very familiar

  • 0

I am just beginning to program using Java Swing. I am not very familiar with each of the many Java Swing Classes, and have ran into a stumbling block regarding the creation of a Table.

I am trying to figure out how to add a table to a Java Swing Program I am making. I have looked at docs.oracle.com to try and figure it out but I am getting an error when I try to access the values of the table. I will include my code to help show what I am currently trying to do and where the error is.

This is my code for making the table and the table model (I think the problem is that my table model is not working correctly):

        /*Create the Table Entry*/
        columnNames = new String[listoffields.size()+1];
        columnNames[0] = "Record Number";
        for(int a = 1; a < columnNames.length;a++){
            columnNames[a] = listoffields.get(a-1).getTitle();
        }
        int count = 1;
        data = new Object[numrecords][listoffields.size()+1];
        for(int a = 0; a < numrecords;a++){
            for(int b = 0; b < listoffields.size()+1;b++){
                if(b == 0){
                    data[a][b] = count;
                    count++;
                }
                else{
                    data[a][b] = "";
                }
            }
        }

        /* create the table */
        JTable table = new JTable(data,columnNames);
        table.setCellSelectionEnabled(true);
        table.setModel(new AbstractTableModel() {
            public String getColumnName(int col) {
                return columnNames[col].toString();
            }
            public int getRowCount() { return data.length; }
            public int getColumnCount() { return columnNames.length; }
            public Object getValueAt(int row, int col) {
                return data[row][col];
            }
            public boolean isCellEditable(int row, int col) {
                return (col >= 1);
            }
            public void setValueAt(Object value, int row, int col) {
                data[row][col] = value;
                fireTableCellUpdated(row, col);
            }
        });
        JScrollPane scrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);

        /* addthe table to the JTable tableentry*/
        tabelentry.setLayout(new BoxLayout(ProjectFrame.tabelentry, BoxLayout.Y_AXIS));
        tabelentry.add(scrollPane);
        tabelentry.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

After the table is displayed and I edit the blank values for each cell, I click a submit button that submits the cell. However, the following error is encountered:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
   at java.util.Vector.elementAt(Unknown Source)
   at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)
   at Client.GUIComponents.ProjectFrame$1.submit(NameOfFile.java:LineNumber)

The error is encountered when trying to submit as follows:

public void submit(){
   String recordvalues = ""; //contains a String representation of what I want to submit
   for(int a = 0; a < numberoffields;a++){
      for(int b = 0; b < numberofrecords;b++){
         if(a != 0){ //I want to skip the first column
            recordvalues = recordvalues  + (String) tabelentry.getModel().getValueAt(b, a) + ","; //The error is at this line in the code
         }
      }
   }
}

I provided my code to give an example of the current problems I am encountering and what I have currently tried. To generalize my question, I am wondering how to correctly write a table model class so that I can access the values at each of the cells in the table. Any assistance would be helpful. Thanks a ton!

  • 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-15T11:00:24+00:00Added an answer on June 15, 2026 at 11:00 am

    My immediate thought is that you’re creating two different table models.

    JTable table = new JTable(data,columnNames);
    table.setCellSelectionEnabled(true);
    table.setModel(new AbstractTableModel() {...
    

    new JTable(data,columnNames) is actually creating it’s own DefaultTableModel based on the information you are providing it. Try removing the table.setModel(new AbstractTableModel() {... code

    The other, associated, problem is, I don’t know how data and columnNames are declared.

    Have a read through How to Use Tables for more details

    Updated

    The other problem, as pointed about by Hovercraft, is you adding one table to another and then accessing the wrong model.

    The table creation should look more like…

    tableEntry = new JTable(data,columnNames);
    JScrollPane scrollPane = new JScrollPane(tableEntry );
    tableEntry .setFillsViewportHeight(true);
    // Don't forget to add the scroll pane to you view !!
    

    Then you submit method should look more like…

    public void submit(){
        String recordvalues = ""; //contains a String representation of what I want to submit
        TableModel model = tableEntry.getModel();
        for(int a = 0; a < model.getColumnCount();a++){
            for(int b = 0; b < model.getRowCount();b++){
                if(a != 0){ //I want to skip the first column
                    recordvalues = recordvalues  + (String) model.getValueAt(b, a) + ","; //The error is at this line in the code
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just beginning my journey into web development and I have a very
I am just beginning to program in Java and I noticed that String is
I am just beginning to get into desktop application development, and have chosen C#
I am just beginning to work with Rails. I have a model, User and
I am just beginning to set up a Continuous Integration Server using CruiseControl.Net. To
Possible Duplicate: How can I lock a file using java (if possible) I have
So, I'm just beginning C# today in hopes of creating a screen shot program.
I am just beginning to try to learn C and have been trying to
I'm just beginning to learn about file compression and I've run into a bit
I am just beginning to localize an ASP.NET MVC application. Most of the strings

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.