I'm trying to adding the scrollpane in a inner panel,and the scrollbar is showing in the inner panel, but as i add the labels and textfield, the components were added but the scroll bar is not working.
public class AddNewProject extends JFrame {
private JButton btnNewButton;
private JSpinner spinner;
private JScrollPane scrollPane;
private JPanel panel_1;
public AddNewProject() {
getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBackground(Color.PINK);
panel.setBounds(134, 37, 583, 610);
getContentPane().add(panel);
panel.setLayout(null);
spinner = new JSpinner();
spinner.setModel(new SpinnerNumberModel(0, 0, 30, 1));
spinner.setBounds(63, 51, 164, 31);
panel.add(spinner);
btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int n=(int) spinner.getValue();
JLabel jlabel[]=new JLabel[n];
JTextField jtxt[]=new JTextField[n];
for(int i =0;i<n;i++)
{
jlabel[i]=new JLabel("Label "+(i+1));
jtxt[i]=new JTextField(32);
panel_1.add(jlabel[i]);
panel_1.add(jtxt[i]);
}
panel_1.validate();
panel_1.repaint();
}
});
btnNewButton.setBounds(336, 54, 149, 28);
panel.add(btnNewButton);
scrollPane = new JScrollPane();
scrollPane.setBounds(69, 141, 434, 298);
panel.add(scrollPane);
panel_1 = new JPanel();
scrollPane.setViewportView(panel_1);
scrollPane.setPreferredSize(new Dimension(434,300));
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
setSize(900,800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new AddNewProject();
}
}
Output of the above Program
This is the output image of my program.
