Adding Scrolling to JPanel

Hello All,

I have a form with a JPanel and a JScrollPane. The panel has a row of components like JTextField, Combo box, check box etc. A button will generate another row of the same components mentioned above. I would like to add scrolling to the JPanel as the number of rows increase so that I can scroll up and down. I am using JDeveloper and my problem is that the scrolling functionality is not added when the number of rows increase. Can any one help how to solve this problem. Any code snippets/explanation will be highly appreciated. I have to urgently implement it.

Thanks in advance.

Kind regards and best wishes.

Hasnain.

Here is my code sample.

// Declarations

private JButton jButton1 = new JButton();

private XYLayout xYLayout1 = new XYLayout();

private ScrollPane scrollPane1 = new ScrollPane();

private Panel panel1 = new Panel();

private JTextField jTextField2 = new JTextField();

// Initialization and setup

this.getContentPane().setLayout( null );

this.setSize(new Dimension(842, 586));

jButton1.setText("jButton1");

jButton1.setBounds(new Rectangle(10, 260, 75, 25));

jButton1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

jButton1_actionPerformed(e);

}

});

scrollPane1.setBounds(new Rectangle(90, 65, 335, 170));

panel1.setLayout(null);

jTextField2.setBounds(new Rectangle(5, 5, 85, 20));

jTextField2.setText("First TextField");

panel1.add(jTextField2, null);

scrollPane1.add(panel1, null);

this.getContentPane().add(scrollPane1, null);

this.getContentPane().add(jButton1, null);

// action listener method for the button

int x = jTextField2.getBounds().x;

int y = jTextField2.getBounds().y;

System.out.println("X : " + x + ", Y : " + y);

JTextField f = new JTextField();

f.setText("Second TextField");

//f.setAlignmentX(x+20);

f.setBounds(x,y+20,jTextField2.getWidth(),jTextField2.getHeight());

System.out.println("X2: " + f.getBounds().x + " , Y2 : " + f.getBounds().y);

panel1.add(f,null);

panel1.repaint();

jTextField2 = f;

[2239 byte] By [HJaveda] at [2007-11-26 23:35:30]
# 1

When you put a JPanel in JScrollPane, the JPanel's preferred size will decide if scrolling is needed or not. Normally, the LayoutManager will take of that for you. However, since you are using a null-layout, it is up to you to update the panel's preferred size whenever you add something to it.

Torgila at 2007-7-11 14:57:10 > top of Java-index,Desktop,Core GUI APIs...