Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6388891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:20:10+00:00 2026-05-25T03:20:10+00:00

I’m trying to use the example taken from the http://java.sun.com/products/jfc/tsc/articles/treetable2/index.html , in which I’ve

  • 0

I’m trying to use the example taken from the http://java.sun.com/products/jfc/tsc/articles/treetable2/index.html, in which I’ve substituted the filesystem model with my model.

I initially create a model, I display it in the JTreeTable, but now I’d like to update my model and then the JTreeTable (for example I want to add a node at the tree, modify a node, remove a node, etc.).

I don’t know how I can do it. I can’t see method that allow me to do what I want, I only see some method like treeNodesChanged, treeNodesInserted, etc., but probably I miss something in the global logic of this JTreeTable component.

Besides I’m not sure that I correctly create the model, because in various example I’ve seen people call various method over a “model” object (model.insertNodeInto, model.reload), inspite of I haven’t a model object..In the example above, is simply called the abstract class AbstractTreeTableModel which implements TreeTableModel..

Update

public class TableModel extends AbstractTreeTableModel 
                         implements TreeTableModel {
 static protected String[]  cNames = {"TrackNumber", "MWRTN", "LSRTN", "RFTN","TrackStatus","Prova","Prova2"};
    // Types of the columns.
 static protected Class[]  cTypes = {TreeTableModel.class,Integer.class, Integer.class, Integer.class, Integer.class,String.class,String.class};

 private ArrayList<Object> data=new ArrayList<Object>();
     public void insertNode(Object node)
     {    this.data.add(node);    super.setRoot(data.get(0));}

In my main class I add objects to my model in this way:

...
model =new TableModel();
model.insertNode(threatList.get(i)); //inserting the root node
model.addChild(threatList.get(i),threatList.get(j)); // inserting the child
...

Then I pass the model to my JTreeTable and add it to my frame:

treeTable = new JTreeTable(model);
JScrollPane scroll=new JScrollPane(treeTable);
scroll.setAutoscrolls(false);
scroll.setPreferredSize(new Dimension(1000,80));
frame.add(scroll);

And this is the JTreeTable class:

public class JTreeTable extends JTable {
protected TreeTableCellRenderer tree;

public JTreeTable(TreeTableModel treeTableModel) {
super();

// Create the tree. It will be used as a renderer and editor. 
tree = new TreeTableCellRenderer(treeTableModel); 

// Install a tableModel representing the visible rows in the tree. 
super.setModel(new TreeTableModelAdapter(treeTableModel, tree));

// Force the JTable and JTree to share their row selection models. 
tree.setSelectionModel(new DefaultTreeSelectionModel() { 
    // Extend the implementation of the constructor, as if: 
 /* public this() */ {
    setSelectionModel(listSelectionModel); 
    } 
}); 
// Make the tree and table row heights the same. 
tree.setRowHeight(getRowHeight());

// Install the tree editor renderer and editor. 
setDefaultRenderer(TreeTableModel.class, tree); 
setDefaultEditor(TreeTableModel.class, new TreeTableCellEditor());  

setShowGrid(false);
setIntercellSpacing(new Dimension(0, 0)); 
setPreferredSize(new Dimension(60,60));
}

/* Workaround for BasicTableUI anomaly. Make sure the UI never tries to 
 * paint the editor. The UI currently uses different techniques to 
 * paint the renderers and editors and overriding setBounds() below 
 * is not the right thing to do for an editor. Returning -1 for the 
 * editing row in this case, ensures the editor is never painted. 
 */
public int getEditingRow() {
    return (getColumnClass(editingColumn) == TreeTableModel.class) ? -1 : editingRow;  
}

// 
// The renderer used to display the tree nodes, a JTree.  
//

public class TreeTableCellRenderer extends JTree implements TableCellRenderer {

protected int visibleRow;

public TreeTableCellRenderer(TreeModel model) { 
    super(model); 
}

public void setBounds(int x, int y, int w, int h) {
    super.setBounds(x, 0, w, JTreeTable.this.getHeight());
}

public void paint(Graphics g) {
    g.translate(0, -visibleRow * getRowHeight());
    super.paint(g);
}

public Component getTableCellRendererComponent(JTable table,
                           Object value,
                           boolean isSelected,
                           boolean hasFocus,
                           int row, int column) {
    if(isSelected)
           setBackground(table.getSelectionBackground());
    else
           setBackground(table.getBackground());

    visibleRow = row;
    return this;
    }
 }

 // 
 // The editor used to interact with tree nodes, a JTree.  
 //

public class TreeTableCellEditor extends AbstractCellEditor implements TableCellEditor {
    public Component getTableCellEditorComponent(JTable table, Object value,
                             boolean isSelected, int r, int c) {
        return tree;
    }

    @Override
    public Object getCellEditorValue() {
        // TODO Auto-generated method stub
        return null;
    }
}

What I would is to fire an event after adding(or modifying, or removing) a child.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:20:10+00:00Added an answer on May 25, 2026 at 3:20 am

    The model is the class holding your data. It must tell its view each time the data changes, so that the view refreshes itself and shows the new data of the model. This is the goal of the methods fireXxx().

    Like with the other Swing components, when you change the data displayed by the component, you should thus do it by changing the data in the model, and call the appropriate fireXxx methods. The best thing to do is to encapsulate this in the model class, by adding specific methods in your subclass of AbstractTreeTableModel which perform the data modification and fire the appropriate event using one or several calls to fireXxx.

    I suggest you read the Swing tutorial about tables and or trees and then apply what you learn here to your tree table. The idea is the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.