JPanel with many children doesn't display them all in a narrow window
I'm writing an application that has a JPanel at the top containing many
JToolBars. However, the height of the JPanel stays constant when I narrow
the application window so that many of the JToolBars are not displayed.
I would like to have the JPanel increase in height as the window narrows
so that all the JToolBars remain visible.How can you do this? Thanks.
Try widening/narrowing the window in this example:
-
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
publicclass Sampleextends JFrame{
public Sample(String name){
super(name);
JPanel tp =new JPanel(new FlowLayout());
for (int i = 0; i < 10; i++){
JToolBar tb =new JToolBar("toolbar" + i);
for (int j = 0; j < 10; j++){
tb.add(new AbstractAction(("Action" + i) + j){
publicvoid actionPerformed(ActionEvent event){
}
});
}
tp.add(tb);
}
JPanel cp =new JPanel();
cp.setSize(400, 400);
cp.setBackground(Color.red);
add(tp, BorderLayout.PAGE_START);
add(cp, BorderLayout.CENTER);
}
publicstaticvoid main(String[] args){
Sample s =new Sample("Sample");
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
s.setSize(500, 500);
s.setVisible(true);
}
}

