Results 1 to 6 of 6
- 10-18-2010, 01:56 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
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 frameLast edited by raomore; 10-19-2010 at 08:33 AM.
- 10-18-2010, 02:15 PM #2
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.
- 10-18-2010, 02:51 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
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
- 10-18-2010, 03:17 PM #4
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.
- 10-19-2010, 08:36 AM #5
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
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
- 10-19-2010, 01:44 PM #6
Similar Threads
-
[SOLVED] Adding JTree, JTable inside a JTabbedPane
By javanewbie in forum AWT / SwingReplies: 6Last Post: 05-28-2009, 05:32 AM -
JTable in Frame only shows 3 rows
By Laura Warren in forum New To JavaReplies: 4Last Post: 12-24-2008, 05:48 AM -
Is there a way for updating JTree and JTable with a same model?
By Melki in forum AWT / SwingReplies: 0Last Post: 08-29-2008, 12:49 PM -
Is there a way for updating JTree and JTable with a same model?
By Melki in forum AWT / SwingReplies: 0Last Post: 08-29-2008, 12:16 PM -
JSplitPane Arranging Components(JTree) Doubt
By hemanthjava in forum AWT / SwingReplies: 0Last Post: 08-03-2008, 08:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks