• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Remove JPanel Components

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi im triying to remove all the components ina JPanle and replace it with new ones. The JPanel is running on an applet, every time I remove all components the apllet just crashes. I also tryed revalidate and repaint and the applet still dies. here is the last code I tried:

main.removeAll();
main.add(new JLabel("Just to test"), BorderLayout.CENTER); main.revalidate();
main.repaint();

Thanks in advance for all the help you can give me xD
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say it "crashes", what do you mean, exactly? Is there a stack trace? Show it to us, and we can explain it to you.
 
Haroldo Level
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The program itself still running but you can do nothing in the applet window, it just do nothing
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you've removed all the components except a label. What do you expect it to be able to do?
 
Haroldo Level
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what I mean is that it stop working. The leabel doesnt show up and it doesnt remove anithing from the window
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, generally you want to call "validate()", not "revalidate()", on a container whose contents you've changed while it's visible. Without seeing more of your code, that'd be the first thing I'd recommend.
 
Haroldo Level
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried validate also and it didn't work, here is the complete code...
public class ChatRoom extends Applet implements ActionListener, KeyListener
{
public static final int chatPort=3737;

private JPanel main;
private JTextField input;
private JTextArea status;

public void init()
{
main=new JPanel(new BorderLayout());

main.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.black),
BorderFactory.createEmptyBorder(5,5,5,5)));


drawConecctionRequest(main);

}

private void drawConecctionRequest(JPanel main)
{
JPanel conectionRequestNorth= new JPanel(new GridLayout(2,2));
JPanel conectionRequestCenter= new JPanel(new GridLayout(3,2));
JPanel conectionRequestSouth= new JPanel(new GridLayout(2,1));




try
{
Image im=(ImageIO.read(new File("logo.jpg"))).getScaledInstance(100, 75, Image.SCALE_AREA_AVERAGING);



JLabel nickName= new JLabel("NickName:");
JLabel empty1= new JLabel();
JLabel empty2= new JLabel();
JLabel empty3= new JLabel();
JLabel empty4= new JLabel();
JLabel empty5= new JLabel();
JLabel empty6= new JLabel();
JLabel empty7= new JLabel();
JLabel logo= new JLabel(new ImageIcon(im));

logo.setBorder(BorderFactory.createEmptyBorder(0,30,0,0));

input= new JTextField(20);
JButton connect=new JButton("Conectar");
status=new JTextArea();

status.setEditable(false);

nickName.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
input.setBorder(BorderFactory.createLineBorder(Color.BLUE));


connect.setBorder(BorderFactory.createLineBorder(Color.black));
status.setBorder(BorderFactory.createLineBorder(Color.BLUE));

connect.addActionListener(this);
input.addKeyListener(this);

conectionRequestNorth.add(nickName);
conectionRequestNorth.add(input);
conectionRequestNorth.add(empty6);

conectionRequestCenter.add(empty1);
conectionRequestCenter.add(empty2);
conectionRequestCenter.add(empty3);
conectionRequestCenter.add(connect);
conectionRequestCenter.add(empty4);
conectionRequestCenter.add(empty5);

conectionRequestSouth.add(empty7);
conectionRequestSouth.add(status);

main.add(conectionRequestSouth,BorderLayout.SOUTH);

main.add(conectionRequestNorth,BorderLayout.NORTH);
main.add(conectionRequestCenter,BorderLayout.CENTER);
main.add(logo,BorderLayout.WEST);

this.add(main,BorderLayout.CENTER);
}

catch(IOException e)
{
System.out.print(e);
}
}


public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Conectar"))
{
if(!input.getText().trim().equals(""))
{
status.setText("Conectando...");
connectToChat();
}

else
{
status.setText("Porfavor inserte su nickname.");
}
}
}

public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==10)
{
connectToChat();
}
}

public void keyTyped(KeyEvent e)
{
}

public void keyReleased(KeyEvent e)
{
}

private void connectToChat()
{
InetAddress remoteIP;
Socket ChatRoom;
BufferedReader recive;
PrintWriter send;

try
{
remoteIP=InetAddress.getLocalHost();
ChatRoom=new Socket(remoteIP,chatPort);

send= new PrintWriter( ChatRoom.getOutputStream( ), true );
recive=new BufferedReader(new InputStreamReader(ChatRoom.getInputStream()));

send.println(input.getText());

if(recive.readLine().equals("accepted"))
{
main.removeAll();
main.add(new JLabel("Just to test"), BorderLayout.CENTER);
main.revalidate();
main.repaint();
}

}

catch (UnknownHostException e)
{

}

catch(IOException e1)
{
status.setText("El Servidor esta desconectado, intente mas tarde.");
}
}


}
 
Haroldo Level
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the part at that is not working is in the connectToChat Method
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could try
1 — going back to AWT, ie, removing the "J" from your components, or

2 — changing Applet to JApplet and updating the gui with:

Swing is fussy about multi–threading. See How to Use Threads for more information.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic