2 Replies - 657 Views - Last Post: 25 May 2011 - 03:16 PM Rate Topic: -----

#1 johnathanc456  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 53
  • Joined: 05-May 10

JLabel not printing correctly in GUI

Posted 25 May 2011 - 12:11 PM

I am having trouble geting my JLabel to print on the SouthPanel of boarder layout. The JLabel prints correctly for the Selectable tree class, but for the other 3 classes it does not work, am i referencing it wrong or not using the correct syntax? I first thought it was because i was missing the return statement but when i aded it it didnt change anything. any help is greatly appreciated.


Here is my code:


import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import javax.swing.JPanel;

public class Trees 
{  public static void main(String[] args)
   {  SelectableTree tree = new SelectableTree();
   }
}
 
// The tree class to be used as the framework for cs258 lab3.

class SelectableTree extends JFrame implements TreeSelectionListener
{  private JTree       tree;         // Reference tree that will be created.
   private JLabel      selectField;  // Reference label for messages.
   private JScrollPane scrollArea;   // Reference scroll area to display tree.
   private int         childName = 1;// Node name.
   

// The constructor method.

   public SelectableTree()
   {  super("JTree Selections"); // Set the title line.
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Enable the exit button.

// Create the tree nodes.

      DefaultMutableTreeNode grandChild, child;     // References to tree nodes.
      DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); 

// In a loop create children and grandchildren. 
// Link the children to the root and the grandchildren to the children.

      for (int childX=1; childX<=4; childX++)
      {  child = new DefaultMutableTreeNode("Node " + childName++);
         root.add(child);
         for (int grandX=1; grandX<=4; grandX++)
         {  grandChild = new DefaultMutableTreeNode("Node " + childName++);
            child.add(grandChild);
         }
      }

// Instantiate the tree and add the selection listener.

      DefaultTreeModel treeModel = new DefaultTreeModel(root);
      tree      = new JTree(treeModel);
      tree.addTreeSelectionListener(this);
      for (int row = 0; row < tree.getRowCount(); row++) { tree.expandRow(row); }

// Construct the frame that will be displayed.
      //container buttons = getcontentpane();
      

      Container content = getContentPane();         // JFrame default root panel.
      JTabbedPane tabs = new JTabbedPane();
      JPanel treeChanges = new TreeChangePanel(treeModel, tree);
      JPanel CollapseNodes = new CollapseNodesPane(treeModel, tree);
      JPanel CollapseTree = new CollapseTreePane(treeModel, tree);
      
      
      scrollArea        = new JScrollPane(tree);    // Instantiate scroll area.
      selectField       = new JLabel("Current Selection: NONE"); // Output Label.
      
      tabs.addTab("Modify", treeChanges);
      tabs.setSelectedIndex(0);
      tabs.addTab("Expand/Collapse Nodes", CollapseNodes);
      tabs.addTab("Expand/Collapse Tree", CollapseTree);
      
// Add the scroll pane and the text field to the JFrame container.
      
      content.add(tabs, BorderLayout.NORTH);
      content.add(scrollArea, BorderLayout.CENTER);
      content.add(selectField, BorderLayout.SOUTH);

      setSize(500, 700);  // Set the JFrame window size.
      setVisible(true);   // Make everything visible.
   }

// Method to respond to tree selection changes.

   public void valueChanged(TreeSelectionEvent event) 
   {  selectField.setText( "Current Selection: " +  
              tree.getLastSelectedPathComponent().toString());
   }
}

class TreeChangePanel extends JPanel implements ActionListener
{
    JButton add = new JButton("Add");
    JButton remove = new JButton("Remove");
    JButton change = new JButton("Change");
    
    JTree tree = null;
    DefaultTreeModel TreeModel = null;
    TreePath p = null;
    DefaultMutableTreeNode selection;
    JLabel selectField       = new JLabel("Current Selection: NONE");
    
    int count =0;
    
    public TreeChangePanel(DefaultTreeModel TreeModelX, JTree JTreex)
    {
        TreeModel = TreeModelX;
        tree = JTreex;
        add(add);
        add(remove);
        add(change);
        add.addActionListener(this);
        remove.addActionListener(this);
        change.addActionListener(this);
        
        
    }  
    
    public void actionPerformed(ActionEvent event)
    {
        
       selection = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
       if(selection == null){ selectField.setText( "Nothing Selected");return;}
       //if(selection.toString().equals("Root")){System.out.println("cannot modify root!!"); return;}
        
        if (event.getSource()==add)
        {
            DefaultMutableTreeNode newN = new DefaultMutableTreeNode("Node"+ selection.getChildCount()+1);
            TreeModel.insertNodeInto(newN,selection,selection.getChildCount());
            
            p = new TreePath(newN.getPath());
            tree.scrollPathToVisible(p);
            
        }
        else if(event.getSource()==remove)
        {
            if(selection.toString().equals("Root")){selectField.setText("cannot modify root!!");return;}
            count = count - 1;
            if(count < 0){count = 0;}
            if(selection.isLeaf())
            {    
                DefaultMutableTreeNode parent = (DefaultMutableTreeNode)selection.getParent();
                tree.setSelectionPath(new TreePath(parent.getPath()));
                TreeModel.removeNodeFromParent(selection);
            }else{selectField.setText("Must select leaf.");return;}    
            
            
        }    
        else if(event.getSource()==change)
        {
            
            if(selection.toString().equals("Root")){selectField.setText("cannot modify root!!"); return;}
            
            count =count + 1;
            
           selection.setUserObject("ModifiedNode "+ count);
           TreeModel.nodeChanged(selection);
           
            
        }
    
    }
    
}

class CollapseNodesPane extends JPanel implements ActionListener
{
    
       
    JButton Expand = new JButton("Expand");
    JButton Collapse = new JButton("Collapse");
    
    
    JTree tree = null;
    DefaultTreeModel TreeModel = null;
    TreePath p = null;
    DefaultMutableTreeNode selection;
    JLabel selectField = new JLabel();
    
    public CollapseNodesPane(DefaultTreeModel TreeModelX, JTree JTreex)
    {
        TreeModel = TreeModelX;
        tree = JTreex;
        add(Expand);
        add(Collapse);
        
        Expand.addActionListener(this);
        Collapse.addActionListener(this);
        
        
    }  
    
    public void actionPerformed(ActionEvent event)
    {
       selection = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
       if(selection == null){selectField.setText("nothing selected");}
       if(selection.toString().equals("Root")){selectField.setText("cannot modify root!!"); return;}
        
        if (event.getSource()==Expand)
        {
            tree.expandPath(new TreePath(selection.getPath()));
            
        }
        else if(event.getSource()==Collapse)
        {
            tree.collapsePath(new TreePath(selection.getPath()));
        }   
    }   
            
}

class CollapseTreePane extends JPanel implements ActionListener
{
     
     JButton Expand = new JButton("Expand");
    JButton Collapse = new JButton("Collapse");
    
    
    JTree tree = null;
    DefaultTreeModel TreeModel = null;
    TreePath p = null;
    DefaultMutableTreeNode selection;
    JLabel selectField = new JLabel();
    
    public CollapseTreePane(DefaultTreeModel TreeModelX, JTree JTreex)
    {
        TreeModel = TreeModelX;
        tree = JTreex;
        add(Expand);
        add(Collapse);
        
        Expand.addActionListener(this);
        Collapse.addActionListener(this);
        
        
    }  
    
    public void actionPerformed(ActionEvent event)
    {
       selection = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
       if(selection == null){selectField.setText("nothing selected");return;}
       if(selection.toString().equals("Root"))
       {
        
        if (event.getSource()==Expand)
        {
            for (int row = 0; row < tree.getRowCount(); row++)
            { 
                tree.expandRow(row); 
            }
            
            
        }
        else if(event.getSource()==Collapse)
        {
            for (int row = 0; row < tree.getRowCount(); row++)
            {    
                tree.collapseRow(row);
            }    
        }
       }else{selectField.setText("select the tree root.");return; }
   }  
}





Is This A Good Question/Topic? 0
  • +

Replies To: JLabel not printing correctly in GUI

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1376
  • View blog
  • Posts: 3,027
  • Joined: 05-April 11

Re: JLabel not printing correctly in GUI

Posted 25 May 2011 - 12:17 PM

in your CollapseNodesPane you never add the JLabel.
Was This Post Helpful? 0
  • +
  • -

#3 exiles.prx  Icon User is offline

  • D.I.C Head

Reputation: 65
  • View blog
  • Posts: 239
  • Joined: 22-November 10

Re: JLabel not printing correctly in GUI

Posted 25 May 2011 - 03:16 PM

The label is added to the content pane on line 77. I would try setting your content pane layout manager to Border Layout. I've had issues where certain components wouldn't be displayed using border layout until I set the layout manager to border layout (even though border layout is the default layout).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1