By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,034 Members | 819 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

Problems setting TableCellEditor for JTable

P: n/a
I am trying to set a TableCellEditor for a JTable, however,
the editor never seems to get called. I can not even get
a simplified version (below) to work. When I put a breakpoint on the
return statement in getTableCellEditorComponent, it never stops there
even though I edit cells in the table. Can anyone tell me what I am
missing. By the way, I have gotten TableCellRenderers to work with no
problems.

public class Frame1 extends JFrame
{
String[] headers = {"Column1", "Column2", "Column3"};
DefaultCellEditor cellEditor = (new DefaultCellEditor(new JTextField())
{
public Component getTableCellEditorComponent(JTable table, Object
value,
boolean isSelected,
int row, int column)
{
return editorComponent;
}
});

JPanel contentPane;
JTable jTable1 = new JTable()
{
public boolean isCellEditable(int row, int column)
{
return true;
}
};

XYLayout xYLayout1 = new XYLayout();
DefaultTableModel tableModel = new DefaultTableModel(headers, 5);

//Construct the frame
public Frame1()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
jTable1.setCellEditor(cellEditor);
}

//Component initialization
private void jbInit() throws Exception
{
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(601, 455));
this.setTitle("Frame Title");
jTable1.setModel(tableModel);
contentPane.add(jTable1, new XYConstraints(19, 46, 498, 136));
}

//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}
}

Thanks for any help you may give,

Ray Saunders
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Well, got it to work by setting the cell editor for each column
separately. Still don't understand why I can set it for all columns at
once.

Ray

raysaun wrote:
I am trying to set a TableCellEditor for a JTable, however,
the editor never seems to get called. I can not even get
a simplified version (below) to work. When I put a breakpoint on the
return statement in getTableCellEditorComponent, it never stops there
even though I edit cells in the table. Can anyone tell me what I am
missing. By the way, I have gotten TableCellRenderers to work with no
problems.

public class Frame1 extends JFrame
{
String[] headers = {"Column1", "Column2", "Column3"};
DefaultCellEditor cellEditor = (new DefaultCellEditor(new JTextField())
{
public Component getTableCellEditorComponent(JTable table, Object
value,
boolean isSelected,
int row, int column)
{
return editorComponent;
}
});

JPanel contentPane;
JTable jTable1 = new JTable()
{
public boolean isCellEditable(int row, int column)
{
return true;
}
};

XYLayout xYLayout1 = new XYLayout();
DefaultTableModel tableModel = new DefaultTableModel(headers, 5);

//Construct the frame
public Frame1()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
jTable1.setCellEditor(cellEditor);
}

//Component initialization
private void jbInit() throws Exception
{
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(601, 455));
this.setTitle("Frame Title");
jTable1.setModel(tableModel);
contentPane.add(jTable1, new XYConstraints(19, 46, 498, 136));
}

//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}
}

Thanks for any help you may give,

Ray Saunders

Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.