After adding a number of buttons greater than JPanel
it supports, I would like to update (increase JPanel
) automatically, how can I do this?
private JPanel contentPane;
private static int tamanho = 429;
public static void main(String[] args)
{
teste frame = new teste();
frame.setVisible(true);
}
private static int qtButton = 0 ;
public teste()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 900, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scroll = new JScrollPane();
scroll.setBounds(10, 11, 464, 439);
contentPane.add(scroll);
JPanel panel = new JPanel();
scroll.setViewportView(panel);
panel.setLayout(null);
panel.setPreferredSize(new Dimension(390, 1000));
JButton adicionar = new JButton("Adicionar");
adicionar.setBounds(481, 11, 134, 23);
contentPane.add(adicionar);
adicionar.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
gerarButton(panel);
}
});
}
public static void gerarButton(JPanel panel)
{
JButton NewButton = new JButton("New button");
NewButton.setBounds(10, 11 + (34 * qtButton), 425, 23);
panel.add(NewButton);
qtButton ++;
tamanho = 11 + (34 * qtButton) + 50;
if(qtButton == 16)
{
}
panel.repaint();
}
As you can see in the image below, the last button is outside of the panel, and I would like it when this happens, I could resize the panel
so that it increases in size, so the last button fits. >