jtable in combobox

Hi,

a have a table that works perfect with cell editing, add a new empty row and delete a row when using the table inside a JPanel. But I want to show it in a JCombobox.

With code I found in this forum I was able to add the table to a combobox. Now the problem is that editing cells of the table doesn't work.

I tried various listeners, renderers and editors for the combox and the popup bit no success.

The problem could be that table never gains focus:

getTableCellRendererComponent(...) always gets hasFocus=false

Any help ?

table

publicclass MyTabelle

extends JTable{

staticfinalint DATE = 0;

staticfinalint DOUBLE = 1;

static String defaultText[] =new String[]{

" . ."," , "};

DBListe dBliste =null;

MyTableModel model =new MyTableModel();

int editRow = 0;

int editColumn = 0;

boolean falseEdit =false;

public MyTabelle (){

setModel(model);

setDefaultRenderer(models.Date.class,new MyRenderer(DATE));

setDefaultRenderer(Double.class,new MyRenderer(DOUBLE));

setDefaultEditor(models.Date.class,new MyCellEditor(DATE));

setDefaultEditor(Double.class,new MyCellEditor(DOUBLE));

setListe(getListe());

}

/**

* Die Liste der anzuzeigenden Daten setzen.

*/

publicvoid setListe (DBListe liste){

this.dBliste = liste;

model.setListe(liste);

}

publicvoid addLine (){

model.addLine();

}

publicvoid removeLine(){

model.removeLine(getSelectedRow());

}

/**

* TableModel der Tabelle

*/

class MyTableModel

extends AbstractTableModel{

String[] columnNames;

ArrayList data;

Class[] classNames;

public MyTableModel (){

setTableData();

}

privatevoid setTableData (){

columnNames =new String[0];

data =new ArrayList();

classNames =new Class[0];

}

publicvoid setListe (DBListe liste){

if (liste ==null || liste.size() == 0){

setTableData();

}

else{

data.clear();

DBObject o = (DBObject) liste.get(0);

columnNames = o.getColumnNames();

if (columnNames ==null){

setTableData();

}

else{

int l = liste.size();

for (int i = 0; i < l; i++){

data.add(((DBObject) liste.get(i)).getData());

}

classNames =new Class[(o.getData().size())];

l = o.getData().size();

for (int i = 0; i < l; i++){

classNames[i] = o.getData().get(i).getClass();

}

}

}

fireTableStructureChanged();

}

public String getColumnName (int col){

return columnNames[col];

}

publicint getColumnCount (){

return columnNames ==null ? 0 : columnNames.length;

}

publicint getRowCount (){

return data.size();

}

public Class getColumnClass (int c){

return classNames[c];

}

public Object getValueAt (int aRow,int aColumn){

ArrayList row = (ArrayList) data.get(aRow);

return row.get(aColumn);

}

publicboolean isCellEditable (int rowIndex,int columnIndex){

returntrue;

}

publicvoid addLine (){

int cols = getColumnCount();

ArrayList li =new ArrayList();

for (int i = 0; i < cols; i++){

li.add("");

}

data.add(li);

fireTableStructureChanged();

}

publicvoid removeLine (int row){

data.remove(row);

fireTableStructureChanged();

}

publicvoid updateCell (Object newVal,int r,int c){

ArrayList row = (ArrayList) data.get(r);

row.set(c, newVal);

fireTableStructureChanged();

}

}

/**

* CellEditor der Tabelle

*/

publicclass MyCellEditor

extends DefaultCellEditor{

int type;

public MyCellEditor (int type){

super(type == DATE ? (JTextField)new DatumFeld() :

new DoubleFeld(DoubleFeld.STUNDEN));

this.type = type;

}

public Component getTableCellEditorComponent (

JTable table, Object value,boolean isSelected,int row,int column){

editRow = row;

editColumn = column;

return super.getTableCellEditorComponent(

table, value, isSelected, row, column);

}

publicboolean stopCellEditing (){

Object value =null;

try{

switch (type){

case DATE:

DatumFeld dateF = (DatumFeld) getComponent();

value =new models.Date(dateF.getText());

break;

case DOUBLE:

DoubleFeld doubF = (DoubleFeld) getComponent();

value =new Double(doubF.getValue());

break;

}

model.updateCell(value, editRow, editColumn);

falseEdit =false;

return super.stopCellEditing();

}

catch (Exception ex){

super.stopCellEditing();

falseEdit =true;

returnfalse;

}

}

}

/**

* Renderer der Tabelle

*/

class MyRenderer

extends DefaultTableCellRenderer{

SimpleDateFormat df =new SimpleDateFormat("dd.MM.yyyy");

int type;

public MyRenderer (int type){

this.type = type;

}

public Component getTableCellRendererComponent (

JTable table, Object value,boolean isSelected,boolean hasFocus,

int row,int column){

if (isSelected && hasFocus){

if (falseEdit){

table.editCellAt(editRow, editColumn);

((DefaultCellEditor) table.getCellEditor(editRow, editColumn)).

getComponent().requestFocus();

}

else{

table.editCellAt(row, column);

((DefaultCellEditor) table.getCellEditor(row, column)).

getComponent().requestFocus();

}

}

else{

this.setValue(value, row, column);

}

returnthis;

}

protectedvoid setValue (Object value,int row,int column){

if (value ==null || value.toString().equals("")){

setText(defaultText[type]);

}

else{

switch (type){

case DATE:

try{

setText(df.format(df.parse(value.toString())));

}

catch (ParseException ex){

}

break;

case DOUBLE:

setText(APSFormate.doubleFormat(((Double) value).doubleValue()));

break;

}

}

}

}

private DBListe getListe(){

DBListe liste =new DBListe();

try{

Wiedereingliederung w =new Wiedereingliederung();

w.setBeginn(new models.Date("01.08.2007"));

w.setEnde(new models.Date("31.08.2007"));

w.setStunden(6.0);

liste.add(w);

w =new Wiedereingliederung();

w.setBeginn(new models.Date("01.09.2007"));

w.setEnde(new models.Date("30.08.2007"));

w.setStunden(12.2);

liste.add(w);

w =new Wiedereingliederung();

w.setBeginn(new models.Date("01.10.2007"));

w.setEnde(new models.Date("31.10.2007"));

w.setStunden(18.5);

liste.add(w);

}

catch (Exception ex){

}

return liste;

}

}

Frame with combobox

publicclass ComboWithTableextends JComboBox

implements ActionListener, ListSelectionListener{

ButtonPanel buttonPanel =new ButtonPanel();

MyTabelle table =new MyTabelle();

MyPopup myPopup =null;

int rowSelected = -1;

int columnSelected = -1;

public ComboWithTable(){

this.setUI(new MyComboBoxUI(this));

addItem("Wiedereingliederung");

buttonPanel.addActionListener(this);

table.getSelectionModel().addListSelectionListener(this);

}

publicvoid valueChanged (ListSelectionEvent e){

rowSelected = table.getSelectedRow() ;

columnSelected = table.getSelectedColumn();

// ... ?

}

publicvoid actionPerformed(ActionEvent e){

if (e.getActionCommand().equals("Neu")){

table.addLine();

}

if (e.getActionCommand().equals("Lschen")){

table.removeLine();

}

if (e.getActionCommand().equals("Schlieen")){

myPopup.hide();

}

}

class MyComboBoxUIextends BasicComboBoxUI{

JComboBox comboBox;

public MyComboBoxUI (JComboBox combo){

this.comboBox = combo;

}

protected ComboPopup createPopup(){

myPopup =new MyPopup (comboBox);

return myPopup;

}

}

class MyPopupextends BasicComboPopup{

JComboBox comboBox;

public MyPopup (JComboBox combo){

super(combo);

this.comboBox = combo;

removeAll();

add(buildUI());

}

private JPanel buildUI(){

JPanel panel =new JPanel(new BorderLayout());

JScrollPane pane =new JScrollPane(table);

panel.add(pane, BorderLayout.CENTER);

panel.add(buttonPanel, BorderLayout.SOUTH);

return panel;

}

}

publicstaticvoid main (String[] args){

JFrame f =new JFrame();

JPanel panel =new JPanel(new BorderLayout());

panel.add(new ComboWithTable());

f.getContentPane().add(panel, BorderLayout.CENTER);

f.pack();

f.setLocation(300, 300);

f.show();

}

}

[19830 byte] By [es14552a] at [2007-11-27 10:56:45]
# 1

If you want help and are posting code, then post code that is simple & compiles on everyone's machine, without all your dependent classes and packages.

Recreate your problem as a simplified code example and repost it. (You might even solve the problem that way)

Suggestion: You could probably use another component aside a JComboBox and just place your JTable in a JWindow and show the window when the component (say a button) is clicked. May not be the same as a JComboBox but might give you more room to add functionality and prevent you from trying to modify the UI of a component just to achieve a simple functionality.

ICE

icewalker2ga at 2007-7-29 12:04:51 > top of Java-index,Desktop,Core GUI APIs...