Results 1 to 1 of 1
- 08-03-2008, 08:29 AM #1
Member
- Join Date
- Dec 2007
- Posts
- 22
- Rep Power
- 0
JSplitPane Arranging Components(JTree) Doubt
I was working on a JTree Example.
The program is shown below for convenience sake
The output shows a JSplitPane with JTree on the top and button Panel in the bottem. The JTree is not well aligned and I am not able to align it entirely in the top panel. I don't even know how by default the output jtree is so small when there is no size setting. How do I correct this program.Java Code:import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JSplitPane; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreePath; import javax.swing.tree.TreeSelectionModel; public class DynamicJTreeDemo extends JFrame{ private static int newNodeCounter; private static String ADD_ACTION = "add"; private static String REMOVE_ACTION = "remove"; private static String CLEAR_ACTION = "clear"; private DefaultMutableTreeNode rootNode; private DefaultTreeModel defaultTreeModel; private JTree jtree; public DynamicJTreeDemo() { // Creates the JTree rootNode = new DefaultMutableTreeNode("Root Node"); defaultTreeModel = new DefaultTreeModel(rootNode); jtree = new JTree(defaultTreeModel); jtree.setEditable(true); jtree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION); jtree.setShowsRootHandles(true); initializeTree(); JScrollPane jscrollPane = new JScrollPane(jtree); JPanel treePanel = new JPanel(); treePanel.add(jscrollPane); JPanel buttonPanel = new JPanel(); JButton jbnAdd = new JButton("Add"); jbnAdd.setActionCommand(ADD_ACTION); // jbnAdd.addActionListener(this); buttonPanel.add(jbnAdd, BorderLayout.WEST); JButton jbnRemove = new JButton("Remove"); jbnRemove.setActionCommand(REMOVE_ACTION); // jbnRemove.addActionListener(this); buttonPanel.add(jbnRemove, BorderLayout.SOUTH); JButton jbnClear = new JButton("Clear"); jbnClear.setActionCommand(CLEAR_ACTION); // jbnClear.addActionListener(this); buttonPanel.add(jbnClear, BorderLayout.EAST); JSplitPane jsplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, treePanel, buttonPanel); //Lay everything out. // setPreferredSize(new Dimension(300, 150)); getContentPane().add(jsplitPane); } private static void createAndShowGUI() { DynamicJTreeDemo dynamicJTreeDemo = new DynamicJTreeDemo(); dynamicJTreeDemo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); dynamicJTreeDemo.setTitle("Dynamic JTree Demo"); dynamicJTreeDemo.pack(); dynamicJTreeDemo.setVisible(true); } public void initializeTree() { String p1 = new String("Parent 1"); //p1 = Parent 1 String p2 = new String("Parent 2"); String c1 = new String("Child 1"); String c2 = new String("Child 2"); DefaultMutableTreeNode p1Node = null, p2Node = null; p1Node = addObject(null, p1, false); p2Node = addObject(null, p2, false); addObject(p1Node, c1, false); addObject(p1Node, c2, false); addObject(p2Node, c1, false); addObject(p2Node, c2, false); } public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Object child, boolean isVisible) { DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child); if (parent == null) { parent = rootNode; } defaultTreeModel.insertNodeInto(childNode, parent, parent .getChildCount()); if (isVisible) { jtree.scrollPathToVisible(new TreePath(childNode.getPath())); } return childNode; } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
Source of the Program is from iwebieLast edited by hemanthjava; 08-03-2008 at 08:31 AM. Reason: removed the unwanted parts of the program
Similar Threads
-
A Simple JTree Example
By Java Tip in forum javax.swingReplies: 0Last Post: 06-27-2008, 07:41 PM -
JSplitPane nightmare
By SwinGirl in forum SWT / JFaceReplies: 1Last Post: 05-01-2008, 08:24 PM -
JTree trouble
By Alantie Vala in forum AWT / SwingReplies: 3Last Post: 07-31-2007, 11:12 PM -
how to display data in Jtree
By paty in forum New To JavaReplies: 1Last Post: 07-24-2007, 12:28 AM -
I need create a JSplitPane
By Daniel in forum AWT / SwingReplies: 1Last Post: 07-05-2007, 06:12 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks