Results 1 to 3 of 3
  1. #1
    someone5 is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Getting value from another class

    Hi

    I'm trying to get the value of a listbox from another class and to set the value of it in a text box which is also in another class.

    How do I get the value of the listbox and set it in a textbox in another class?

    Thanks.

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

    Default

    What have you tried? Where's your SSCCE?

    Pass a reference of one class into another. Then call a getter/setter for the text as necessary.

  3. #3
    someone5 is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    This is the class where the textbox is:

    Java Code:
    public class MyForm extends JPanel
    {
                 ...
                 JTextField CourseCode = new JTextField( 20 );
    	... 
    	public MyForm()
    	{
    		super();
    		
    		GridBagLayout gridbag = new GridBagLayout();
    		GridBagConstraints c1 = new GridBagConstraints();
    		this.setLayout( gridbag );
    		c1.weightx = 1.0;
    		c1.weighty = 1.0;
    		c1.ipadx = 2;
    	 
    		.... 
    		
                              //Add course code
    		c1.gridx = 0;
    		c1.gridy = 3;
    		c1.anchor = GridBagConstraints.EAST;
    		gridbag.setConstraints( CourseCodeLbl, c1 );
    		this.add( CourseCodeLbl );
    		c1.gridx = 1;
    		c1.anchor = GridBagConstraints.WEST;
    		gridbag.setConstraints( CourseCode, c1 );
    		this.add( CourseCode );
    	             ....
    	}
     
    	}
    This is the class where the list box is:

    Java Code:
                  .....
    
    public class DeptCourseChooser extends JPanel {
                   ....
        
                JList listtwo = new JList();
    
                  .....
    This is the class where the value of the listbox is taken and shown in the textbox. This does not work:

    Java Code:
       .....
    public class Registration extends JPanel {
        
    
         
    
         String UcasCodeComp[] = {"G510", "G511", "G400", "G401",
             "G451", "G452", "G530", "G531", "G403", "G500", "G501",  "G502", "G503",
              "G430", "G431", "G421", "G440", "G441", "G601", "G602", "GH76", "GH7P"};
    
               String UcasCodeMedia[] = {"WW26", "WW2P", "P311", "P303",
              "P3W6", "P301", "GW4F", "WP63", "GW46", "HP63", "P300", "P301", "P312"};
    
                  String UcasCodeCreative[] = {"G450", "G443","G473", 
              "GW41", "GW42"};
    
                  String UcasCodeMaths[] = {"GG14", "GG1K"};
    
                          
        
        //constructor
        Registration() {
    
            super();
                
                 
                DeptCourseChooser dcc = new DeptCourseChooser(); 
                                             
                MyForm mf = new MyForm();
                    
    
             
                dcc.listtwo.getValue(new ListSelectionListener() {
    
    
                public void valueChanged(ListSelectionEvent event) {
                    
                    int index = dcc.listtwo.getSelectedIndex();
    
                  
                    if (index == 0 ){
                        
                        for (int i = 0; i < UcasCodeComp.length; i++)
    
                       
                          UcasCodeComp[i] = mf.CourseCode.setText();
        
                    }
                    else if (index == 1){
                       
                         for (int i = 0; i < UcasCodeMedia.length; i++)
                        
                           UcasCodeMedia[i] = mf.CourseCode.setText();
                    }
                    else if (index == 2){
                        
                        for (int i = 0; i < UcasCodeCreative.length; i++)
                        
                         UcasCodeCreative[i] = mf.CourseCode.setText();
                    }
                    else if (index == 3){
                        
                        for (int i = 0; i < /*selected.*/UcasCodeMaths.length; i++)
                        
                         UcasCodeMaths[i] = mf.CourseCode.setText();
                    }
                }
            } );
        }
    }

Similar Threads

  1. Replies: 2
    Last Post: 05-01-2010, 08:59 AM
  2. Replies: 8
    Last Post: 04-14-2010, 05:49 AM
  3. Replies: 1
    Last Post: 01-03-2010, 09:48 PM
  4. Replies: 7
    Last Post: 05-06-2009, 12:42 PM
  5. Replies: 8
    Last Post: 03-21-2009, 09:34 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
  •