Results 1 to 4 of 4
  1. #1
    JOHNINALBANY is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default Can't add column to table dynamically

    I have added a table (JTable) to a frame. That table has been created by using Netbeans. Once my table has been created (after initcomponents()) I try to add a column to my table by using the jTable1.addColumn(new TableColumn(jTable1.getColumnCount())) statement. This stament triggers an error:

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4 >= 4

    What am I missing here?

    Thanks



    ************************************************** ********************************************



    import javax.swing.table.*;
    import javax.swing.table.TableColumn;

    public class MyTable extends javax.swing.JFrame {

    /** Creates new form MyTable */
    public MyTable() {
    TableColumn tc = new TableColumn();
    tc.setModelIndex(4);

    initComponents();

    //*************************************
    //**************************************
    // The below statement triggers an error. What happens?
    jTable1.addColumn(new TableColumn(jTable1.getColumnCount()));
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

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

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jTable1.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {
    {null, null, null, null},
    {null, null, null, null},
    {null, null, null, null},
    {null, null, null, null}
    },
    new String [] {
    "Title 1", "Title 2", "Title 3", "Title 4"
    }
    ));
    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(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
    .addContainerGap(15, Short.MAX_VALUE)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap())
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
    .addContainerGap(14, Short.MAX_VALUE)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap())
    );

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

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

    public void run() {
    new MyTable().setVisible(true);
    }
    });
    }
    // Variables declaration - do not modify
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Can't add column to table dynamically

    Moved from New to Java.

    Please go through the following links:
    Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Can't add column to table dynamically

    Also, a visual designer is not a beginners' tool. Hand code your GUIs until you're sufficiently familiar with the API to know where to look to add features in a visually designed Swing GUI. By which time you'll already have discovered that it's not worth the time and effort, unless you plan to make a living as a GUI designer/coder.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Can't add column to table dynamically

    I see that you didn't bother to return to 4 threads you started. In the two that you did return to, only one response has a word of thanks to the member who helped you.

    Not the best way to continue to get free help on a forum, that.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 1
    Last Post: 03-28-2012, 03:37 PM
  2. Replies: 1
    Last Post: 02-16-2012, 10:24 AM
  3. Replies: 1
    Last Post: 07-06-2010, 05:50 PM
  4. table column sort
    By new2java2009 in forum New To Java
    Replies: 8
    Last Post: 04-16-2010, 02:17 AM
  5. Sorting a SWT table by column
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •