• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1092
  • 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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