Multiple Row selection using ctrl button while using bean component extending Jtable
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Manikandan_Kasi
Posted On:   Sunday, July 6, 2003 07:01 PM

I use a bean component which extends Jtable i try to use the multiple row selection control for the bean but it doesnt work,i give the source for my bean component pls help me to make the multiple Row Selection working. the Bean Component Source // package wac.entity_aware; import java.awt.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; import java.util.EventObject; import wac.entity_data.EntityInfo; import wac.entity_control.EntitySet; import wac.entity_control.EntitySetListener; import wac.entity_control.EntitySetEvent; import wac.entity_control.EntityAdapter; import wac.entity_control.TypeConverter;    More>>

I use a bean component which extends Jtable i try to use the multiple row selection control for the bean but it doesnt work,i give the source for my bean component pls help me to make the multiple Row Selection working.


the Bean Component Source

//
package wac.entity_aware;

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.util.EventObject;
import wac.entity_data.EntityInfo;
import wac.entity_control.EntitySet;
import wac.entity_control.EntitySetListener;
import wac.entity_control.EntitySetEvent;
import wac.entity_control.EntityAdapter;
import wac.entity_control.TypeConverter;

/**
*

Title:


*

Description:


*

Copyright: Copyright (c) 2003


*

Company:


* @author not attributable
* @version 1.0
*/

public class EntityTable extends JTable implements EntitySetListener{


private String[] headers;
private boolean isFromUserInput;
private String[] visibleFieldNames;
private TableColumnMask[] masks;
public EntityTable() {
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
setLayout(null);
setSize(400,300);
SymFocus aSymFocus = new SymFocus();
this.addFocusListener(aSymFocus);
}
public void setModel(TableModel model) {
super.setModel(model);
if (dataModel instanceof EntitySet) ((EntitySet)dataModel).addEntitySetListener(this);
}

public String[] getHeaders() {
return headers;
}
public void setHeaders(String[] headers) {
this.headers = headers;
columnModel.removeColumnModelListener(this);
while (columnModel.getColumnCount() > 0) columnModel.removeColumn(columnModel.getColumn(0));

if (this.headers == null) return;

int size = this.headers.length;
for (int i = 0; i < size; i++) {
TableColumn column = new TableColumn();
column.setHeaderValue(headers[i]);
addColumn(column);
}

}
public void createDefaultColumnsFromModel(){
if (!(dataModel instanceof EntitySet)) {
super.createDefaultColumnsFromModel();
return;
}
setUpEntityColumns();
}
public void setValueAt(Object newValue, int rowIndex, int fieldIndex){
TableColumn theColumn = getColumnModel().getColumn(fieldIndex);
String typeName = ((wac.entity_data.FieldInfo)theColumn.getIdentifier()).getTypeName();
Object inputValue = TypeConverter.getObjectValue(typeName, newValue);
super.setValueAt(inputValue, rowIndex, fieldIndex);
}

public void beforeOpen(EntitySetEvent event){}
public void onOpen(EntitySetEvent event){}
public void afterOpen(EntitySetEvent event){updatePosition(event);}
public void beforeClose(EntitySetEvent event){if (isEditing()) getCellEditor().stopCellEditing();}
public void afterClose(EntitySetEvent event){updatePosition(event);}
public void beforeAppend(EntitySetEvent event){}
public void afterAppend(EntitySetEvent event){updatePosition(event);}
public void beforeInsert(EntitySetEvent event){}
public void afterInsert(EntitySetEvent event){updatePosition(event);}
public void beforeEdit(EntitySetEvent event){}
public void afterEdit(EntitySetEvent event){}
public void beforeDelete(EntitySetEvent event){if (isEditing()) getCellEditor().stopCellEditing();}
public void afterDelete(EntitySetEvent event){updatePosition(event);}
public void beforeSave(EntitySetEvent event){if (isEditing()) getCellEditor().stopCellEditing();}
public void onSave(EntitySetEvent event){}
public void afterSave(EntitySetEvent event){}
public void beforeScroll(EntitySetEvent event){if (isEditing()) getCellEditor().stopCellEditing();}
public void afterScroll(EntitySetEvent event){updatePosition(event);}
public void onException(EntitySetEvent event){}
private void setUpEntityColumns(){
if (dataModel == null) return;
EntitySet entitySet = (EntitySet)dataModel;
EntityInfo entityInfo = entitySet.getEntityInfo();
if (entityInfo == null) return;
int size = entityInfo.getFieldInfoCount();
if (size == 0) return;


if (visibleFieldNames == null) {
visibleFieldNames = new String[size];
for (int i = 0; i < size; i++) visibleFieldNames[i] = entityInfo.getFieldInfo(i).getFieldName();
}

columnModel.removeColumnModelListener(this);
while (columnModel.getColumnCount() > 0) columnModel.removeColumn(columnModel.getColumn(0));


if (headers == null) {
headers = new String[size];
for (int i = 0; i < size; i++) headers[i] = entityInfo.getFieldInfo(i).getFieldName();
}

for (int i = 0; i < visibleFieldNames.length; i++) {
int fieldIndex = entitySet.findColumn(visibleFieldNames[i]);
TableColumn newColumn = new TableColumn(fieldIndex);
newColumn.setIdentifier(entityInfo.getFieldInfo(fieldIndex));

newColumn.setHeaderValue(headers[i]);
columnModel.addColumn(newColumn);
}

int maskCount = masks == null ? 0 : masks.length;
int colCount = columnModel.getColumnCount();
for (int i = 0; i < maskCount; i++) {
for (int j = 0; j < colCount; j++) {
TableColumn column = columnModel.getColumn(j);
wac.entity_data.FieldInfo fieldInfo = (wac.entity_data.FieldInfo)column.getIdentifier();
String fieldName = masks[i].getFieldName();
if (fieldInfo.getFieldName().equals(fieldName))
column.setCellRenderer(new MaskedColumnRenderer(masks[i].getMask()));
}
}

columnModel.addColumnModelListener(this);
}
public void valueChanged(ListSelectionEvent event){
try {
super.valueChanged(event);
if (!isFromUserInput)
return;

int rowIndex = getSelectedRow();
EntitySet entitySet = (EntitySet) dataModel;
if (rowIndex == entitySet.getCursor().getCurrentRow())
return;
entitySet.moveTo(rowIndex);
}
catch (Exception e) {
throw new RuntimeException(e.toString());
}
}

public String[] getVisibleFieldNames() {
return visibleFieldNames;
}
public void setVisibleFieldNames(String[] visibleFieldNames) {
this.visibleFieldNames = visibleFieldNames;
}
public boolean editCellAt(int row, int column, EventObject e){
EntitySet set = (EntitySet)dataModel;
set.moveTo(row);
boolean result = false;
result = super.editCellAt(row, column, e);

DefaultCellEditor editor = (DefaultCellEditor)getCellEditor(row, column);
if (editor != null) {
java.awt.Component editorComponent = editor.getComponent();

if (editorComponent != null && editorComponent instanceof JTextField)
((JTextField)editorComponent).selectAll();
}

return result;
}
private void updatePosition(EntitySetEvent event){
int rowIndex = event.getFirstRow();
if (rowIndex < 0) return;

isFromUserInput = false;
setRowSelectionInterval(rowIndex, rowIndex);
scrollRectToVisible(getCellRect(rowIndex, 0, true));
setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
isFromUserInput = true;
}

class SymFocus extends java.awt.event.FocusAdapter
{
public void focusLost(java.awt.event.FocusEvent event)
{
Object object = event.getSource();
if (object == EntityTable.this)
EntityTable_focusLost(event);
}
}

void EntityTable_focusLost(java.awt.event.FocusEvent event)
{
if (isEditing()) {
int row = getEditingRow(),
col = getEditingColumn();
TableCellEditor editor = getCellEditor(row, col);
if (editor != null) setValueAt(editor.getCellEditorValue(), row, col);
}
}
public TableColumnMask[] getMasks() {
return masks;
}
public void setMasks(TableColumnMask[] masks) {
this.masks = masks;
}

}


in the application i the code goes as below


EntityTable entityTable1 = new EntityTable();
entityTable1.setHeaders(new String[] {"Form ID", "User Name", "Req Date", "Developer Name", "Finish Date", "Status"});
entityTable1.setVisibleFieldNames(new String[] {"FORM_ID", "REQ_USER_NAME", "REQ_DATE", "DEV_USER_NAME", "FINISH_DATE", "FINISH_YN"});
entityTable1.setMasks(new wac.entity_aware.TableColumnMask[]{
new wac.entity_aware.TableColumnMask("REQ_DATE", "0000/00/00"),
new wac.entity_aware.TableColumnMask("FINISH_DATE", "0000/00/00")
}
);
entityTable1.setModel(m_entitySet);
entityTable1.setSelectionForeground(Color.lightGray);

Adding this doesnt make it work. Can any one tell me where is am wrong ......

entityTable1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

Regards
Mani    <<Less
About | Sitemap | Contact