Reputation: 1609
I have a panel (with GridLayout(0,1)) embedded on JFrame.
As per my requirement :-
Everything is working fine, except below two listed problems :
For reference, have a look at this .
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
Reputation: 201
You should use another layout. GridLayout
draws the components to maximum size available.
Upvotes: 1
Reputation: 109823
JPanel
(BorderLayout
) nested
JPanel
(has by default FlowLayout
) for JButton
, this JPanel
put to the BorderLayout.NORTH
JTextArea
put to then BorderLayout.CENTER
revalidate & repaint
, only in the case that you switch between JComponents
or remove and then add new JComponent(s)
, hide/visible
particular JPanel
or JComponent
use method setVisible()
Upvotes: 3