views:

31

answers:

1

I want to create a function so that i can call add JLabel's, etc inside the JScrollPanel. I am not sure what the command is in NetBeans.

I tried doing JScrollPanel -> events -> container -> componentAdded to create the code below. But nothing shows up when i add code to that function.

     private void initComponents() {

        scrollPanel = new javax.swing.JScrollPane();

        scrollPanel.addContainerListener(new java.awt.event.ContainerAdapter() {
        public void componentAdded(java.awt.event.ContainerEvent evt) {
            scrollPanelComponentAdded(evt);
        }
     }


    private void scrollPanelComponentAdded(java.awt.event.ContainerEvent evt) {
       System.out.println("main");
    }   

Any help would be great, thanks.

+3  A: 

I don't use Netbeans and I'm not quite sure I understand exactly what you're trying to do, but the normal case for adding components to a scroll pane is to add a panel as the scroll pane's "viewport". The scroll pane is then like a window into that panel. If the panel is too big to fit into the scroll pane, the scrollbars will appear.

Here is a snippet to show what I mean. This might be what you're looking for in your initComponents method:

JPanel panel = new JPanel();
panel.add( ... ); // Add whatever components to the panel
scrollPanel = new JScrollPane();
scrollPanel.setViewportView(panel);

A ContainerListener will only be called when a component is actually added or removed from a container. In your above code, no other components are ever added to the scroll pane.

Ash
I wanted to put that into a function instead of into my initComponents code.
nubme
Okay, trying to clarify: You want to add some components to the scroll pane in a function when "something" happens? What is the "something"? From the your code above it looks like you want to respond to changes to components in the scroll pane.
Ash