activating a JCombobox in Jtable
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Abhay_Kulkarni
Posted On:   Friday, November 8, 2002 01:51 AM

Hi all, I have a Jtable with my custom ComboBox editor and Renderer.I have written a sample code which can be copy-pasted and run from any editor. //~code import javax.swing.table.*; import javax.swing.event.*; import java.util.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Frame2 extends JFrame { JScrollPane jScrollPane1 = new JScrollPane(); JTable jTable1 = new JTable(5,5); public Frame2() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { this.setDefaultCloseOperation   More>>

Hi all,

I have a Jtable with my custom ComboBox editor and Renderer.I have written a sample code which can be copy-pasted and run from any editor.


			
//~code

import javax.swing.table.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Frame2 extends JFrame {
JScrollPane jScrollPane1 = new JScrollPane();
JTable jTable1 = new JTable(5,5);

public Frame2() {
try {
jbInit();
} catch(Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(null);
this.setSize(new Dimension(482, 296));
jScrollPane1.setBounds(new Rectangle(25, 30, 405, 170));
jTable1.setRowHeight(25);
jTable1.setDefaultEditor(Object.class,new EdCombo());
jTable1.setDefaultRenderer(Object.class,new RenCombo());

jScrollPane1.getViewport().add(jTable1, null);
this.getContentPane().add(jScrollPane1, null);
}

public static void main(String[] args) {
new Frame2().show();
}

}

class RenCombo extends JComboBox implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table,Object value,boolean bIsSelected,boolean bHasFocus,int iRow,int iCol) {
return this;
}
}

class EdCombo extends JComboBox implements TableCellEditor {

public Component getTableCellEditorComponent(JTable table,Object value,
boolean bIsSelected,int iRow,int iCol) {

return this;
}

public boolean isCellEditable(EventObject eo) {
return true;
}

public boolean shouldSelectCell(EventObject eo) {
return true;
}

public boolean stopCellEditing() {
Object[] listeners = listenerList.getListenerList();
for(int iCount = listeners.length-2; iCount > 0; iCount -= 2)
((CellEditorListener)listeners[iCount+1]).editingStopped(changeEvent);
return true;
}

public void cancelCellEditing() {
Object[] listeners = listenerList.getListenerList();
for(int iCount = listeners.length-2; iCount > 0; iCount -= 2)
((CellEditorListener)listeners[iCount+1]).editingCanceled(changeEvent);
}

public void addCellEditorListener(CellEditorListener cel) {
listenerList.add(CellEditorListener.class,cel);
}

public void removeCellEditorListener(CellEditorListener cel) {
listenerList.remove(CellEditorListener.class,cel);
}

public Object getCellEditorValue() {
return this.getSelectedItem();
}

private EventListenerList listenerList = new EventListenerList();
private ChangeEvent changeEvent = new ChangeEvent(this);
}



If you run this code ... you'll observe

1> The focus gets *trapped* inside the comboBox and the Table is non navigable using Keyboard. i.e. to access any cell, a mouse click is necessary

2> It takes 2 mouse clicks to activate a cell.
Can anyone tell me how to overcome these 2 problems ?


regards
-abhay    <<Less
About | Sitemap | Contact