• Post Reply Bookmark Topic Watch Topic
  • New Topic

changing the Applet apprearance

 
j susan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose I have an applet with a button. When the button is clicked, I want to remove that button and show a textfield there. How do I do it? I tried removing the button from ContentPane of the applet and then adding the textfield, didn't work. The textfield does not appear(seems it appear at the same place as button !), the button is still there.
//code
public class Ex31 extends JApplet implements ActionListener {
JTextField ans;
JButton learn;
Container c;
String msg, status;
int a, b;
public void init() {
c = getContentPane();
c.setLayout(new FlowLayout());
learn = new JButton("Start learning !");
learn.addActionListener(this);
c.add(learn);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == learn) {

c.remove(learn);
ans = new JTextField(5);
c.add(ans);

//code continued

Thanks...
js
:roll:
 
Greg Brouelette
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally I try to stay away from GridBagLayout. But this could be a good place to use it. You could have 2 "cells" in the grid bag: the button and the text field. Then use setVisible(boolean) to turn one or the other on and off.
The GridBagLayout will allow the two components to occupy the same location in your applet.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!