| Author |
Remove JPanel Components
|
Haroldo Level
Greenhorn
Joined: Feb 17, 2005
Posts: 6
|
|
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
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24050
|
|
|
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.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Haroldo Level
Greenhorn
Joined: Feb 17, 2005
Posts: 6
|
|
|
The program itself still running but you can do nothing in the applet window, it just do nothing
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24050
|
|
|
Well, you've removed all the components except a label. What do you expect it to be able to do?
|
 |
Haroldo Level
Greenhorn
Joined: Feb 17, 2005
Posts: 6
|
|
|
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
Marshal
Joined: Jul 08, 2003
Posts: 24050
|
|
|
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
Joined: Feb 17, 2005
Posts: 6
|
|
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
Joined: Feb 17, 2005
Posts: 6
|
|
|
the part at that is not working is in the connectToChat Method
|
 |
Craig Wood
Ranch Hand
Joined: Jan 14, 2004
Posts: 1535
|
|
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.
|
 |
 |
|
|
subject: Remove JPanel Components
|
|
|