Results 1 to 6 of 6
  1. #1
    raomore is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default Jtree in JSplitPane + Jtable in the same frame

    Hi,

    I am implementing an application using Jtree and JSplitPane .On clicking the node on the the tree a new Jtable should be formed on the right side of the JSplitpane.It should not create new fram rather the table should be created on the existing frame only and the table should be updated when a new node is clicked.

    I am posting a sample program which creates Jtree in Jsplit pane

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import javax.swing.event.*;
    import javax.swing.tree.*;
    public class SimpleTree_2 extends JFrame implements TreeSelectionListener{
    public SimpleTree_2() {
    initComponents();
    }
    private void initComponents() {
    splipane = new JSplitPane();
    scroller2 = new JScrollPane();
    textarea = new JTextArea();
    setDefaultCloseOperation(WindowConstants.EXIT_ON_C LOSE);
    setTitle("Simple Tree");
    textarea.setBackground(new Color(255, 255, 204));
    textarea.setEditable(false);
    textarea.setBorder(new TitledBorder("Selected Nodes"));
    scroller2.setViewportView(textarea);
    splipane.setRightComponent(scroller2);
    root = new DefaultMutableTreeNode("root", true);
    n1 = new DefaultMutableTreeNode("node 1", true);
    n2 = new DefaultMutableTreeNode("node 2" , true);
    n3 = new DefaultMutableTreeNode("node 3" , true);
    n4 = new DefaultMutableTreeNode("node 4" , true);
    n5 = new DefaultMutableTreeNode("node 5" , true);
    root.add(n1);
    n1.add(n2);
    root.add(n3);
    n3.add(n4);
    n4.add(n5);
    tree = new JTree(root);
    tree.addTreeSelectionListener(this);
    treeScroller = new JScrollPane((JTree)tree);
    treeScroller.setPreferredSize(new Dimension(200,100));
    splipane.setLeftComponent(treeScroller);
    getContentPane().add(splipane, BorderLayout.CENTER);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((screenSize.width-400)/2, (screenSize.height-300)/2, 400, 300);
    }
    public void valueChanged(TreeSelectionEvent tse) {
    DefaultMutableTreeNode dmtn;
    TreePath path = tree.getSelectionPath();
    if (path != null) {
    dmtn = (DefaultMutableTreeNode) path.getLastPathComponent();
    textarea.append(dmtn.getUserObject().toString()+"\ n");
    }
    }
    public static void main(String args[]) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    new SimpleTree_2().setVisible(true);
    }
    });
    }
    private JTree tree;
    private DefaultMutableTreeNode root, n1, n2, n3, n4, n5;
    private JTextArea textarea;
    private JScrollPane treeScroller, scroller2;
    private JSplitPane splipane;
    }


    I can change the valueChanged method to give different class name but it invokes a table in a new frame.

    public void valueChanged(TreeSelectionEvent tse) {
    DefaultMutableTreeNode dmtn;
    TreePath path = tree.getSelectionPath();
    if (path != null) {
    dmtn = (DefaultMutableTreeNode) path.getLastPathComponent();
    String str = dmtn.getUserObject().toString();
    textarea.append(str+"\n");
    if( str.trim().equalsIgnoreCase("node 2") ){
    TableBasic frame = new TableBasic();
    frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
    frame.pack();
    frame.setLocationRelativeTo( this );
    frame.setVisible(true);




    }
    }
    }



    Can anyone explain me how i can implement this so that the table should be invoked in the same frame
    Last edited by raomore; 10-19-2010 at 08:33 AM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default

    Why can't you just add both to a JPanel, then add the JPanel to the JSplitPane?

    PS- When posting code, make sure you use the code tags to preserve formatting, or else nobody will read it.

  3. #3
    raomore is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default JTree in Jsplitpane + JTable in the same frame

    Hi,

    Thanks for the replay but i couldn't get what u r saying as i am new to java and swings. Can u please modify that code and sent to me or please elaborate.
    U can take any JTable example and integrate it with my posted code so that when the node is clicked the JTable should be showed on same GUI frame

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default

    No. This isn't a code service.

    Recommended reading: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Read through that (the whole thing, carefully, it might take you a few hours or a couple days), and come back with a more specific question.

  5. #5
    raomore is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    Hi,

    Can Anyone else explain how the the Jtable can be showed in the same frame with the sample code when we are using JTree In JSplitPane

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default

    Why can't you just add both to a JPanel, then add the JPanel to the JSplitPane?

Similar Threads

  1. [SOLVED] Adding JTree, JTable inside a JTabbedPane
    By javanewbie in forum AWT / Swing
    Replies: 6
    Last Post: 05-28-2009, 05:32 AM
  2. JTable in Frame only shows 3 rows
    By Laura Warren in forum New To Java
    Replies: 4
    Last Post: 12-24-2008, 05:48 AM
  3. Replies: 0
    Last Post: 08-29-2008, 12:49 PM
  4. Replies: 0
    Last Post: 08-29-2008, 12:16 PM
  5. JSplitPane Arranging Components(JTree) Doubt
    By hemanthjava in forum AWT / Swing
    Replies: 0
    Last Post: 08-03-2008, 08:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •