Closed Thread
Results 1 to 6 of 6

Thread: Help with JList and TextArea...

  1. #1
    gabrielle_l is offline Newbie
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Help with JList and TextArea...

    So far my app has an empty jlist and it can also open a file and display it in the text area. I want my app to do the following: when u click on different elements in the jlist, they display different things in the text area. The text area should come from a file in the computer. So when you open a file and display in the text area, i want to save it as an element in the list, that can be displayed again when that element is selected. does anyone have any ideas, im really struggling...

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    so1i's Avatar
    so1i is offline Programming Professional
    Join Date
    Sep 2009
    Location
    Aberystwyth, United Kingdom
    Posts
    309
    Rep Power
    0

    Re: Help with JList and TextArea...

    What code have you got so far?

  4. #3
    gabrielle_l is offline Newbie
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Re: Help with JList and TextArea...

    so far my code can add element to list with the save button and can open file to the text area, but i dont know how to associate the element to the contents of the text area. code is below:
    Last edited by gabrielle_l; 03-22-2010 at 05:18 AM. Reason: Use [CODE] tags for code.

  5. #4
    Stu_328 is offline Learning Programmer
    Join Date
    Dec 2008
    Posts
    92
    Rep Power
    12

    Re: Help with JList and TextArea...

    Here try something like this:

    When you open the file for the first time:
    Code:
    					//Add it to the list
    					listModel.addElement(newFile);
    					TestMain.this.validate();
    Then something like this when you click the list:
    Code:
    		list.addListSelectionListener(new ListSelectionListener(){
    			@Override
    			public void valueChanged(ListSelectionEvent e) {
    				File file = null;
    				if(list.getSelectedValue() instanceof File)
    					file = (File)list.getSelectedValue();
    				else
    					return;
    
    				BufferedReader bReader = null;
    				try {
    					bReader  = new BufferedReader( new InputStreamReader( new FileInputStream(file) ) ) ;
    					String read;  
    					StringBuffer text = new StringBuffer() ;  
    					while( (read = bReader.readLine() ) != null )   
    					{  
    						text.append( read ).append( "\n" ) ;  
    					}  
    					textArea.setText( text.toString() ) ; 
    				} catch (Exception e1) {
    					e1.printStackTrace();
    				} finally{
    					try{
    						bReader.close();
    					}catch(Exception ex){ex.printStackTrace();}
    				}
    				TestMain.this.validate();
    			}
    		});
    Where <TestMain> is the name of your class.

    You might need to make your own object if you want to display other stuff in the text area...

    HTH

  6. #5
    gabrielle_l is offline Newbie
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Re: Help with JList and TextArea...

    Thanks this works great, it displays the file path in the jlist as the item, and the listener works how i wanted it to. One further question: if i wanted to have a custom name for the jlist item (from an input dialog) will it still work, because i have been trying to do this.

    The naming of the item will come from:

    Code:
    String textName = JOptionPane.showInputDialog(null, "Graph Name : ");
    				if(textName != null){
    					
    					System.out.println(textName);
    					
    			         }
    			        else
    			        	JOptionPane.showMessageDialog(null, "You pressed cancel button.");
    when you open the file:
    Code:
    String newFile = textName;
    listModel.addElement(newFile);
    test.this.validate();
    This code renames the jlist items correctly but then the listener doesnt work and the textarea doesnt change, have you got any more suggestions. thanks.

  7. #6
    Stu_328 is offline Learning Programmer
    Join Date
    Dec 2008
    Posts
    92
    Rep Power
    12

    Re: Help with JList and TextArea...

    The list is a list of File objects not Strings. Your adding the String (ie the path of the file), to the list.

    In the listener code, if you look, there's this:

    Code:
    if(list.getSelectedValue() instanceof File)
    file = (File)list.getSelectedValue();
    else
    return;
    This is using the instanceof keyword. If you haven't encountered it before, look it up or ask here.
    It checks to see if the selected item is a File and only if its a file will the proceed. Otherwise it returns.
    So when it finds your String that you added it just returns because it fails the test to see if it's a file.

    So, what do you think you need to do to resolve?

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Can't get JList to show in Panel
    By N u l l i f y in forum Java Help
    Replies: 2
    Last Post: 12-08-2010, 09:39 AM
  2. PROBLEM WITH JList CONTROL
    By fas in forum Java Help
    Replies: 2
    Last Post: 09-29-2010, 07:19 AM
  3. jList problem
    By roro in forum Java Help
    Replies: 2
    Last Post: 04-13-2010, 06:47 AM
  4. TextArea in VB 6 ??
    By jashsayani in forum Visual Basic Programming
    Replies: 4
    Last Post: 04-08-2009, 11:33 AM
  5. ListModel or JList to Array
    By Victor in forum Java Help
    Replies: 1
    Last Post: 07-16-2008, 10:39 AM

Tags for this Thread

Bookmarks

Posting Permissions

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