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

  • SEARCH
  • Home
  • 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 7160177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:21:23+00:00 2026-05-28T13:21:23+00:00

I have a panel (with GridLayout( 0,1 )) embedded on JFrame. As per my

  • 0

I have a panel (with GridLayout(0,1)) embedded on JFrame.

As per my requirement :-

  • I am adding (JButton,JTextArea) in pairs on above declared panel.
  • Now, on click of JButton of any pair, it’s JTextArea should be removed, and on reclicking JButton, it’s JTextArea should be added again.

Everything is working fine, except below two listed problems :

  1. Dimensions of JButton are changed on revalidation.
  2. Initial dimensions of JButton are very large(How to restrict dimension of JButton)

For reference, have a look at this screenshot.

And Please find below modified Code listings :-

TestFrame.java

package com.test;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class TestFrame extends JFrame implements ActionListener{

    final JPanel panel ;

    private TestFrame() {
        setExtendedState(JFrame.MAXIMIZED_BOTH) ;
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) ;
        panel = new JPanel() ;
        panel.setLayout( new GridLayout(0, 1) ) ;

        for(int i = 1 ; i <= 3 ; ++i){
            TButton btn = new TButton("User " + i + " [Toggle Button for TextArea " + i + "] (Click)")  ;
            panel.add(btn) ;        btn.addActionListener(this) ;   panel.add(new JScrollPane(btn.getLoggingArea())) ;
        }

        add(panel, BorderLayout.CENTER) ;
        setVisible(true) ;
        setExtendedState(JFrame.MAXIMIZED_BOTH) ;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        TButton btn = (TButton) e.getSource() ;
        JPanel parent = (JPanel) btn.getParent() ;

        int index = getIndex(parent, btn) ;

        if(btn.isLoggingAreaVisible()){
            parent.remove( parent.getComponent(index + 1) ) ;
            btn.setLoggingAreaVisible(false) ;
        }else{
            parent.add(new JScrollPane(btn.getLoggingArea()), index + 1) ;
            btn.setLoggingAreaVisible(true) ;
        }

        parent.revalidate() ;
        parent.repaint() ;
    }

    private int getIndex(JComponent parent, Component btn) {

        Component []comps = parent.getComponents() ;

        for(int i = 0 ; i < comps.length ; ++i){
            if( comps[i].equals(btn) ){
                return i ;
            }
        }

        return -1 ;
    }

    public static void main(String[] args) {
        new TestFrame() ;
    }
}

TButton.java

package com.test;

import javax.swing.JButton;
import javax.swing.JTextArea;

public class TButton extends JButton{

    private JTextArea loggingArea ;
    private boolean loggingAreaVisible = true ;

    public TButton(String threadName) {
        super(threadName) ;
        initComponents(threadName) ;
    }

    public void initComponents(String threadName) {

        String str = "1. By default large buttons are displayed." + "\n" +
                     "    But, I want buttons with a little small dimensions." + "\n\n" +
                     "2. On click of above button, above button expands to cover space used by this textArea." + "\n" +
                     "    But, what I want, that button size does not changes onClick, only textArea length increases/decreases." ;
        loggingArea = new JTextArea(getText() + " textArea." + "\n\n" + str + "\n\n" + "Type Here...") ;
    }


    public boolean isLoggingAreaVisible() {
        return loggingAreaVisible;
    }

    public void setLoggingAreaVisible(boolean loggingAreaVisible) {
        this.loggingAreaVisible = loggingAreaVisible;
    }

    public JTextArea getLoggingArea() {
        return loggingArea;
    }
}

Thanks,
Rits 🙂

  • 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-28T13:21:24+00:00Added an answer on May 28, 2026 at 1:21 pm

    Use e.g. vertical BoxLayout instead of GridLayout

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

Sidebar

Related Questions

I have a Panel and I am adding controls inside this panel. But there
I have a panel with GridLayout(1, 3) but i want centralize the content of
I have A Jpanel with A GridLayout inside. Now I added another Jpanel inside
I have created a JFrame subclass containing 9 JPanels using a GridLayout (3x3). I'm
I have panel that is using group layout to organize some label. I want
I have a panel on an aspx page which contains an UpdatePanel. This panel
I have an Panel control that I need to maintain position across postbacks. I
I have a panel that draws alot of things. To make drawing effecient I
i have a panel (A), which contains 3 other panels (AA, AB, AC). Each
I have update panel that content check box, textbox, 3 DropDownList with CascadingDropDown extender.

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.