File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JList in JScrollPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JList in JScrollPane" Watch "JList in JScrollPane" New topic
Author

JList in JScrollPane

A. Wolf
Ranch Hand

Joined: Sep 28, 2003
Posts: 57
Hi,
I have a JList inside a JScrollPane.getviewport.setview.... whatever. I have a separate thread that parses an xml file, and adds components to the JLists defaultlistmodel as it goes along with setElement(String). The application seems to have a mind of its own sometimes.
I start the app, it starts to list some of the elements in the jList and then the list just goes blank. it does it a second time when I start the app again. then sometimes on the third time it loads all the elements and I can see them. now when I scroll down past the last element that was viewable from the top I cant select it. I select it, but I dont see the hi-light. once i scroll the selected but not hilighted element out of the viewable area and back in, its suddenly selected.
So, in all I have 2 problems. one, the elements not displaying when I start the app, and two, the elements not being hi-lighted. Does anyone have any idea what could cause this?
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Post your code... it could be a problem with the threading, the XML, Swing events, or many other things...


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
A. Wolf
Ranch Hand

Joined: Sep 28, 2003
Posts: 57
here it goes:

NEXT CLASS------------------------------------------------

NEXT CLASS-----------------------------------------------------------------

and then i used javalayer to play the files. I have also noticed lots of little mistakes in the filenames and locations of the files when i retrieve them.

[Nathan - edited to remove long lines in code.]
[ November 14, 2003: Message edited by: Nathan Pruett ]
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

I think the problem is that you are mixing AWT and Swing components... Frame is an AWT frame, and you are adding a Swing JScrollPane to it. To fix this, use only Swing components, like JFrame, JButton, etc.

Some other things you should do : use a layout manager instead of null... this can lead to sizing problems later. There are also better ways to instantiate all the widgets than the way you are doing it. Take a look at the refactored code for your window below.


[ November 14, 2003: Message edited by: Nathan Pruett ]
A. Wolf
Ranch Hand

Joined: Sep 28, 2003
Posts: 57
hmm... it still does that flicker in the list. once, twice and then either it stays off or it comes back on to stay. I have to say, your code looks very neat compared to mine, and makes more sense logically. As to layout managers, if there was an emoticon for pain i'd put it here -> ain: . for some reason my apps never look "cool" when i use them.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

That really doesn't make sense... all your code does is add things to the model. They are never removed, so there should never be a case where the list is not populated.

It sounds like either you still have a problem somewhere mixing AWT and Swing components, or that there is a problem with the other thread updating the model. If it is a problem with threading, try minimizing and restoring your frame when it shows up with nothing in the list. If it looks normal after you've restored the frame, then you've got a threading problem, and to fix it you can do one of two things. You're going to have to either wait until the list is completely populated before you show the frame, or you're going to have to use something like SwingUtilities.invokeLater() or invokeAndWait() to make sure you update the model in the event thread.
A. Wolf
Ranch Hand

Joined: Sep 28, 2003
Posts: 57
ok, I've consistently noticed this message popping up every time it does NOT work, and after i try to resize the window like you said.
2003-11-20 14:11:14.119 java[708] Java couldn't paint in Java_apple_awt_CRenderer_doRect, no focused view.

it's done that with other applications too, but I haven't seen any problems with them. this is the first time something is giving me trouble and this is displayed. it consistenly only appears every time my app fails. any suggestions?
[edit] Q/A
found this on apples website...
Q: Terminal/Console reports "Java couldn't paint in Java_apple_awt_CRenderer_doRect, no focused view."

A: This is a known issue with Swing applications under 1.4.1 Update 1, as well as Java 1.4.1 on Panther. It specifically refers to a failed paint call, but typically has no noticeable effect on applications and can be safely ignored.
but it doesn't draw my list items...
[ November 20, 2003: Message edited by: A. Wolf ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JList in JScrollPane