I want to fill out a JPanelformat which , if this is not possible with the help , I would like to know if use is possible and how? GridLayoutGridLayoutGridBagLayout
Here is the code:
public class NestedLayoutExample {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
final JFrame frame = new JFrame("Nested Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(300, 400));
final JPanel gui = new JPanel(new BorderLayout(5,5));
gui.setBorder( new TitledBorder("BorderLayout(5,5)") );
JPanel dynamicLabels = new JPanel(new BorderLayout(4,4));
dynamicLabels.setBorder( new TitledBorder("BorderLayout(4,4)") );
gui.add(dynamicLabels, BorderLayout.CENTER);
final JPanel labels = new JPanel(new GridLayout(4,0,3,3));
labels.setBorder( new TitledBorder("GridLayout(0,2,3,3)") );
JButton addNew = new JButton("Add Another Label");
dynamicLabels.add( addNew, BorderLayout.NORTH );
addNew.addActionListener( new ActionListener(){
private int labelCount = 0;
@Override
public void actionPerformed(ActionEvent ae) {
labels.add( new JLabel("Label " + ++labelCount) );
frame.validate();
}
} );
dynamicLabels.add( new JScrollPane(labels), BorderLayout.CENTER );
frame.setContentPane(gui);
frame.pack();
frame.setLocationRelativeTo(null);
try {
frame.setLocationByPlatform(true);
frame.setMinimumSize(frame.getSize());
} catch(Throwable ignoreAndContinue) {}
frame.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}}
Here is what I got above:

And this is what I want:

I would also like to know how best to do this.
source
share