MedicoreProgrammer
MedicoreProgrammer

Reputation: 31

How to resize a JFrame so that the components also expand or reduce in size, depending on user?

I will explain my point using the pictures below:

this example the project is executed:

https://i.sstatic.net/s1nLH.png

As you see the user re size's the frame, which then does not expand the components itself but instead fills it with a grey color:

https://i.sstatic.net/kdNA3.png

I would like to know how to re size the JFrame making the components within the frame expand or reduce in size depending on the user.

Here is the main class of this program, I am using GridBag layout and before someone points out I do understand the setSize function can be overridden by the managers, Only realized after coding this program.

Please note this is in fact for a project and this is just another previous program I have used, I would just like to know how to go about it.

public class Email{

    public static void main(String[] args) {
        // The GUI thread is created, with the JFrame in the run method
        SwingUtilities.invokeLater(new Runnable(){

             public void run(){

                JFrame frame = new Compose("Send an Email");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(700 , 600);
                // This stops the program from being resized
                frame.setResizable(true);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

}

Upvotes: 0

Views: 2230

Answers (2)

Priyamal
Priyamal

Reputation: 2989

import javax.swing.JFrame;
import javax.swing.JLabel;


public class emailjava extends JFrame{

    public emailjava(){
    adjustFrame();
    }




    /**
     * variable declaration
     **/
    private final javax.swing.JLabel bccLbl= new javax.swing.JLabel();
    private final javax.swing.JTextField bccText= new javax.swing.JTextField();
    private final javax.swing.JTextField ccTExt= new javax.swing.JTextField();
    private final javax.swing.JLabel jLabel1= new javax.swing.JLabel();
    private final javax.swing.JLabel jLabel2= new javax.swing.JLabel();
    private final javax.swing.JPanel jPanel1 = new javax.swing.JPanel();
    private final javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
    private final javax.swing.JLabel messageLbl= new javax.swing.JLabel();
    private final javax.swing.JTextArea messageText = new javax.swing.JTextArea();
    private final javax.swing.JLabel subjectLbl= new javax.swing.JLabel();
    private final javax.swing.JTextField subjectText= new javax.swing.JTextField();
    private final javax.swing.JTextField toText= new javax.swing.JTextField();
    //emd of the declaration

   public final void adjustFrame(){

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);


        JLabel []array = {jLabel1,jLabel2,bccLbl,subjectLbl,messageLbl};
        for (JLabel jLabel : array) {
           jLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        }

        jLabel1.setText("To :");
        jLabel2.setText("CC :");
        bccLbl.setText("BCC :");
        subjectLbl.setText("SubjectLine");
        messageLbl.setText("MessageBody");

        messageText.setColumns(20);
        messageText.setRows(5);
        jScrollPane1.setViewportView(messageText);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(messageLbl, javax.swing.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE)
                    .addComponent(subjectLbl, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bccLbl, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(5, 5, 5)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(toText)
                    .addComponent(ccTExt)
                    .addComponent(bccText)
                    .addComponent(subjectText)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(toText, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ccTExt, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bccText, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bccLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(subjectText, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(subjectLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(messageLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))))
        );

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

        pack();

    }

    public static void main(String[] args) {
        new emailjava().setVisible(true);
    }

}

this is the code that i came up with tried it my self and it is working 100%

Upvotes: 0

camickr
camickr

Reputation: 324207

Looks like you are using the GridBagLayout. So you need to modify your constraints to get your desired effect.

Check out the section from the Swing tutorial on How to Use GridBagLayout.

In particular you need to look at the weightx/weighty contraints. This tells the layout how to allocate space as the frame size changes. Because your values are 0, none of the components resize and the components are displayed in the center. I would guess you want the text field to resize.

So read the tutorial, download the demo code and play with it. Then modify your layout to do what you want.

Upvotes: 2

Related Questions