Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1084
  • Last Modified:

making uneditable JTable

Hello Dear;
i m try to make jtable to uneditable and gotting error


 private JTable jTable1;
     DefaultTableModel tabModel =new DefaultTableModel();
jTable1 = new JTable(tabModel)
{
public boolean isCellEditable(int row, int column)
{
return false;
}
};
 getContentPane().add(jTable1);
        jTable1.setBounds(100, 90, 320, 170);

   this.jTable1.setValueAt("ROLLS", 0, 0);
        this.jTable1.setValueAt("CITY", 0, 1);
        this.jTable1.setValueAt("YEAR", 0, 2);



Error
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
        at java.util.Vector.elementAt(Vector.java:432)
        at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:280)
        at javax.swing.JTable.convertColumnIndexToModel(JTable.java:1761)
        at javax.swing.JTable.setValueAt(JTable.java:1875)
        at arbindex.Rolls.<init>(Rolls.java:47)
        at arbindex.Projects.dataAction(Projects.java:195)
        at arbindex.Projects.jButton1ActionPerformed(Projects.java:158)
        at arbindex.Projects.access$100(Projects.java:15)
        at arbindex.Projects$2.actionPerformed(Projects.java:81)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
     
0
MuhammadAdil
Asked:
MuhammadAdil
1 Solution
 
CEHJCommented:
Try

DefaultTableModel tabModel = new DefaultTableModel() {
      public boolean isCellEditable(int row, int column) {
            return false;
      }
};
tabModel.addRow(new Object[] { "ROLLS", "CITY", "YEAR" });

Now use 'tabModel' to construct JTable
0
 
Giant2Commented:

>Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
>        at java.util.Vector.elementAt(Vector.java:432)

What is at Rolls.java line 47 ?
Maybe you use a Vector to represent the data and it is empty or not initialized.

Bye, Giant.
0
 
objectsCommented:
Set your column headings in the ctor, *not* using setValueAt()
Change your code to the following.

private JTable jTable1;
DefaultTableModel tabModel =new DefaultTableModel(new String[] { "ROLLS", "CITY", "YEAR"}, 0 );
jTable1 = new JTable(tabModel)
{
public boolean isCellEditable(int row, int column)
{
   return false;
}
};
JScrollPane pane = new JScrollPane(jTable1);
pane.setBounds(100, 90, 320, 170);
getContentPane().add(pane);
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
MuhammadAdilAuthor Commented:
Hello Dear;

I have got one row with names ROLLS, CITY, YEAR
using that Objects's code
private JTable jTable1;
DefaultTableModel tabModel =new DefaultTableModel(new String[] { "ROLLS", "CITY", "YEAR"}, 0 );
jTable1 = new JTable(tabModel)
{
public boolean isCellEditable(int row, int column)
{
   return false;
}
};
JScrollPane pane = new JScrollPane(jTable1);
pane.setBounds(100, 90, 320, 170);
getContentPane().add(pane);

Now i want to add rows using Resultset. But i m got error.
i m using following code
this.tabModel.insertRow(a,new String[] { rs.getString("RFILE"),rs.getString("CITY"), rs.getString("PYEAR")});
0
 
CEHJCommented:
Add a row using

tabModel.addtRow(new Object[] { rs.getString("RFILE"),rs.getString("CITY"), rs.getString("PYEAR")});
0
 
CEHJCommented:
Typo:

>>addtRow

should be

addRow
0
 
objectsCommented:
> this.tabModel.insertRow(a,new String[] { rs.getString("RFILE"),rs.getString("CITY"), rs.getString("PYEAR")});

make sure a is a valid row number

whats the error?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now