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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:08:19+00:00 2026-06-02T07:08:19+00:00

I’m reading in a file and want to output it to a jTable for

  • 0

I’m reading in a file and want to output it to a jTable for viewing and editing. When I try to add rows the the DefaultTableModel the model is always empty for some reason. Any help/direction would be greatly appreciated. Thanks!

public class ReadFileGUI extends javax.swing.JFrame {

public static String file;
private DefaultTableModel model = new DefaultTableModel();

public ReadFileGUI() {
    initComponents();
}

 @SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jButton1 = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTable1 = new javax.swing.JTable(model);

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);


    jButton1.setText("Test");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jScrollPane1.setViewportView(jTable1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(14, 14, 14)
                    .addComponent(jButton1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGap(0, 0, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 397, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton1))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

        String line = "Hello~There~This~Is~A~Test";
        String datavalue[] = line.split("~");                            
        Vector v = new Vector(Arrays.asList(datavalue));

        model.addRow(v);                  
        jTable1.tableChanged(new TableModelEvent(model));   
}

public static void main(String args[]) {

    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(ReadFileGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(ReadFileGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(ReadFileGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(ReadFileGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new ReadFileGUI().setVisible(true);
        }
    });
}    

private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;

}

  • 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-02T07:08:20+00:00Added an answer on June 2, 2026 at 7:08 am

    Here an updated version of your code which seems to work

    import javax.swing.event.TableModelEvent;
    import javax.swing.table.DefaultTableModel;
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Arrays;
    import java.util.Vector;
    
    public class ReadFileGUI extends javax.swing.JFrame {
      private DefaultTableModel tableModel = new DefaultTableModel();
      private javax.swing.JButton populateTableButton;
      private javax.swing.JScrollPane tableScrollPane;
      private javax.swing.JTable table;
    
      public ReadFileGUI() {
        initComponents();
        setDefaultCloseOperation( javax.swing.WindowConstants.EXIT_ON_CLOSE);
      }
    
      @SuppressWarnings("unchecked")
      private void initComponents() {
        populateTableButton = new javax.swing.JButton();
        tableScrollPane = new javax.swing.JScrollPane();
        table = new javax.swing.JTable( tableModel );
    
        populateTableButton.setText( "Test" );
        populateTableButton.addActionListener( new ActionListener() {
          @Override
          public void actionPerformed( ActionEvent evt ) {
            populateTable();
          }
        } );
    
        tableScrollPane.setViewportView( table );
    
        getContentPane().setLayout( new BorderLayout(  ) );
        getContentPane().add( tableScrollPane, BorderLayout.CENTER );
        getContentPane().add( populateTableButton, BorderLayout.SOUTH );
        pack();
      }
    
    
      private void populateTable( ) {
        String line = "Hello~There~This~Is~A~Test";
        String dataValue[] = line.split("~");
        Vector<String> v = new Vector<>( Arrays.asList( dataValue ));
        tableModel.setColumnCount( v.size() );
        tableModel.addRow( v );
      }
    
      public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          @Override
          public void run() {
            new ReadFileGUI().setVisible(true);
          }
        });
      }         
    }
    

    Things I changed:

    • used imports instead of full names to avoid horizontal scroll bars
    • used a BorderLayout iso generated layout to shorten the code
    • renamed some variables for improved readability of the code
    • removed the jTable1.tableChanged(new TableModelEvent(model)); call as the DefaultTableModel will fire the appropriate events when you use the addRow method. No need to indicate this change another time to the table

    But the relevant change is the tableModel.setColumnCount( v.size() ); call. The default constructor of DefaultTableModel creates a model with zero columns and rows. If you first set the number of columns, you can use the addRow method

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

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
i want to parse a xhtml file and display in UITableView. what is the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm interested in microtypography issues on the web. I want a tool to fix:
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:

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.