This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have an object that inherits from JPanel. I would like to add this JPanel to a JScrollPane in another class. Everything works fine when I do not add this panel to the scrollpane, but when I do, no information is present and it is, or is as if the panel does not exist. Here is the bit of code that I am using . . . .
I do not get any exception thrown. Any help or suggestion are greatly appreciated.
I am using the JScrollPane exactly how Ive seen it used on multiple tutorial sites. Rather than just saying it is wrong, maybe you could tell me HOW I am using it wrong. Then I would also have a frame of referance during the search to correct the errors. Thanks.
I got it to work by putting the panel directly into the contructor for the JScrollPane, but I still would like to know why it did not work the add method, afterall, what else would an add method be used for. . .
But I agree that perhaps JScrollPane should override the methods for adding and removing components, to delegate these calls to the JViewport. The Sun UI classes have an overall problem with guarding against unwanted adding / removing of components. When I create a JComponent subclass I usually make sure that it's not possible to add or remove components, or to set the layout, in any way that's not intended.
Part of the problem here is that JComponent extends Container, and that's wrong in my opinion. It means that you can technically set the layout or add child components to any JComponent, including those like JLabel, JTextField and JButton. I doubt that's intended. Sun should have made JComponent extend Component, and copied the code of Container to a new class (JContainer comes to mind). Classes like JLabel should then still extend JComponent, whereas JPanel and other classes that actually should be containers for other components should extend JContainer instead.
Thank you very much for the information and explaining this to me. One problem with trying to learn how to program alone, and without the help of teachers, is that you never learn stuff like this. Thanks a lot!