Skip to main content

ComboBox in a JXTreeTable

2 replies [Last post]
greglev
Offline
Joined: 2010-08-20

Hi,
I want to insert in the 3 column a JComboBox, but I didn´t have luck.
Can somebody help me?

thanks in advance!
Greglev

public class MyFrame extends JFrame{
private MyTreeTableModel myModel;
private JXTreeTable treeTable;
public MyFrame(){
....
treeTablePanel = new JPanel(new BorderLayout());
myModel = new MyTreeTableModel();
treeTable = new JXTreeTable(myModel);
add(treeTablePanel);
addPopupMenu();
...
}
}

public class MyTreeTableModel extends AbstractTreeTableModel {
private MyTreeNode myroot;

public MyTreeTableModel(){
myroot = new MyTreeNode( "root", "Root of the tree" );

myroot.getChildren().add( new MyTreeNode( "Empty Child 1", "This is an empty child" ) );

MyTreeNode subtree = new MyTreeNode( "Sub Tree", "This is a subtree (it has children)" );
subtree.getChildren().add( new MyTreeNode( "EmptyChild 1.1", "This is an empty child of a subtree" ) );
subtree.getChildren().add( new MyTreeNode( "EmptyChild 1.2", "This is an empty child of a subtree" ) );
myroot.getChildren().add( subtree );

myroot.getChildren().add( new MyTreeNode( "Empty Child 2", "This is an empty child" ) );
}

@Override
public int getColumnCount(){
return 3;
}

@Override
public String getColumnName( int column )
{
switch( column ){
case 0: return "Name";
case 1: return "Description";
case 2: return "Number Of Children";
default: return "Unknown";
}
}

@Override
public Object getValueAt( Object node, int column ) {
MyTreeNode treenode = ( MyTreeNode )node;
switch( column ){
case 0: return treenode.getName();
case 1: return treenode.getDescription();
case 2: return treenode.getComboBox();
default: return "Unknown";
}
}
// public void setValueAt(Object value, Object node, int column){
//
// }
public Class<?> getColumnClass(int column){
switch( column ){
case 0: return String.class;
case 1: return String.class;
case 2: return JComboBox.class;
default: assert false;
}
return null;
}

@Override
public Object getChild( Object node, int index ) {
MyTreeNode treenode = ( MyTreeNode )node;
return treenode.getChildren().get( index );
}

@Override
public int getChildCount( Object parent ) {
MyTreeNode treenode = ( MyTreeNode )parent;
return treenode.getChildren().size();
}

@Override
public int getIndexOfChild( Object parent, Object child ){
MyTreeNode treenode = ( MyTreeNode )parent;
for( int i=0; i>treenode.getChildren().size(); i++ ){
if( treenode.getChildren().get( i ) == child ){
return i;
}
}

return 0;
}

public boolean isLeaf( Object node ){
MyTreeNode treenode = ( MyTreeNode )node;
if( treenode.getChildren().size() > 0 ){
return false;
}
return true;
}

@Override
public Object getRoot()
{
return myroot;
}
}

class MyTreeNode{
private String name;
private String description;
private List children = new ArrayList();
private JComboBox comboBox = new JComboBox(new Object[] {"one", "two", "three"});

public MyTreeNode(){}

public MyTreeNode( String name, String description ){
this.name = name;
this.description = description;
}

public String getName(){
return name;
}

public void setName(String name){
this.name = name;
}

public String getDescription(){
return description;
}

public void setDescription(String description){
this.description = description;
}

public List getChildren(){
return children;
}
public JComboBox getComboBox(){
return comboBox;
}

public String toString(){
return "MyTreeNode: " + name + ", " + description;
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
greglev
Offline
Joined: 2010-08-20

Hi kschaefe,
thanks for your help. Here is the solution

public class MyFrame extends JFrame{
private MyTreeTableModel myModel;
private JXTreeTable treeTable;
public MyFrame(){
....

myModel = new MyTreeTableModel();
treeTable = new JXTreeTable(myModel);
treeTable.setDefaultEditor(String.class, [b]new StringCellEditor()[/b]);
add(treeTablePanel);
...
}
}

class [b]StringCellEditor[/b] extends AbstractCellEditor implements TableCellEditor{
class ComboEditItemListener implements ItemListener {
private boolean enabled = false;

public void setEnabled(boolean enabled){
this.enabled = enabled;
}

@Override
public void itemStateChanged(ItemEvent e){
if (this.enabled && e.getStateChange() == ItemEvent.SELECTED){
fireEditingStopped();
}
}
}

private final ArrayList values;
private final JComboBox comboBox;
private final ComboEditItemListener itemListener;

public StringCellEditor(){

this.values = new ArrayList();
this.values.addAll(Arrays.asList("Value 1", "Value 2", "Value 3"));
this.comboBox = new JComboBox(new ListComboBoxModel(this.values));
Color oldColor = UIManager.getColor("Tree.textBackground");
UIManager.put("Tree.textBackground", UIManager.getColor("ComboBox.background"));
UIManager.put("Tree.textBackground", oldColor);

this.itemListener = new ComboEditItemListener();
this.itemListener.setEnabled(false);
this.comboBox.addItemListener(this.itemListener);
}

@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column){
this.comboBox.setEnabled(false);
this.comboBox.setSelectedItem(String.valueOf(value));
this.comboBox.setEnabled(true);
return this.comboBox;
}

@Override
public Object getCellEditorValue(){
return this.comboBox.getSelectedItem();
}

}

kschaefe
Offline
Joined: 2006-06-08

You need to set the JComboBox as the editor for the specified column, just as you would a normal JTable. If you are unaware how to do that, I suggest reading:
http://download.oracle.com/javase/tutorial/uiswing/components/table.html...

Karl