• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 723
  • Last Modified:

help with JTree and JTabbedPane


Hello there,

I have a Jtree which gets populated from the database.on the right side i have 3 JTabbedPane which contains JTable inside it.the table also gets populated from the DB.Nowwhen i click on the nodes,the tabpane do not change,i have attached my code which does most of the bits,so experts can tell me what i am missing or doing wrong
for example.

JTree                         JTabbedPane
-xxx                           xxx     yyy    zzz
   -yyy
     -zzz

public class Policy extends JInternalFrame implements ActionListener, TreeSelectionListener
{      
      Frame JFParentFrame;
      Dimension screen =       Toolkit.getDefaultToolkit().getScreenSize();
      //private DynamicTree treePanel;
      //private static final String SWITCH = "Switch";
      private JScrollPane treeScroll;
    private DefaultMutableTreeNode dbNode;      // Root node for the database tree
    private DefaultTreeModel dbTreeModel;
   
    DefaultMutableTreeNode tableNode,tableNode1,tableNode2;
    private JTree dbTree;
    private JPanel pN;
    private CardLayout layoutMgr;
    //private JSplitPane split;
   
    JPanel cityPanel;
    JPanel coPanel;
    JPanel dslamPanel;
    JPanel linecardPanel;
   
       
    Connection conn;
    Statement state;
    Statement state1;
    Statement state2;
    Statement state3;
   
    String tableName;
   
    class TreeListener implements TreeSelectionListener
    {
        public void valueChanged(TreeSelectionEvent tse)
        {
            handleTreeSelection();
        }
    }
   
    private void handleTreeSelection()
    {
        Object node = dbTree.getLastSelectedPathComponent();
        if (node == null || !(node instanceof TreeNode) || node.toString() == null)
            return;
        System.out.print(node.toString());
        layoutMgr.show(pN, node.toString());
    }
   
      public Policy(JFrame getParentFrame)
      {
            super("Records",false,true,false,true);
            cityPanel = new JPanel();
            coPanel = new JPanel();
            dslamPanel = new JPanel();
            linecardPanel = new JPanel();
            
            JFParentFrame = getParentFrame;
            //treePanel = new DynamicTree();
            String[] switchTabs = { "City", "Co", "DSLAM", "Line Card" };
        JPanel[] switchPanels = { cityPanel, coPanel, dslamPanel, linecardPanel};
        JTabbedPane switchPane = createNodePane(switchTabs, switchPanels);
            //populateTree(treePanel);
            
            //Container c = this.getContentPane();
            //c.add( treePanel, BorderLayout.WEST );
            
            layoutMgr = new CardLayout();
            pN = new JPanel(layoutMgr);
            pN.setPreferredSize(new Dimension(200, 200));
            pN.add(switchPane, "tableName");
            
            dbNode = new DefaultMutableTreeNode("No database");
        dbTreeModel = new DefaultTreeModel(dbNode);
        dbTree = new JTree(dbTreeModel);
        //dbTree.addTreeSelectionListener(this);
        dbTree.addTreeSelectionListener(new TreeListener());
        treeScroll = new JScrollPane(dbTree);
            
            //Add the scroll panes to a split pane.
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT );
        splitPane.setLeftComponent( treeScroll);
        splitPane.setRightComponent(pN);

            Dimension minimumSize = new Dimension(100, 50);
            //pN.setMinimumSize(new Dimension(0, 0));
            treeScroll.setMinimumSize(minimumSize);
        splitPane.setDividerLocation(200);
       
        //treeView.setPreferredSize(new Dimension(100, 100));
        splitPane.setPreferredSize(new Dimension(200, 300));
       
        String query = "select * from city ORDER BY CITY_ID";
        JDBCAdapter dt = new JDBCAdapter(url, driver, userid, pass);
        dt.executeQuery(query);

        // Create the table
        JTable tableView = new JTable(dt);

        JScrollPane scrollpane = new JScrollPane(tableView);
        scrollpane.setPreferredSize(new Dimension(500, 300));
        cityPanel.add(scrollpane);
            getContentPane().add(splitPane);
        //c.add( splitPane, BorderLayout.WEST );
                        
            setSize(747,450);
            setLocation((screen.width - 747)/2,((screen.height-450)/2)-45);
            setFrameIcon(new ImageIcon("images/customer.png"));      
0
zolf
Asked:
zolf
  • 3
  • 2
2 Solutions
 
objectsCommented:
you need to make the tabbed pane a member variable and change the selected tab in your listener.

http://java.sun.com/docs/books/tutorial/uiswing/components/tabbedpane.html#tabapi
0
 
zolfAuthor Commented:

hi objects,

could you please explain in more details or help me modify my code above to make it work.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> JTabbedPane switchPane = createNodePane(switchTabs, switchPanels);

Should not be declared inside the method. You should declare it as a member of a class along with JFParentFrame.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
zolfAuthor Commented:

what should i do now.

the method createNodePane(switchTabs, switchPanels) is defined in the same class Policy

private static JTabbedPane createNodePane(String[] tabs, Component[] items)
      {
        // assume that tabs and items have the same
        // length and contain no null values
        JTabbedPane tp = new JTabbedPane();
        tp.setPreferredSize(new Dimension(200, 200));
        for (int i = 0; i < tabs.length; i++)
            tp.addTab(tabs[i], items[i]);
        return tp;
    }
0
 
zolfAuthor Commented:

Thanks mate, made that thing to work.can you'll suggest me how can i change the tabs based on the nodes.i mean if i click on the yyy

JTree                         JTabbedPane
-xxx                           xxx     yyy    zzz
   -yyy
     -zzz


yyy node the yyy tab should be shown,then again if i click the xxx node the tab should go back to the xxx tab.same thing goes for the zzz nodes.
0
 
objectsCommented:
you need to keep something like a map of node text -> tab component
and use the setSelectedComponent method
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now