MuhammadAdil
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(jTabl e1);
jTable1.setBounds(100, 90, 320, 170);
this.jTable1.setValueAt("R OLLS", 0, 0);
this.jTable1.setValueAt("C ITY", 0, 1);
this.jTable1.setValueAt("Y EAR", 0, 2);
Error
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfB oundsExcep tion: 0 >= 0
at java.util.Vector.elementAt (Vector.ja va:432)
at javax.swing.table.DefaultT ableColumn Model.getC olumn(Defa ultTableCo lumnModel. java:280)
at javax.swing.JTable.convert ColumnInde xToModel(J Table.java :1761)
at javax.swing.JTable.setValu eAt(JTable .java:1875 )
at arbindex.Rolls.<init>(Roll s.java:47)
at arbindex.Projects.dataActi on(Project s.java:195 )
at arbindex.Projects.jButton1 ActionPerf ormed(Proj ects.java: 158)
at arbindex.Projects.access$1 00(Project s.java:15)
at arbindex.Projects$2.action Performed( Projects.j ava:81)
at javax.swing.AbstractButton .fireActio nPerformed (AbstractB utton.java :1849)
at javax.swing.AbstractButton $Handler.a ctionPerfo rmed(Abstr actButton. java:2169)
at javax.swing.DefaultButtonM odel.fireA ctionPerfo rmed(Defau ltButtonMo del.java:4 20)
at javax.swing.DefaultButtonM odel.setPr essed(Defa ultButtonM odel.java: 258)
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(jTabl
jTable1.setBounds(100, 90, 320, 170);
this.jTable1.setValueAt("R
this.jTable1.setValueAt("C
this.jTable1.setValueAt("Y
Error
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfB
at java.util.Vector.elementAt
at javax.swing.table.DefaultT
at javax.swing.JTable.convert
at javax.swing.JTable.setValu
at arbindex.Rolls.<init>(Roll
at arbindex.Projects.dataActi
at arbindex.Projects.jButton1
at arbindex.Projects.access$1
at arbindex.Projects$2.action
at javax.swing.AbstractButton
at javax.swing.AbstractButton
at javax.swing.DefaultButtonM
at javax.swing.DefaultButtonM
>Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfB
> at java.util.Vector.elementAt
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) ;
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)
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.g etString(" CITY"), rs.getString("PYEAR")});
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,
Add a row using
tabModel.addtRow(new Object[] { rs.getString("RFILE"),rs.g etString(" CITY"), rs.getString("PYEAR")});
tabModel.addtRow(new Object[] { rs.getString("RFILE"),rs.g
Typo:
>>addtRow
should be
addRow
>>addtRow
should be
addRow
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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