Java : JLayeredPane resize problem

3.3k views Asked by At

I am writing a program GUI in java. I want to hava a JLayeredPane as ContentPane of my JFrame. On my JLayeredPane there are two layers : one layer a Canvas(canvas1) and another layer has a JPanel(jPanel5). I use Netbeans IDE and this code is generated:

    canvas1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    canvas1.setBounds(0, 0, -1, -1);
    jLayeredPane1.add(canvas1, javax.swing.JLayeredPane.PALETTE_LAYER);

    jPanel5.setBounds(0, 0, 670, 550);
    jLayeredPane1.add(jPanel5, javax.swing.JLayeredPane.DEFAULT_LAYER);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 670, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE)
    );

pack();

when I run my program I saw a strange event! When I change size of tje JFrame; the size of main JLayeredPane did not change. I used diffrerent backGround colours for components and it is obvious to me that the size of JLayered is permanent however I set it vertical and horizonal resizable in it's properties. The JFrame is resizable but main jLayeredPane's size is permamnet.

How can I cause the jLayeredPane to resize as my JFrame ?!

thank you

enter image description here

2

There are 2 answers

3
Raceimaztion On

Try using the default BorderLayout instead of the GroupLayout you're using now. Simply comment out the group of lines that start with creating the GroupLayout and add this line:

getContentPane().add(jLayeredPane, BorderLayout.CENTER);

The BorderLayout.CENTER parameter to the .add() method tells the BorderLayout manager that you want the given widget to fill the center of your window.

0
Yuval Miron On

You can change the JLayeredPane along with the JFrame. While in (NetBeans IDE) Design mode - right click on the JLayeredPane, then on the popup menu hover over "Auto Resizing", then check bothe verticaland horizontal. That way the size of the JLayeredPane will change relatively to the JFrame.