I have been researching JTables fairly thoroughly, and have found some brilliant resources that explain how to set the CellRenderer and CellEditor for columns, but have found that trying to set the Renderer and Editor of a single cell is a little less forthcoming.
The general idea of my table is to have two columns, where the left column displays variable names, and the right column allows for the modification of the associated variables. Most cells I have been able to perform validation on, and even found out how to use the DefaultCellRenderer and DefaultCellEditor the way that I required, but trying to do this with custom renderers and editors I am struggling with. Below I have tried to indicate how a table under my design would look: (Left hand value is the variable name, the right hand is the value)
x 0
y 0
isDestructable [checkbox]
image [clickable cell]
The image value should allow the user to click the cell, which will then bring up a JFileChooser, and extract the response and render a String representation inside the cell. I have numerous snippets of code written, but trying to link them together is the bit that I am really lost on. Below I have provided the code I have so far relating to these functions:
/*
Code that creates the table and passes a custom model to it. Code for setting the other
types of renderers and editors I have already got working have been omitted
*/
table = new JTable(new MyDataModel(columnNames, values))
{
public TableCellRenderer getCellRenderer(int row, int col)
{
if(col == 1)
{
if(table.getModel().getValueAt(row, 0).equals("image"))
{
//This is where I would want to set my renderer I believe
}
}
return super.getCellRenderer(row, col);
}
public TableCellEditor getCellEditor(int row, int col)
{
if(col == 1)
{
if(table.getModel().getValueAt(row, 0).equals("image"))
{
//This is where I would want to set my editor I believe
}
}
return super.getCellEditor(row, col);
}
};
/*
As an inner class, I have created the following editor that should display a JFileChooser
when the user clicks on the cell
*/
private class ButtonTableCellEditor extends AbstractTableCellEditor implements TableCellEditor
{
private JFrame mainframe;
private JFileChooser jfc;
public ButtonTableCellEditor(JFrame frame)
{
mainframe = frame;
jfc = new JFileChooser();
}
@Override
public void addCellEditorListener(CellEditorListener arg0)
{
}
@Override
public void cancelCellEditing()
{
//Hide the JFileChooser here
super.cancelCellEditing();
}
@Override
public Object getCellEditorValue()
{
//Here we need to return the filename that has been returned by the JFileChooser
return "test";
}
@Override
public boolean isCellEditable(EventObject arg0)
{
return true;
}
@Override
public void removeCellEditorListener(CellEditorListener arg0)
{
//Not sure that I will actually need to use this at all
}
@Override
public boolean shouldSelectCell(EventObject arg0)
{
int returned = jfc.showOpenDialog(mainFrame);
if(returned == JFileChooser.APPROVE_OPTION)
{
System.out.println("File was fine");
}
else
{
stopCellEditing();
}
return true;
}
public boolean stopCellEditing()
{
return false;
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean arg2, int row, int col)
{
//This is something I know that is incredibly important based off other examples, but
//I am unsure what to do in here in my case
return null;
}
}
All I really need help with (I think) is getting the ButtonTableCellEditor linked with the single cell I require (condition is if the left hand column equals "image", and what to do with the getTableCellEditorComponent() in my situation. Any help would be hugely appreciated, I have been stuck for a little while now.
Also, this is my first question (I have read numerous questions, and tried to take as much note as possible), so if there is any feedback in the way that I have posed the question so that later questions may be better structured, that would be most welcome. Thanks all!