I have a JPanel that I want to dynamically fill with other jpanels, Like this: result If I use a gridLayout it works unless it's not filled, in which case it will stretch the panels inside, like this gridlayout, stretched If I use GridBagLayout it doesn't stretch them when there's few of them, but then it will not scroll when the panel is filled, like this gridbag, noscroll I just can't get it to do both things: only vertical scroll AND no stretching when the panel is not filled. The blue squares are JPanels with a borderLayout that I create passing labels and color as parameters. I tried making them absoluteLayout, gridBagLayout, fix their size, but the JPanel I add them to seems to overrule their settings.
public class Gbmapper extends JFrame {
/**
* Create the frame.
*/
public Gbmapper() {
//main window
setTitle("Gbmapper");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panelParagrafi = new JPanel(new BorderLayout(4, 4));
panelParagrafi.setBounds(10, 67, 760, 180);
panelParagrafi.setBorder(new TitledBorder("Paragrafi"));
// here's the problem
JPanel listaNodi = new JPanel(new GridLayout(0,20,2,2));
//JPanel listaNodi = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2,2,2,2);
/* flowlayout also will display a single row
JPanel listaNodi = new JPanel();
listaNodi.setBounds(10, 67, 764, 180);
listaNodi.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); */
// init the jpanel with some items
for (int i = 0; i < 10; i++) {
// for use with gridlayout
listaNodi.add( new GbmParNode(Integer.toString(50), Integer.toString(i), 'b') );
// for use with gridbaglayout, passes insets
//listaNodi.add( new GbmParNode(Integer.toString(50), Integer.toString(i), 'b'), gbc );
}
JScrollPane scrollPane = new JScrollPane(listaNodi,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panelParagrafi.add(scrollPane);
contentPane.add(panelParagrafi);
}}
// My JPanel class
package gbmapper;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.SwingConstants;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.BorderLayout;
public class GbmParNode extends JPanel {
// COLORI
Color myGreen = new Color(173, 255, 47);
Color myRed = new Color(255, 80, 50);
Color myBlue = new Color(0, 204, 255);
Color myYellow = new Color(255, 255, 0);
Color myWhite = new Color(255, 255, 255);
JLabel lblParNum; // = new JLabel(parNum);
/**
* Create the panel.
*/
public GbmParNode(String parNum, String parDesc, char parColor) {
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
Color myColor;
switch(parColor) {
case 'g':
myColor = myGreen;
break;
case 'r':
myColor = myRed;
break;
case 'b':
myColor = myBlue;
break;
case 'y':
myColor = myYellow;
break;
default:
myColor = myWhite;
}
setBackground(myColor);
setLayout(new BorderLayout(0, 0));
//setLayout(new GridBagLayout());
//setSize(50,50);
lblParNum = new JLabel(parNum);
lblParNum.setHorizontalAlignment(SwingConstants.CENTER);
lblParNum.setFont(new Font("Tahoma", Font.PLAIN, 18));
//lblParNum.setBounds(14, 5, 100, 22);
add(lblParNum, BorderLayout.CENTER);
JLabel lblParDesc = new JLabel(parDesc);
lblParDesc.setHorizontalAlignment(SwingConstants.CENTER);
//lblParDesc.setBounds(14, 25, 100, 14);
add(lblParDesc, BorderLayout.SOUTH);
}
}