Re: Swing Custom JPanel Component Help
On Jul 15, 8:22 am, pek <kimwl...@gmail.com> wrote:
On Jul 15, 7:29 am, Daniel Pitts <googlegrou...@coloraura.com> wrote:
On Jul 14, 7:23 pm, pek <kimwl...@gmail.com> wrote:> Currently I have a CardGroupPanel that extends JScrollPane (because I
want it to be scrollable).
CardGroupPanel should extends JPanel, and be put into a JScrollPane,
rather than extending JScrollPane
I extend JScrollPane because the scrollability of the object is not
optional. It always exist. So, rather than setting for every
CardGroupPanel a new JScrollPane, I find it much easier to extend
JScrollPane. Although, I did have some problems with the background.
When extending a JPanel, you can use setOpaque(false) and the JPanel
is transparent. When extending the JScrollPane, even if you set both
(or any combination) JScrollPane's setOpaque and the JPanel's
container setOpaque it still isn't transparent. I'd hate adding to the
constructor a Color which would be used to set it's background color.
I find it bad practice. But, this would be my solution if nothing else
doesn't work. Also, I tried overriding JScrollPane's
setBackground(Color bg) and after calling super.setBackground(bg) I
try to call container.setBackground(bg) but container hasn't been yet
initialized and a NullPointerException is been thrown. I haven't yet
found I way so please advise. ;)
If you don't set the CardGroupPAnel as the viewport view for your
scroll pane, you aren't going to get any scroll bars.
The proper way to do what you are trying to do:
CardGroupPanel panel = new CardGroupPanel();
JScrollPane scrollPane = new JScrollPane(panel);
panel.setBackground(Color.green);
scrollPane.setBackground(Color.green);
Make sure CardGroupPanel extends JPanel and implements Scrollable.
That should give you the control you need over width/height/scroll
area.
Read the sun tutorial for more information <http://java.sun.com/docs/
books/tutorial/uiswing/components/scrollpane.html>