Hi all,
I am stuck with a problem here.Could someone please help me out.
I have a table and in one of the columns i have given a combobox with the autofill property.
The problem is that when I type in the letters the autofill works just fine but when i select the option then it selects not only the current cell of the table but also the cells below it.But if I do the same on a lower cell the cells below are affected but the cells above are in tact.
Here is a part of the code:
tf = (JTextField)jcb.getEditor().getEditorComponent();
tf.addCaretListener(this);
tf.addKeyListener(new KeyAdapter()
{
public void keyReleased(KeyEvent e)
{if (e.getKeyCode() == e.VK_ENTER)
{
Object value = comboUi.getList().getSelectedValue();
tf.transferFocus();
jcb.hidePopup();
System.out.println("enter pressed, selected value, when enter pressed: "+value);
fireEditingStopped();
}
}
});
jcb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
// fireEditingStopped();
}
});
}
/* public JList getList()
{
return listBox;
}
*/
public void focusGained(FocusEvent fe)
{
System.out.println("focusgained");
}
public void focusLost(FocusEvent fe) {
System.out.println("focuslost");
}
public void addCellEditorListener(CellEditorListener listener) {
System.out.println("addCellEdtrlistner");
listenerList.add(CellEditorListener.class, listener);
}
public void removeCellEditorListener(CellEditorListener listener) {
System.out.println("removeCellEdtrlistner");
listenerList.remove(CellEditorListener.class, listener);
}
protected void fireEditingStopped() {
System.out.println("fireEditingStopped called ");
CellEditorListener listener;
Object[] listeners = listenerList.getListenerList();
for (int i = 0; i < listeners.length; i++) {
if (listeners[i] == CellEditorListener.class) {
listener = (CellEditorListener) listeners[i + 1];
listener.editingStopped(changeEvent);
}
}
tf.setText(newValue.toString());
}
protected void fireEditingCanceled() {
System.out.println("fireEditingCanceled called ");
CellEditorListener listener;
Object[] listeners = listenerList.getListenerList();
for (int i = 0; i < listeners.length; i++) {
if (listeners[i] == CellEditorListener.class) {
listener = (CellEditorListener) listeners[i + 1];
listener.editingCanceled(changeEvent);
}
}
// tf.setText(newValue.toString());
}
public void cancelCellEditing() {
System.out.println("cancelCellEditing called ");
fireEditingCanceled();
}
public boolean stopCellEditing() {
System.out.println("stopCellEditing called ");
fireEditingStopped();
return true;
}
public boolean isCellEditable(EventObject event) {
System.out.println("iscellEditable");
return true;
}
public boolean shouldSelectCell(EventObject event) {
System.out.println("shouldSelectcell");
return false;
}
public Object getCellEditorValue() {
System.out.println("getCellEditorValue called returning vlaue: "+newValue);
//tf.setText(newValue.toString());
// o1.table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(tf));
// o1.table.getModel().setValueAt(tf,2,1);
return newValue;
// return super.getSelectedItem();
}
/*public void setcellEditorValue(Object obj) {
System.out.println("setcelleditor");
tf.setText(obj.toString());
}
*/
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
return jcb;
}
public void caretUpdate(CaretEvent e) {
System.out.println("caretUpdate");
System.out.println(++caretctr);
// newValue=" ";
if (!jcb.isPopupVisible() && tf.isShowing() && tf.hasFocus())
jcb.showPopup();
JTextField tf = (JTextField)e.getSource();
String text = tf.getText().toLowerCase();
int index = -1;
for (int i = 0; i < jcb.getItemCount(); i++) {
String item = ((String)jcb.getItemAt(i)).toLowerCase();
if (item.startsWith(text))
{
index = i;
break;
}
}
if (index != -1 )
{
comboUi.getList().setSelectedIndex(index);
System.out.println(comboUi.getList().getSelectedIndex());
System.out.println(comboUi.getList().getSelectedValue());
}
else
comboUi.getList().clearSelection();
newValue = comboUi.getList().getSelectedValue();
System.out.println("new value set to: "+newValue);
}
}
this is the code for the renderer..
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,int column) {
System.out.println("row and col "+row+" "+column);
System.out.println("getTableCellRenderercmpnt hasfocus"+hasFocus);
System.out.println("getTableCellRenderercmpnt isSelected"+isSelected);
if (hasFocus)
{
System.out.println("has focus-is selected "+isSelected);
table.editCellAt(row,column);
JComboBox combo = (JComboBox)table.getColumnModel().getColumn(1).getCellEditor();
ComboBoxEditor editor = (ComboBoxEditor)combo.getEditor();
tf = (JTextField)editor.getEditorComponent();
tf.requestFocusInWindow();
System.out.println("selectall");
tf.selectAll();
}
if (isSelected) {
jcb.setForeground(table.getSelectionForeground());
jcb.setBackground(table.getSelectionBackground());
System.out.println("row "+row+" col "+column);
} else {
jcb.setForeground(table.getForeground());
jcb.setBackground(table.getBackground());
}
//table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(tf));
//table.getModel().setValueAt(tf.getText(),row,column);
jcb.setSelectedItem(value);
return jcb;
}
}
Please forgive me for giving all those print statements...
Any solution would be helpful...
Thanks,
Antony