Link to home
Start Free TrialLog in
Avatar of MuhammadAdil
MuhammadAdilFlag for Pakistan

asked on

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)
     
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of Giant2
Giant2


>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.
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);
Avatar of MuhammadAdil

ASKER

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")});
Add a row using

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

>>addtRow

should be

addRow
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial