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...
What code have you got so far?
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.
Here try something like this:
When you open the file for the first time:
Then something like this when you click the list:Code://Add it to the list listModel.addElement(newFile); TestMain.this.validate();
Where <TestMain> is the name of your class.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(); } });
You might need to make your own object if you want to display other stuff in the text area...
HTH
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:
when you open the file:Code:String textName = JOptionPane.showInputDialog(null, "Graph Name : "); if(textName != null){ System.out.println(textName); } else JOptionPane.showMessageDialog(null, "You pressed cancel button.");
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.Code:String newFile = textName; listModel.addElement(newFile); test.this.validate();
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();elsereturn;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?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks