• Post Reply Bookmark Topic Watch Topic
  • New Topic

help needed in rectifying JTable problem  RSS feed

 
sahas ullur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am relatively new to JTable, i like to handle focus event when a cell
looses focus please help me with solving the below problem

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.util.Vector;
import java.util.*;
public class TestTable
{static Integer i=new Integer(1);
private JTable table;
private TableModel defaultModel;
private JScrollPane jsp;
public TestTable()
{
table = new JTable();
defaultModel = new DefaultTableModel(1,5);
setModel(defaultModel);
Vector v = new Vector();
v.addElement(i);
v.addElement("");
v.addElement("");
v.addElement("");
v.addElement("");
((DefaultTableModel)table.getModel()).addRow(v);
int rowCount = table.getRowCount();
((DefaultTableModel)table.getModel()).removeRow((rowCount-2));
table.setEditingRow(rowCount);
((DefaultTableModel)table.getModel()).fireTableRowsInserted(rowCount,rowCount);
int j=i.intValue();
j+=1;
i=new Integer(j);

jsp = new JScrollPane(table);
table.addKeyListener(new MyKeyListener(table));

}
public void setModel(TableModel model)
{
table.setModel(model);
}
public JScrollPane getScrollPane()
{
return jsp;
}
public JTable getTable()
{
return table;
}
public class MyKeyListener implements KeyListener
{
JTable table = null;
MyKeyListener(JTable table)
{
this.table = table;
}
public void keyPressed(KeyEvent e){ }
public void keyTyped(KeyEvent e)
{}
public void keyReleased(KeyEvent e)
{}
}
public static void main(String[] args)
{
TestTable tt = new TestTable();
JFrame frame = new JFrame("Test Table");
frame.getContentPane().add(tt.getScrollPane());
frame.pack();
frame.setVisible(true);
}

public class MyTableCellEditor extends DefaultCellEditor
{
public MyTableCellEditor(JTextField textField)
{
super(textField);
System.out.println(" MyTableCellEditor ");
JTextField txt;
txt = (JTextField) super.getComponent();
txt.addFocusListener(new FocusAdapter()
{
public void focusLost(FocusEvent e)
{
System.out.println("FOCUS LOST");
table.editingCanceled(new ChangeEvent(table));
}
});
}
public void addListener(JComponent component)
{
component.addFocusListener(new FocusAdapter()
{
public void focusLost(FocusEvent e)
{
System.out.println("FOCUS LOST*****IN AddListener");
stopCellEditing() ;
}
});
}
}
}
 
Daniel Puryear
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, you need to add a FocusListener to each of the cell renderers involved. Normally, the cells use the DefaultTableCellRenderer which extends JLabel assuming they are not editable. You can create your own renderer and attach it as needed. If the cell is editable, when you tab into the cell, you're actually on a cell editor, not the cell itself.
The JTable is a VERY complex 'family' of inter-related components - See the JavaTutorial for the gory details or come back if you add additional info...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!