Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4236490
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:38:46+00:00 2026-05-21T02:38:46+00:00

I try to change a layout of the panel, but there are problems, because

  • 0

I try to change a layout of the panel, but there are problems, because when I change layout the components remain on the panel and there are many bags.

import java.awt.LayoutManager;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class LayoutTest extends JFrame {
public LayoutTest() {
    initComponents();
}

private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jButton = new JButton();

    jPanel1.setBackground(new java.awt.Color(204, 204, 255));
    jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

    jButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            LayoutManager tempGroupLayout = jPanel1.getLayout();
            jPanel1.setLayout(layoutManager2);
            jPanel1.getParent().validate();
            layoutManager = layoutManager2;
            layoutManager2 = tempGroupLayout;
        }
    });

    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().
            addContainerGap(286, Short.MAX_VALUE).
            addComponent(jButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)));
    jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
            addComponent(jButton, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE));
    layoutManager = jPanel1Layout;

    javax.swing.GroupLayout jPanel1Layout2 = new javax.swing.GroupLayout(jPanel1);
    jPanel1Layout2.setHorizontalGroup(
            jPanel1Layout2.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
            addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout2.createSequentialGroup().
            addComponent(jButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)));
    jPanel1Layout2.setVerticalGroup(
            jPanel1Layout2.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).
            addComponent(jButton, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE));
    layoutManager2 = jPanel1Layout2;
    getContentPane().add(jPanel1);
    pack();
}

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new LayoutTest().setVisible(true);
        }
    });
}
private JButton jButton;
private JPanel jPanel1;
private LayoutManager layoutManager;
private LayoutManager layoutManager2;

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T02:38:47+00:00Added an answer on May 21, 2026 at 2:38 am

    Have a look at the JSplitPane component. You can nest a few of them.

    splitPane.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, new SplitPanePropertyChangeListener());
    
    private class SplitPanePropertyChangeListener implements PropertyChangeListener {    
        public void propertyChange(java.beans.PropertyChangeEvent evt) {    
            int nv = ((Integer) evt.getNewValue()).intValue();    
             if (nv == 1) { //adjust this depending on your divider location    
             setSize(new Dimension(800, 560));    
          }    
          if (nv == 221) { //adjust this depending on your divider location    
             setSize(new Dimension(1000, 560));    
          }     
       }    
    }
    

    Check with above code and remodel it for your requirement

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hello i try to use simple application in android but i have many problems
example i try to change cell.accessoryView from picture1 to picture2 by fadein but my
When I try to change the location of a layout with setParent the result
here's an SSCCE: import java.awt.Color; import java.awt.Dimension; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import
I have a textview in my XML layout file, but when I try to
I try to change the dimensions of the view in interface builder from the
When I try to change the default collation of a SQL Server instance using
I'm try to change text color and align item in spinner to center of
When I try to change image for WPF trigger 'IsMouseOver' for button, the control
In Delphi 2007, in a mouse move event, I try to change the mouse

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.