jtree

Jtable as a Jtree Node

女生的网名这么多〃 提交于 2019-11-28 12:11:06
I know you can create a table and add a JTree as a column . But what I want to do is the complete opposite. Check the image and tell me if this is possible. Thanks! UPDATE: By using MKorbel's code and by randomizing the number of columns with the following code: @Override public int getColumnCount() { int i = (int) (Math.random( )* 10.0); if (i%2 ==0) return 2; else return 3; } I was able to get the following image: mKorbel 1) I didn't solve isLeaf() in Renderer and Editor somehow 2) if I put JScrollPane with JTable to the Node directly, (without using JPanel as parent ) then JTree View isn't

Java detect CTRL+X key combination on a jtree

末鹿安然 提交于 2019-11-28 10:52:02
I need an example how to add a keyboard handler that detect when Ctrl + C , Ctrl + X , Ctrl + C pressed on a JTree . I were do this before with menu shortcut keys but with no success. You can add KeyListeners to any component (f) f.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { if ((e.getKeyCode() == KeyEvent.VK_C) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) { System.out.println("woot!"); } } @Override public void keyReleased(KeyEvent e) { } }); MOHAMED FATHEI Use KeyListener for example : jTree1

How to update JTree elements

☆樱花仙子☆ 提交于 2019-11-28 10:50:25
问题 I use JTree with TreeNode which extending DefaultMutableTreeNode.when I add new node,i cant update JTree.Any help will be appreciated 回答1: Adding to the node only is not enough, the Tree needs to be notified. The correct way to do this is to let the model take care of adding/removing nodes, as it will notify correctly the Tree on the way. In case of a DefaultTreeModel (which should be the one used in the JTree if you haven't made your own TreeModel), you have the method insertNodeInto

How do you make components of JPanel as a node in JTree usable?

别等时光非礼了梦想. 提交于 2019-11-28 09:59:08
问题 When I click on the JButton nothing happens. This applies even for a JScrollPane that I put in (it shows, but will not scroll). Why is the JPanel not at the front? I get the sense that something has to be overridden. Should it be the expansion of the part that is not the arrow? If so, how is that done? import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.tree.*; public class test { public test() { JTree tree =

TreeCellRenderer: how to set background color?

别来无恙 提交于 2019-11-28 08:08:35
问题 I've written a custom TreeCellRenderer in order to change a components appearance. Everything works fine, except that setBackground has no effect. The code is definitely executed as the foreground color always changes correctly. Since selected items are rendered in blue and deselected item in white (without having written that code myself), I assume that my changes are overridden by JTree. So what would be the proper way to change the background color? This is essentially my code: JTree tree

How can I refresh a JTree after adding some nodes to the underlying model?

牧云@^-^@ 提交于 2019-11-28 07:36:56
First of all, let me say that I dont use the DefaultTreeModel. I implement my own TreeModel, so i cant use the DefaultXXX stuff. The problem is this: Through some addStuff() methods which my model defines I add nodes to the underlying data structure. I then notify listeners by calling treeNodesChanged() inside the addStuff() function (I Know there are treeNodesInserted methods but it is the same thing. It just notifies listeners with a different method). Now, one of the listeners is a static class in my main form and this listener can tell the JTree, which is also contained in my main form, to

How do I auto-expand a JTree when setting a new TreeModel?

眉间皱痕 提交于 2019-11-28 02:38:28
问题 I have a custom JTree and a custom JModel ; I would for the JTree to "auto-expand" when I give it a new model. At the moment, it simply collapse all the nodes to the root. Here is an example: private class CustomTree extends JTree { @Override public boolean isExpanded(TreePath path) { return ((Person) path.getLastPathComponent).hasChildren(); } private class CustomTreeModel extends TreeModel { // ... omitting various implementation details @Override public boolean isLeaf(Object object) {

Set icon to each node in Jtree

久未见 提交于 2019-11-28 00:07:05
问题 I want to set for each node in my JTree a different icon, actually I'm loading each node from a data base, with a "while", I set each icon like a root, leaf or parent. Like this: All my declarations are global: private ResultSet myResultSet; protected DefaultTreeModel treeModel; private DefaultMutableTreeNode rootNode,childNode,parent1,parent2; And this is the code where I set my nodes: myResultSet=rtnNodes(); /*Method that returns a RS with my nodes*/ while(myResultSet.next()){ switch

Where are these error and warning icons as a java resource?

醉酒当歌 提交于 2019-11-27 22:09:15
I've got a custom tree cell renderer that I'm using to render custom icons a JTree, and I really like the warning icon and the error icon that JOptionPane displays for both warning messages and error messages respectively. Obviously I can use the following code to get the icons for my own use, but this is way heavy handed and requires me to instantiate dialogs that I'm never going to use: public class ValidationCellRenderer extends DefaultTreeCellRenderer { private Icon warnIcon; private Icon errorIcon; public ValidationCellRenderer() { JOptionPane optionPane = new JOptionPane(new Object(),

Java: How to display an XML file in a JTree

心不动则不痛 提交于 2019-11-27 21:42:08
I would like to have a way to display the contents of an XML file in a JTree . I have already accomplished this using DOM, by implementing a custom TreeModel (and TreeCellRenderer ). However it is very clunky (much workaround-ery and hackery) and rather rough around the edges. Is anyone aware of a way to get a JTree to display the contents of an XML file, parsed with SAX? Thanks! Here's the code that I use. It is based on the API of Dom4J, but you can easily convert it to the APIs of your favorite XML library: public JTree build(String pathToXml) throws Exception { SAXReader reader = new