I have a JXTreeTable
structure with 5 columns and I have to make the last one (of Boolean type) editable.
I've tried anything, googled for hours, I searched here on SO and I've found similar questions but they're not helping in my specific case (that's because they gave answers about issues with JTrees
or simple JTables
and not with JXTreeTable
component).
I've already specified the getColumnClass(int column)
method in my tree model to make the boolean cells in the last column be rendered as JCheckBoxes
, and the isCellEditable(Object node, int column)
method to return the column and make it editable.
I'm actually populating that column with a query that reads a file from a database and sets some of those cells to true if they meet the requirements.
Here's the link to what my GUI actually looks like:
Let me know if you need some pieces of my code to see, and thanks in advice!
Here is the tree construction:
DefaultMutableTreeNode albero = initTree();
final JXTreeTable binTree = new JXTreeTable(new MyTreeModel(albero));
Highlighter highligher = HighlighterFactory.createSimpleStriping(HighlighterFactory.BEIGE);
binTree.setHighlighters(highligher);
binTree.setShowGrid(false);
binTree.setShowsRootHandles(true);
binTree.setEditable(true);
configureCommonTableProperties(binTree);
binTree.setTreeCellRenderer(new TreeTableCellRenderer());
The definition of the TreeModel:
public class MyTreeModel extends AbstractTreeTableModel {
private String [] titles = {"Utente", "Radice Menù", "Programma", "Descrizione", "Abilitazione"};
public MyTreeModel(DefaultMutableTreeNode root) {
super(root);
}
@Override
public String getColumnName(int column) {
if (column < titles.length)
return (String) titles[column];
else
return "";
}
@Override
public boolean isCellEditable(Object node, int column) {
return column == 4;
}
@Override
public int getColumnCount() {
return titles.length;
}
@Override
public Class getColumnClass(int column) {
if (column == 4 )
return Boolean.class;
else
return String.class;
}
@Override
public Object getValueAt(Object arg0, int arg1) {
if(arg0 instanceof TableRowData) {
TableRowData data = (TableRowData)arg0;
if(data != null) {
switch(arg1) {
case 0: return data.getUtente();
case 1: return data.getRott();
case 2: return data.getProgramma();
case 3: return data.getDescrizione();
case 4: return data.getFlag();
}
}
}
}
if(arg0 instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode dataNode = (DefaultMutableTreeNode)arg0;
TableRowData data = (TableRowData)dataNode.getUserObject();
if(data != null) {
switch(arg1) {
case 0: return data.getUtente();
case 1: return data.getRott();
case 2: return data.getProgramma();
case 3: return data.getDescrizione();
case 4: return data.getFlag();
}
}
}
@Override
public Object getChild(Object arg0, int arg1) {
if(arg0 instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode nodes = (DefaultMutableTreeNode)arg0;
return nodes.getChildAt(arg1);
}
return null;
}
@Override
public int getChildCount(Object arg0) {
if(arg0 instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode nodes = (DefaultMutableTreeNode)arg0;
return nodes.getChildCount();
}
return 0;
}
@Override
public int getIndexOfChild(Object arg0, Object arg1) {
if(arg0 instanceof DefaultMutableTreeNode && arg1 instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) arg0;
return parent.getIndex((DefaultMutableTreeNode) arg1);
}
return 0;
}
@Override
public boolean isLeaf(Object node) {
return getChildCount(node) == 0;
}
return null;
}
And the class that implements the get/set methods for the row data:
public class TableRowData {
private String utente = null;
private String rott;
private String programma;
private boolean flag;
private String descrizione;
private boolean isRoot;
public TableRowData(String Utente, String Rott, String Programma, String Descrizione, Boolean Flag, boolean isLeaf) {
this.utente = Utente;
this.rott = Rott;
this.programma = Programma;
this.flag = Flag;
this.descrizione = Descrizione;
this.isRoot = isLeaf;
}
/**
* @return the isRoot
*/
public boolean isRoot() {
return isRoot;
}
/**
* @param isRoot the isRoot to set
*/
public void setRoot(boolean isLeaf) {
this.isRoot = isLeaf;
}
public String getUtente() {
return utente;
}
public void setUtente(String Utente) {
this.utente = Utente;
}
public String getRott() {
return rott;
}
public void setRott(String Rott) {
this.rott = Rott;
}
public String getProgramma() {
return programma;
}
public void setProgramma(String Programma) {
this.programma = Programma;
}
public Boolean getFlag() {
return flag;
}
public void setFlag(Boolean Flag) {
this.flag = Flag;
}
public String getDescrizione() {
return descrizione;
}
public void setDescrizione(String descrizione) {
this.descrizione = descrizione;
}
}