The target is to have a big center panel and a bar at the top with a lot of buttons and other controls. The top bar should use a flow layout and put buttons in the second row if the window ist not wide enough to contain all the elements.
I first tried to use a standard panel with flow layout, then I tried using a JToolBar.
I also tried to use Gridbaglayout instead, but there's no difference.
The problem remained the same however: The second row of buttons is not displayed because the panel does not increase it's minimum / prefered size.
What can I do to accomplish this?
This is a short example made with netbeans, it just has 2 panels and some buttons in the top panel. If you resize the frame, you can notice that some controls are pushed in the second row but the panel size does not increase
package testproject.forms;
import javax.swing.*;
import java.awt.*;
publicclass TestFrame extends javax.swing.JFrame {
public TestFrame() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
privatevoid initComponents() {
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jToggleButton2 = new javax.swing.JToggleButton();
jPasswordField1 = new javax.swing.JPasswordField();
jPasswordField2 = new javax.swing.JPasswordField();
jPan_Top = new javax.swing.JPanel();
jToggleButton1 = new javax.swing.JToggleButton();
jButton1 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jRadioButton1 = new javax.swing.JRadioButton();
jToggleButton3 = new javax.swing.JToggleButton();
jPasswordField3 = new javax.swing.JPasswordField();
jButton8 = new javax.swing.JButton();
jToggleButton4 = new javax.swing.JToggleButton();
jCheckBox1 = new javax.swing.JCheckBox();
jPan_Center = new javax.swing.JPanel();
jButton2.setText("jButton2");
jButton3.setText("jButton3");
jButton4.setText("jButton4");
jButton5.setText("jButton5");
jToggleButton2.setText("jToggleButton2");
jPasswordField1.setText("jPasswordField1");
jPasswordField2.setText("jPasswordField2");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPan_Top.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 2, 2));
jToggleButton1.setText("jToggleButton1");
jPan_Top.add(jToggleButton1);
jButton1.setText("jButton1");
jPan_Top.add(jButton1);
jButton6.setText("jButton6");
jPan_Top.add(jButton6);
jButton7.setText("jButton7");
jPan_Top.add(jButton7);
jRadioButton1.setText("jRadioButton1");
jRadioButton1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
jRadioButton1.setMargin(new java.awt.Insets(0, 0, 0, 0));
jPan_Top.add(jRadioButton1);
jToggleButton3.setText("jToggleButton3");
jPan_Top.add(jToggleButton3);
jPasswordField3.setText("jPasswordField3");
jPan_Top.add(jPasswordField3);
jButton8.setText("jButton8");
jPan_Top.add(jButton8);
jToggleButton4.setText("jToggleButton4");
jPan_Top.add(jToggleButton4);
jCheckBox1.setText("jCheckBox1");
jCheckBox1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
jCheckBox1.setMargin(new java.awt.Insets(0, 0, 0, 0));
jPan_Top.add(jCheckBox1);
getContentPane().add(jPan_Top, java.awt.BorderLayout.NORTH);
javax.swing.GroupLayout jPan_CenterLayout = new javax.swing.GroupLayout(jPan_Center);
jPan_Center.setLayout(jPan_CenterLayout);
jPan_CenterLayout.setHorizontalGroup(
jPan_CenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 793, Short.MAX_VALUE)
);
jPan_CenterLayout.setVerticalGroup(
jPan_CenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 482, Short.MAX_VALUE)
);
getContentPane().add(jPan_Center, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
publicstaticvoid main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
publicvoid run() {
new TestFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JCheckBox jCheckBox1;
private javax.swing.JPanel jPan_Center;
private javax.swing.JPanel jPan_Top;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JPasswordField jPasswordField2;
private javax.swing.JPasswordField jPasswordField3;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JToggleButton jToggleButton1;
private javax.swing.JToggleButton jToggleButton2;
private javax.swing.JToggleButton jToggleButton3;
private javax.swing.JToggleButton jToggleButton4;
// End of variables declaration
}
Sorry, I don't know the solution myself but a quick search seems to show that it's (surprisingly) non-trivial. Check out a similar problem at http://forum.java.sun.com/thread.jspa?forumID=57&threadID=5117549&start=7 . There's a reference there to a "WrapLayout" that somone developed to solve the same problem.
I developed a "working" solution myself, calculating the panel's new height by it's last component's values (component.yLocation + component.height + layout.verticalGap) on various events, but it didn't work well if teh frame was maximized.