JTreeTable problem - JTree not showing inside the JTable

A

Abraham Khalil

You need to download
<http://java.sun.com/products/jfc/tsc/articles/treetable2/downloads/src.zip>
to compile the source codes below:

The problem is it shows right with JTree but when I use JTreeTable, it
doesn't show the tree in the JTable.

In MyTreeTable class, if I change
JTreeTable treeTable = new JTreeTable(new MyTreeTableModel());
TO:
JTree treeTable = new JTree(new MyTreeTableModel());

It shows the tree correctly. The tree shows Accounts as a root node,
follow by list of accounts once user click Accounts. For each account
click it shows the email address correctly. It works using JTree but
not using JTreeTable and need some help to see why:

Below are the classes I'am working with the src.zip download

Thanks


---------- Accounts.java -----------

import java.util.*;

import javax.swing.tree.*;

public class Accounts {
private ArrayList accounts = new ArrayList();


public Accounts() {
Account account1 = new Account("Yahoo", "POP3/SMTP");
account1.addEmailAddress("(e-mail address removed)");
account1.addEmailAddress("(e-mail address removed)");

Account account2 = new Account("EDS", "POP3/SMTP");
account2.addEmailAddress("(e-mail address removed)");
account2.addEmailAddress("(e-mail address removed)");

Account account3 = new Account("NOFFCROFF", "POP3/SMTP");
account3.addEmailAddress("(e-mail address removed)");
account3.addEmailAddress("(e-mail address removed)");
account3.addEmailAddress("(e-mail address removed)");
account3.addEmailAddress("(e-mail address removed)");


accounts = new ArrayList();
accounts.add(account1);
accounts.add(account2);
accounts.add(account3);
}


public ArrayList getAccounts() {
return accounts;
}


public String toString() {
return "Accounts";
}
}



---------- Account.java -----------


import java.util.*;

import javax.swing.tree.*;


public class Account {
private String accountName = null;
private String accountType = null;
ArrayList emails = null;

public Account(String accountName, String accountType) {
this.accountName = accountName;
this.accountType = accountType;

emails = new ArrayList();
}


public String getAccountName() {
return accountName;
}


public String getAccountType() {
return accountType;
}


public String[] getEmailAddresses() {
return ((String[]) emails.toArray(new String[0]));
}


public ArrayList getEmails() {
return emails;
}


public void addEmailAddress(String emailAddress) {
emails.add(emailAddress);
}


public String toString() {
return getAccountName();
}
}



---------- MyTreeTable.java -----------


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class MyTreeTable {

public static void main(String[] args) {
final int WIDTH = 600;
final int HEIGHT = 400;
final int LOCX =
(Toolkit.getDefaultToolkit().getScreenSize().width - WIDTH) / 2;
final int LOCY =
(Toolkit.getDefaultToolkit().getScreenSize().height - HEIGHT) / 2;

JFrame retFrame = new JFrame("TreeTable Example");
retFrame.setBounds(LOCX, LOCY, WIDTH, HEIGHT);

retFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});

retFrame.getContentPane().setLayout(new BorderLayout());
JTreeTable treeTable = new JTreeTable(new MyTreeTableModel());
retFrame.getContentPane().add(treeTable, BorderLayout.CENTER);
retFrame.setVisible(true);
}
}



---------- MyTreeTableModel.java -----------


import java.util.*;
import javax.swing.tree.*;


public class MyTreeTableModel extends AbstractTreeTableModel {
private final String[] ACCOUNT_COLUMNS = {"Accounts", "User
Name"};


public MyTreeTableModel() {
super(new Accounts());
}


/**
* Returns the number of children of <code>node</code>.
*/
public int getChildCount(Object node) {
Object[] children = getChildren(node);
return (children == null) ? 0 : children.length;
}



/**
* Returns the child of <code>node</code> at index <code>i</code>.
*/
public Object getChild(Object node, int i) {
return getChildren(node);
}



protected Object[] getChildren(Object node) {
if (node instanceof Accounts) {
Accounts accounts = (Accounts) node;
return accounts.getAccounts().toArray();
}
else
if (node instanceof Account) {
Account account = (Account) node;
return account.getEmails().toArray();
}
else {
return null;
}
}



/**
* Returns the value of the particular column.
*/
public Object getValueAt(Object node, int column) {
return getChildren(node) [column];


/*
// Not sure how to implement this if need to for JTable column
to get Account or email

switch(column) {

case 0:
return (node instanceof Accounts) ? "Accounts" : null;
case 1:
return (node instanceof Account) ? ((Account)
node).getAccountName() : null;
case 2:

}

return null;
*/
}



/**
* Returns the number of columns.
*/
public int getColumnCount() {
return ACCOUNT_COLUMNS.length;
}



/**
* Returns the name for a particular column.
*/
public String getColumnName(int column) {
return ACCOUNT_COLUMNS[column];
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top