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.
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.
Related
I want to make my JTable Non-editable
As I use following code to set rows using SetModel():
jTable1.setModel(DbUtils.resultSetToTableModel(rs)); //Resultset is added as each row using r2xml JAR file
I cant use follwing code:
jTable1.setModel(new DefaultTableModel() {
#Override
public boolean isCellEditable(int row, int column) {
return false;
}
});
Because we cannot use two setModel() for jTable.
How to overcome this problem?
I want to setresult and make jTable Noneditable.
Here are 2 ways to achieve that:
Create and use your own TableModel implementation which forwards all calls to the table model returned by DbUtils except for isCellEditable() in which you can return always false hence disabling editing. Your own table model could get the model returned by DbUtils as a constructor argument for example.
You can extend JTable and override its isCellEditable() method to return false (by default it calls the model's isCellEditable() method). Maybe other Swing enthusiasts will see this as an evil hack, but it is the simplest solution to your problem here.
Elaborating method #1
This is how you can create your model:
class MyModel implements TableModel {
private final TableModel m;
public MyModel(TableModel m) {
this.m = m;
}
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
// This is how we disable editing:
return false;
}
// The rest of the methods just forward to the other model:
#Override
public int getRowCount() {
return m.getRowCount();
}
#Override
public int getColumnCount() {
return m.getColumnCount();
}
// ...and all other methods which I omit here...
}
And this is how you can use it:
jTable1.setModel(new MyModel(DbUtils.resultSetToTableModel(rs)));
Elaboration of method #2
Extending JTable can even be an anonymous class:
JTable jtable1 = new JTable() {
#Override
public boolean isCellEditable(int row, int column) {
// This is how we disable editing:
return false;
}
};
And using it:
// You can set any model, the table will not be editable because we overrode
// JTable.isCellEditable() to return false therefore the model will not be asked
// if editable.
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
you can use this code for make non editable jTable
simply you write one line in your program
jTable.disable();
I want to refresh and display my JTable after a user pressed a button. Before that the button generates an Object[][] in which the filtered data from the table is held.
The filtered data is different only in the number of rows.
I'm using the netbeans UI creator and the only way I could populate the table with data is by defining the abstractTableModel.
Here is the code of my abstractTableModel:
class myTable extends AbstractTableModel{
private String[] stolpci = {"Kategorija","Podkategorija","Opis","Cena","Datum","Boni"};
private Object[][] data = PregledovalnikGUI.vrniTabelo(); /*PregledovalnikGUI.vrniTabelo() returns a value in form of Object[][] in which the data is held*/
public int getColumnCount() {
return stolpci.length;
}
public int getRowCount() {
return vrstice.length;
}
public String getColumnName(int col) {
return stolpci[col];
}
public Object getValueAt(int row, int col) {
return vrstice[row][col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public void setValueAt(Object value, int row, int col) {
vrstice[row][col] = value;
fireTableCellUpdated(row, col);
}
The jTable is set like:
Tabela.setModel(new myTable());
Which methods do I need to define to get the table to refresh at runtime?
inside myTable you could have a method called refresh() something like this
public void refresh(Object[][] objects){
//make the changes to the table, then call fireTableChanged
fireTableChanged(null);
}
Then in the button listener, call the above method:
Tablea.refresh(objects);//objects stores your filtered data
If you create a new TableModel, then nothing, the table will automatically update itself.
If the underlying data of the model is changed, then from within the model (seen as you extending from AbstractTableModl), you could call fireTableDataChanged, which lets the table know that the contents of the table have changed and it should redraw itself.
This may require that the model either have a refresh method of its own or that it has the capability to listen to changes from the data it is modelling
I have a bunch of jComboBox in a panel. What is the best way to cycle through the panel and set setSelectedIndex(0) for each of the controls?
Create a list to keep track of all the combo boxes being added to the panel, and then loop over them. For example:
List<JComboBox> list = new ArrayList<JComboBox>();
JComboBox box = new JComboBox();
panel.add(box);
list.add(box); //store reference to the combobox in list
// Later, loop over the list
for(JComboBox b: list){
b.setSelectedIndex(0);
}
You can iterate over a tree of Components by checking whether each Component is an instance of Container, and if so iterate over the container's child components and so forth. You could wrap this functionality in a ComponentIterator, which is initialised with the root Component in the hierarchy. This would allow you to iterate over a component tree and initialise each JComboBox to a specific value.
However, I would not recommend this "generic" approach as it could have unforeseen results as your code evolves over time. Instead, it would probably make sense to write a simply factory method that creates and initialises your JComboBox; e.g.
private JComboBox createCombo(Object[] items) {
JComboBox cb = new JComboBox(items);
if (items.length > 0) {
cb.setSelectedIndex(0);
}
return cb;
}
Here's the ComponentIterator implementation in case it's of any use:
public class ComponentIterator implements Iterator<Component> {
private final Stack<Component> components = new Stack<Component>();
/**
* Creates a <tt>ComponentIterator</tt> with the specified root {#link java.awt.Component}.
* Note that unless this component is a {#link java.awt.Container} the iterator will only ever return one value;
* i.e. because the root component does not contain any child components.
*
* #param rootComponent Root component
*/
public ComponentIterator(Component rootComponent) {
components.push(rootComponent);
}
public boolean hasNext() {
return !components.isEmpty();
}
public Component next() {
if (components.isEmpty()) {
throw new NoSuchElementException();
}
Component ret = components.pop();
if (ret instanceof Container) {
for (Component childComponent : ((Container) ret).getComponents()) {
components.push(childComponent);
}
}
return ret;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
I have an unusual situation where I need to have a JTree with each node containing 2 checkboxes and a label (with the ability to add a listener to tell when any of the potential checkboxes are checked). I also need the root node to have the same layout (which I'm assuming means creating a JPanel with 2 JCheckBoxes and a JLabel), with the ability to select all the checkboxes down the tree if one in the root is checked.
Any guidance or examples? I've checked out previous questions on here and associated examples...some of which allowed me to get to the point of having the tree "look" but without giving me a direction for implementing the action behind it.
Thanks!
This might be a good time to look at the old JTreeTable code, which will give you a tree rendered in the first column, and the freedom to render the cells for each column to the right of the tree node as you wish, in your case putting in checkboxes and a label, and allowing you to have TableCellEditors working with your JTable as you are used to. A warning is that, while the code in that link works, it is a little convoluted.
There is an alternative. I have demoed below a Tree Table implementation that is supposed to be better, called Outline, provided by NetBeans (though you don't need to develop with the NetBeans IDE, you just need the jar). This article indicates how easy it is to be to get started.
I was able to mock up a quick example of the Outline tree table in Eclipse (with the org-netbeans-swing-outline.jar imported to my project) in about 30 minutes (I am slow at typing):
private void buildFrame() {
frame = new JFrame("Demo");
frame.setSize(300, 300);
addStuffToFrame();
frame.setVisible(true);
}
private void addStuffToFrame() {
MyTreeNode top = new MyTreeNode("top");
createNodes(top);
DefaultTreeModel model = new DefaultTreeModel(top);
//here are the netBeans tree table classes
OutlineModel outlineModel =
DefaultOutlineModel.createOutlineModel(model, new MyRowModel());
Outline outline = new Outline();
outline.setRootVisible(true);
outline.setModel(outlineModel);
frame.getContentPane().add(new JScrollPane(outline));
}
private void createNodes(MyTreeNode top) {
MyTreeNode child = new MyTreeNode("child 2");
top.add(new MyTreeNode("child 1"));
child.add(new MyTreeNode("g-child1"));
child.add(new MyTreeNode("g-child2"));
child.add(new MyTreeNode("g-child3"));
top.add(child);
top.add(new MyTreeNode("child3"));
top.add(new MyTreeNode("child4"));
}
I create a TreeNode to hold the Booleans that will interoperate well with the JTable's built-in checkbox rendering mechnanism.
public class MyTreeNode extends DefaultMutableTreeNode {
Boolean data1 = null;
Boolean data2 = null;
String name = null;
MyTreeNode (String name) {
this.name=name;
}
void setData1(Boolean val) {data1=val;}
void setData2(Boolean val) {data2=val;}
Boolean getData1() {return data1;}
Boolean getData2() {return data2;}
String getName() {return name;}
}
The netBeans RowModel is the key to making this a table instead of a simple JTree:
public class MyRowModel implements RowModel {
public Class getColumnClass(int col) {
switch (col) {
case 0: return String.class;
case 1: return Boolean.class; //these return class definitions will
case 2: return Boolean.class; //trigger the checkbox rendering
default:return null;
}
}
public int getColumnCount() {
return 3;
}
public String getColumnName(int col) {
return "";
}
public Object getValueFor(Object node, int col) {
MyTreeNode n = (MyTreeNode)node;
switch (col) {
case 0: return n.getName();
case 1: return n.getData1();
case 2: return n.getData2();
default:return null;
}
}
public boolean isCellEditable(Object node, int col) {
return col > 0;
}
public void setValueFor(Object node, int col, Object val) {
MyTreeNode n = (MyTreeNode)node;
if (col == 1) {n.setData1((Boolean)val);}
else if (col == 2) {n.setData2((Boolean)val);}
//EDIT: here is a recursive method to set all children
// selected for one of the two checkboxes as it is
// checked by the parent
for (Enumeration children = n.children();
children.hasMoreElements(); ) {
MyTreeNode child = (MyTreeNode) children.nextElement();
setValueFor(child, col, val);
}
}
}
here is the finished, albeit simplistic, product:
alt text http://img17.imageshack.us/img17/6643/picture1hz.png
I have updated the setValueFor method to iterate over a node's children and set the checkboxes as selected or deselected when a parent has been modified.
Take a look at http://www.java2s.com/Code/Java/Swing-JFC/CheckBoxNodeTreeSample.htm
It wasn't clear where the buildFrame(), addStuffToFrame() and createNodes() methods go. I put them all into an OutlineJFrame class I created that extends JFrame, and deleted the 'frame.' preface where-ever it appeared. Then in my project's main() method, it just created one of those OutlineJFrame objects and set its visible to true. When it ran, I got a resizable but empty window. Where were the rows? Where were the nodes?
Then I asked Geertjan, the NetBeans guru, what I was doing wrong, and he sent me a re-write. But it had the same behaviour.
But I know that my java is fine, because another demo project I did (FileTreeJFrame) displays outline.java objects just fine.
In Java how do I get a JList with alternating colors? Any sample code?
To customize the look of a JList cells you need to write your own implementation of a ListCellRenderer.
A sample implementation of the class may look like this: (rough sketch, not tested)
public class MyListCellThing extends JLabel implements ListCellRenderer {
public MyListCellThing() {
setOpaque(true);
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
// Assumes the stuff in the list has a pretty toString
setText(value.toString());
// based on the index you set the color. This produces the every other effect.
if (index % 2 == 0) setBackground(Color.RED);
else setBackground(Color.BLUE);
return this;
}
}
To use this renderer, in your JList's constructor put this code:
setCellRenderer(new MyListCellThing());
To change the behavior of the cell based on selected and has focus, use the provided boolean values.