I have a JPanel with a FlowLayout inside of a viewPort of a JScrollPane. I can add components dinamically to this JPanel but when I reach the end of the JScrollPane what i want is for the components to go down the next row instead of continuing horizontally infinitly. I know that to make FlowLayout go down the next row you can set sizes but when its nested in a JScrollPane it doesnt work.
Heres some code
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
public class prova extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
prova frame = new prova();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public prova() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1040, 475);
contentPane = new JPanel();
contentPane.setBackground(new Color(255, 255, 255));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 11, 1004, 206);
contentPane.add(scrollPane);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
scrollPane.setViewportView(panel);
JButton btnNewButton = new JButton("New button");
panel.add(btnNewButton);
JButton btnNewButton_1 = new JButton("New button");
panel.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("New button");
panel.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("New button");
panel.add(btnNewButton_3);
JButton btnNewButton_4 = new JButton("New button");
panel.add(btnNewButton_4);
JButton btnNewButton_5 = new JButton("New button");
panel.add(btnNewButton_5);
JButton btnNewButton_6 = new JButton("New button");
panel.add(btnNewButton_6);
JButton btnNewButton_7 = new JButton("New button");
panel.add(btnNewButton_7);
JButton btnNewButton_8 = new JButton("New button");
panel.add(btnNewButton_8);
JButton btnNewButton_9 = new JButton("New button");
panel.add(btnNewButton_9);
JButton btnNewButton_10 = new JButton("New button");
panel.add(btnNewButton_10);
}
}
The JButton are placeholders for the example, I want the last two of the buttons (9 and 10) to go down a row. Also can't use GridLayout because the components have different sizes and it would strech every cell to the biggest one.