hidding components problem

Hi

i have a text area and button in my applet.

the text area initially not visible

when pressing the button the text area will be visible

when the button is pressed again the text area will not be visible and so on.

this is a part of my code:

import java.awt.*;

import java.applet.*;

import javax.swing.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

publicclass showHideextends JApplet{

TextArea area;

Labellb;

JPanel textarea ;

JPanel label_panel;

JPanel button_textArea_panel;

JPanel buttonsPanel;

JButton Start;

boolean pressed =false;

publicvoid init(){

label_panel =new JPanel();

label_panel.setLayout(new BorderLayout());

label_panel.add(new Label("show/ hide textArea ",Label.CENTER));

textarea =new JPanel();

textarea.setLayout(new FlowLayout(FlowLayout.RIGHT));

textarea.setBackground(new Color(228, 241, 250));

area =new TextArea(5,20);

area.setVisible(false);

textarea.add("Center", area);

Start =new JButton(" show text area");

Start.addActionListener(new StartListener());

buttonsPanel =new JPanel();// i used Panel because i have many buttons

buttonsPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

buttonsPanel.add(Start);

button_textArea_panel =new JPanel();

button_textArea_panel.setLayout(new FlowLayout(FlowLayout.CENTER));

button_textArea_panel.add(buttonsPanel);

button_textArea_panel.add(textarea);

this.setLayout(new BorderLayout());

add("North",label_panel);

add("South",button_textArea_panel);

}

privateclass StartListenerimplements ActionListener{

publicvoid actionPerformed(ActionEvent e){

if(pressed ==true)

{

area.setVisible(false);

Start.setLabel("show text area");

pressed=false;

}

else

{

area.setVisible(true);

Start.setLabel("Hide text area");

pressed=true;

}

}// end actionPerformed

}// end class StartListener

}

every thing is working correctly but,

the Problem is:

How to make the text area place preserved and the button position fixed,so that the button will not move when showing and hiding the text area?

[4266 byte] By [J_proga] at [2007-10-3 8:29:46]
# 1

Don't mix Swing components and AWT components.

Don't use such antiquated example code.

Try and study this code. If this does not satisfy your requirement, try use more appropriate layout manager. Don't use FlowLayout ... it just ffflllows everything.

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class ShowHide extends JApplet {

JTextArea area;

JLabel lb;

JPanel textareaPanel;

JPanel labelPanel;

JPanel buttonsPanel;

JButton start;

boolean pressed = false;

public void init() {

labelPanel = new JPanel();

labelPanel.setLayout(new BorderLayout());

labelPanel.add(new JLabel("show/ hide textArea ", JLabel.CENTER));

textareaPanel = new JPanel();

textareaPanel.setBackground(new Color(228, 241, 250));

area = new JTextArea(5,20);

area.setVisible(false);

textareaPanel.add(area);

start = new JButton(" show text area");

start.addActionListener(new StartListener());

buttonsPanel = new JPanel();// i used Panel because i have many buttons

buttonsPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

buttonsPanel.add(start);

Container con = getContentPane();

con.add(labelPanel, BorderLayout.NORTH);

con.add(textareaPanel, BorderLayout.CENTER);

con.add(buttonsPanel, BorderLayout.SOUTH);

}

private class StartListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

if(pressed == true){

area.setVisible(false);

start.setText("show text area");

pressed=false;

}

else{

area.setVisible(true);

start.setText("Hide text area");

pressed=true;

}

}

}

public static void main(String[] args){

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

ShowHide sh = new ShowHide();

sh.init();

frame.setContentPane(sh);

frame.setSize(400, 300);

frame.setVisible(true);

}

}

hiwaa at 2007-7-15 3:36:42 > top of Java-index,Java Essentials,New To Java...