• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Help needed on editing a specific row in JTable

 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a JTable with a DefaultTableModel which is as the following
class UneditableTableModel1 extends DefaultTableModel {
UneditableTableModel1(Vector data, Vector columnNames) {
super(data, columnNames);
}
UneditableTableModel1(Vector columnNames, int rowcount) {
super(columnNames, rowcount);
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
if(columnIndex == 8 || columnIndex == 9)
return true;
return false;
}
}
I want column 8 and 9 to be edited. But sometimes I want some rows in that column to be disable. How can I do it? Both of the columns are boolean and they appeared as JCheckbox in the view... I need those checkboxes but sometimes I want some rows to be disable... Please help...
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You're on the right track. You use the method isCellEditable(). First you need to state what condition you want a row uneditable.
For example, lets say you want a row uneditable if a user checks the checkbox in column index 8.

If some external event needs to be checked you may need to hold a variable in your table model to keep track of it.
Hope that helps.
-Yoo-Jin
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much, Yoo-Jin... Since I'm not at the office today, I cannot test it... I think that will help too... I overlooked that point...
I appreciate your help very much...
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yoo-Jin Lee, I couldn't it? Do I have to updateUI() after the data are displayed in the table in order to disable some rows???
Please help me...
 
Yoo-Jin Lee
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ko,
I'm going to have to ask you to post some code since 'it doesn't work' is not very descriptive.
Cool?
-Yoo-Jin
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


myTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
myTable_mouseClicked(e);
}
});
protected void myTable_mouseClicked(MouseEvent e) {
int col = myTable.getSelectedColumn();
int row = myTable.getSelectedRow();
String sStatus = (String)myTableModel.getValueAt(row, 5);
if(col == 0 || col == 1 || col == 2 || col == 3 || col == 4 || col == 5 || col == 6 || col == 7){
if (e.getModifiers() == MouseEvent.BUTTON1_MASK) {
if (e.getClickCount() > 1) {
//get column position
int error_pos = 0;
for (int i = 0; i <m_table_config.length ; i++) {
String colname = m_table_config[i][0];
if (colname.equals("")) {
error_pos = i;
break;
}
}
Vector colvec = new Vector();
for(int i =0;i<m_table_config.length;i++)
colvec.addElement(m_table_config[i][0]);
Vector vTemp = new Vector();
vTemp = (Vector)allfield_records.elementAt(row);
int iId = Integer.parseInt((String)vTemp.elementAt(8));
SourceEventPopUp sourceEventPopUp = new SourceEventPopUp(this,(Vector)allfield_records.elementAt(row),colvec,error_pos, iId);
sourceEventPopUp.setResizable(false);
sourceEventPopUp.show();
// System.out.println(getSSourceEvent());
try{
constructPreparedStatement();
queryData();
} catch(SQLException sqle){
}
// myTableModel.setValueAt(new String(getSSourceEvent()), row, col);
}
}
} else if(col == 8){
if(sStatus.equals("Close")){
myTableModel.setValueAt(new Boolean(false), row, col);
return;
}
boolean bResend = ((Boolean)myTableModel.getValueAt(row, col)).booleanValue();
if(bResend){
myTableModel.setValueAt(new Boolean(false), row, col+1);
myTableModel.setValueAt(new Boolean(true), row, col);
} else{
myTableModel.setValueAt(new Boolean(false), row, col+1);
myTableModel.setValueAt(new Boolean(false), row, col);
}
} else if(col == 9){
boolean bResend = ((Boolean)myTableModel.getValueAt(row, col)).booleanValue();
if(sStatus.equals("Close")){
myTableModel.setValueAt(new Boolean(false), row, col);
return;
}
if(bResend){
myTableModel.setValueAt(new Boolean(false), row, col-1);
myTableModel.setValueAt(new Boolean(true), row, col);
} else{
myTableModel.setValueAt(new Boolean(false), row, col-1);
myTableModel.setValueAt(new Boolean(false), row, col);
}
}
}// end myTable_mouseClicked()



Here is the mouseClicked method which I added to the MouseListener... I cannot disable the checkboxes. I want to set the checkboxes disable when sStatus is "Close". That's why I used the boolean value set back to false and return the method... But it's still not working well... Please help...
 
Yoo-Jin Lee
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ko,
Try:
myTableModel.setValueAt(new Boolean(false), row, col);
}
}
myTableModel.fireTableCellUpdated(row, column);
}// end myTable_mouseClicked()

After you update a value in your tableModel.
-Yoo-Jin
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cannot do it... I think I should change it to be radio buttons.. Do u know how to insert two radio buttons of one group in two columns? I mean in two columns of my table, I want to put two radio buttons, which are in the same radio group... Help!
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ColumnIndex is zero-based. ColumnIndex 7,8 represent 8th Column, 9th Column
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic