mogli
mogli

Reputation: 1609

How to restrict JButton to change it's dimensions as a result of revalidation

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

As per my requirement :-

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 :)

Upvotes: 3

Views: 378

Answers (3)

xyz
xyz

Reputation: 201

You should use another layout. GridLayout draws the components to maximum size available.

Upvotes: 1

mKorbel
mKorbel

Reputation: 109823

  1. Return JPanel (BorderLayout) nested
    • JPanel (has by default FlowLayout) for JButton, this JPanel put to the BorderLayout.NORTH
    • JTextArea put to then BorderLayout.CENTER
  2. There I can't see reason for revalidate & repaint, only in the case that you switch between JComponents or remove and then add new JComponent(s),
  3. For hide/visible particular JPanel or JComponent use method setVisible()

Upvotes: 3

StanislavL
StanislavL

Reputation: 57421

Use e.g. vertical BoxLayout instead of GridLayout

Upvotes: 2

Related Questions