0
I would like to add a scroll bar to a Jpanel that uses Absolute Layout.
package Default;
import javax.swing.JFrame;
import javax.swing.border.EmptyBorder;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JScrollPane;
public class teste extends JFrame {
private JPanel contentPane;
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, 500, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane panel = new JScrollPane();
panel.setBounds(10, 11, 464, 439);
contentPane.add(panel);
for(int i = 0; i < 15; i++)
{
gerarButton(panel);
}
}
public static void gerarButton(JScrollPane panel)
{
JButton NewButton = new JButton("New button");
NewButton.setBounds(10, 11 + (34 * qtButton), 444, 23);
panel.add(NewButton);
qtButton ++;
panel.repaint();
}
}
And here’s the way it is
How could I do that ??
Note: the buttons inside are "infinite"
Use Jscrollpane instead of Jpanel.
– user28595
@Diegof I used and it still didn’t work
– Lucas Caresia
Seven a size for the
JScrollPane
, usingsetPreferredSize
.– user28595
@Diegof I would like an example in the code I put in the question, could do it ??
– Lucas Caresia