NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Add row to JTable

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users
View previous topic :: View next topic  
Author Message
thealy
Posted via mailing list.





PostPosted: Tue Feb 24, 2009 8:00 pm    Post subject: Add row to JTable Reply with quote

I'm using NB 6.5 to create a small project that contains a JTable. I
need to add (append) a series of new rows at the bottom of the JTable
based on user input in a TextArea, as triggered by a button push.

The TableModel is created by NB. If I configure the table with, say, 4
rows I am able to change the data, but not add new rows. I've looked
through the tutorials but can't find a solution which fits.

Any suggestions?
Back to top
Bayless Kirtley
Posted via mailing list.





PostPosted: Tue Feb 24, 2009 10:40 pm    Post subject: Add row to JTable Reply with quote

You'll have to create your own table model. Probably can just use the
DefaultTableModel which, I believe does have a addRow or nsertRow
method. It's really pretty easy. Refer to the Javadac for specifics.

Bayless

----- Original Message -----
From: "thealy" <address-removed>
To: <address-removed>
Sent: Tuesday, February 24, 2009 2:00 PM
Subject: [nbusers] Add row to JTable


Quote:
I'm using NB 6.5 to create a small project that contains a JTable. I
need to add (append) a series of new rows at the bottom of the JTable
based on user input in a TextArea, as triggered by a button push.

The TableModel is created by NB. If I configure the table with, say, 4
rows I am able to change the data, but not add new rows. I've looked
through the tutorials but can't find a solution which fits.

Any suggestions?
Back to top
pointer2null



Joined: 11 Feb 2009
Posts: 13

PostPosted: Wed Feb 25, 2009 8:36 pm    Post subject: Reply with quote

You will need to make your own table model as the one used by default in NB doesn't have the addRow method.

A table model is simple to do:

Code:

   // define new table model class
    class SearchResultsModel extends DefaultTableModel {
        private String[] colNames;
        private Object[][] rowData;
        SearchResultsModel(Object[][] rowData, String[] colNames){
            super(rowData, colNames);
            this.colNames = colNames;
            this.rowData = rowData;
        }

        @Override
        public Class getColumnClass(int columnIndex) {
            return getValueAt(0, columnIndex).getClass();
        }

        @Override
        public String getColumnName(int col) {
            return colNames[col];
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false; // note my table does not allow editing
            // if yours does you need to change this.
        }

    }




Then when you need to add a line:

Code:

Object[] newRowData = {new Integer(0), "", new Double(0), new Double(0), new Double(0)};
        myNewTableModelInstance.addRow(newRowData);


Obviously your newRowData will contain the right object types for your table.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo