The Artima Developer Community
Sponsored Link

Java Answers Forum
Auto-Resizing Panel

2 replies on 1 page. Most recent reply: Jun 11, 2007 10:59 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Auto-Resizing Panel Posted: Jun 11, 2007 12:19 AM
Reply to this message Reply
Advertisement
Hi

I'm a bit stuck on a layout problem.

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.*;
 
public class 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 ">                          
    private void 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
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void 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                   
}


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Auto-Resizing Panel Posted: Jun 11, 2007 6:24 AM
Reply to this message Reply
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.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Auto-Resizing Panel Posted: Jun 11, 2007 10:59 PM
Reply to this message Reply
Thanks, I'll look into it.

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.

Flat View: This topic has 2 replies on 1 page
Topic: Auto-Resizing Panel Previous Topic   Next Topic Topic: C# / Java crossplatform development, what tools and methods are common?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use