Results 1 to 4 of 4
- 07-07-2012, 04:41 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 13
- Rep Power
- 0
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
}
- 07-07-2012, 07:56 AM #2
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
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 07-07-2012, 07:58 AM #3
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 07-07-2012, 08:03 AM #4
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Dynamically create n-column and n-rows forms in awt/swing
By plesken in forum AWT / SwingReplies: 1Last Post: 03-28-2012, 02:37 PM -
Java program to read column & value pairs and build sql scripts dynamically
By Ragurama83 in forum JDBCReplies: 1Last Post: 02-16-2012, 09:24 AM -
selecting column names dynamically from table column header
By neha_sinha in forum AWT / SwingReplies: 1Last Post: 07-06-2010, 04:50 PM -
table column sort
By new2java2009 in forum New To JavaReplies: 8Last Post: 04-16-2010, 01:17 AM -
Sorting a SWT table by column
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 03:07 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks