Skip to main content

JXTreeTable how to place JButton in a cell.

7 replies [Last post]
sawerset
Offline
Joined: 2011-05-06

Hello.
What is the right way for placing JButton into the cell of JXTreeTable?
Where example can be found?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kschaefe
Offline
Joined: 2006-06-08

The same as placing a JButton in a standard JTable. If you need help with that, please review the Oracle JTable tutorial.

Karl

sawerset
Offline
Joined: 2011-05-06

I tried it, but button never gets actions.

kschaefe
Offline
Joined: 2006-06-08

Is the button in an editor? JTable swallows all mouse events because the components supplied via the renderer are only used to draw.

Karl

sawerset
Offline
Joined: 2011-05-06

Yes, I have implemented both TableCellEditor and TableCellRenderer. And it is works with JTable but not with JXTreeTable.

kschaefe
Offline
Joined: 2006-06-08

Time to supply a small, runnable test case.

Karl

sawerset
Offline
Joined: 2011-05-06

Here is my code, that doesn`t work

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.treetable.AbstractTreeTableModel;

public class SwingXExample extends JFrame {
private MyTreeTableModel treeTableModel = new MyTreeTableModel();
private JXTreeTable treeTable = new JXTreeTable( treeTableModel );
public SwingXExample() {
add(new JScrollPane(treeTable));
setSize( 700, 200 );
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLocation( d.width / 2 - 512, d.height/2 - 384 );
setVisible( true );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                treeTable.getColumnModel().getColumn(2).setCellRenderer(new ButtonsRenderer());
                treeTable.getColumnModel().getColumn(2).setCellEditor(new ButtonsEditor());
                //treeTable.getColumnModel().getColumn(2).setCellRenderer( new DefaultTableRenderer(new Provider()));
             
}

public static void main( String[] args )
{
SwingXExample sxe = new SwingXExample();
}
}

class ButtonsRenderer  implements TableCellRenderer { 
  private JButton jb = new JButton("Test");

  public ButtonsRenderer() {
      jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Test");
            }
        });
  }
 
  @Override public Component getTableCellRendererComponent(JTable table,
        Object value, boolean isSelected, boolean hasFocus, int row, int column) {
       return jb;
  }
}

class ButtonsEditor implements TableCellEditor {
    private JButton jb = new JButton("Test");
    public ButtonsEditor() {
         jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Test");
            }
        });
    }
    

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
      return jb;
    }

    @Override
    public Object getCellEditorValue() {
         return "";
    }

    @Override
    public boolean isCellEditable(EventObject anEvent) {
        return true;
    }

    @Override
    public boolean shouldSelectCell(EventObject anEvent) {
        return true;
    }

    @Override
    public boolean stopCellEditing() {
       return true;
    }

    @Override
    public void cancelCellEditing() {
    }

    @Override
    public void addCellEditorListener(CellEditorListener l) {
    }

    @Override
    public void removeCellEditorListener(CellEditorListener l) {
    }
}

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 );
for(int i = 0; i< 20; i++){
                    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 "";
                    default: return "Unknown";
}
}

@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;
}

    @Override
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<MyTreeNode> children = new ArrayList<MyTreeNode>();
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<MyTreeNode> getChildren() {
return children;
}
       @Override
public String toString(){
return "MyTreeNode: " + name + ", " + description;
}
}
kleopatra
Offline
Joined: 2003-06-11

AFAICS, your model isn't editable, that's why the editor wouldn't be effective - even if it were a valid implementation (which it isn't: it _must_ notify its listeners when the edit is terminated).

Cheers
Jeanette

(sorry for the late reply)