I have created a JApplet and I want to be able to clear the screen and draw new components depending on what button I click on. Right now the code below will clear the screen but it doesn't redraw the JTextFields (label_1 & label_2). However I did notice whenever I resize the applet(when it pops up in eclipse) the JTextFields suddenly reappear. Is there some type of refresh or restart method I need to call to get the components to draw correctly. Any tips or help in the right direction would be greatly appreciated. Here is my code so far.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Main_app extends JApplet 
{
	//Called when this is loaded into the browser.
	public static Container content;
	//public static JFrame frame;
	
	
    public void init() {
    	//Create the frame and content manager
		//frame = new JFrame("Cryptography Algorithms");
		content = getContentPane();
		setSize(600,600);
		content.setBackground(Color.white);
		//content.setSize(new Dimension(600,600));
		
		//Set the flow for the content
		FlowLayout mgr = new FlowLayout();
		mgr.setHgap(10);
		mgr.setVgap(10);
		mgr.setAlignment(FlowLayout.LEFT);
		content.setLayout(mgr);
		
        //Add buttons to First Page
        
        //Vigenere button
        JButton vigenere_b = new JButton("Vigenere Cipher");
        vigenere_b.addActionListener(new ActionListener()
        {
        	public void actionPerformed(ActionEvent e)
        	{
        		vigenere_gui();
        	}
        });
        content.add(vigenere_b);
        
        //Inverse Matrix
        JButton hill_b = new JButton("Hill Cipher");
        hill_b.addActionListener(new ActionListener()
        {
  		  public void actionPerformed(ActionEvent e)
  		  {
  			hill_gui();
  		  } 
  		});
        content.add(hill_b);
        //Reset Button
        JButton reset_b = new JButton("Reset");
        reset_b.addActionListener(new ActionListener()
        {
  		  public void actionPerformed(ActionEvent e)
  		  {
  			reset_gui();
  		  } 
  		});
        content.add(reset_b);
    }
   
    //
    //	Clears the applet display
    //
    public static void reset_gui()
    {
    	content.removeAll();
    	content.repaint();
    	
    }
    
    //
    //	Set up display for vigenere cipher
    //
    public static void vigenere_gui()
    {
    	reset_gui();
    	
    }
    
    //
    //	Set up display for hill cipher
    //
   // public static Container hill_container;
    public void hill_gui()
    {
    	reset_gui();
    	content = getContentPane();
    	JTextField label_1 = new JTextField(70);
    	label_1.setBorder(BorderFactory.createEmptyBorder());
		label_1.setText("Hill Cipher");
		content.add(label_1);
		JTextField label_2 = new JTextField(70);
		label_2.setBorder(BorderFactory.createEmptyBorder());
		label_2.setText("Enter the encryption matrix to get the decryption matrix");
		content.add(label_2);
		content.paint(content.getGraphics());
		//content.setVisible(true);
    	
    }
}

call validate and then repaint (on the entire JApplet, i.e. just like you're calling getContentPane), rather than that content.paint call.

First init all components
Used BorderLayout and CardLayout

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Main_app extends JApplet {

    private Container content;
    private JPanel centerPanel;
    private CardLayout card;

    public void init() {
        content = getContentPane();
        setSize(800, 600);
        content.setBackground(Color.GREEN);
        content.setLayout(new BorderLayout());
        JPanel northPanel = new JPanel();
        JButton vigenere_b = new JButton("Vigenere Cipher");
        vigenere_b.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                vigenere_gui();
            }
        });
        northPanel.add(vigenere_b);

        //Inverse Matrix
        JButton hill_b = new JButton("Hill Cipher");
        hill_b.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                hill_gui();
            }
        });
        northPanel.add(hill_b);

        //Reset Button
        JButton reset_b = new JButton("Reset");
        reset_b.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                reset_gui();
            }
        });
        northPanel.add(reset_b);
        //
        content.add(northPanel, BorderLayout.NORTH);
        //
        centerPanel = new JPanel();
        card = new CardLayout();
        centerPanel.setLayout(card);
        //
        JPanel hillPanel = new JPanel();
        hillPanel.setBackground(Color.RED);
        JTextField label_1 = new JTextField(70);
        label_1.setBorder(BorderFactory.createEmptyBorder());
        label_1.setText("Hill Cipher");
        hillPanel.add(label_1);
        JTextField label_2 = new JTextField(70);
        label_2.setBorder(BorderFactory.createEmptyBorder());
        label_2.setText("Enter the encryption matrix to get the decryption matrix");
        hillPanel.add(label_2);

        // add hillPanel known as "hill"
        centerPanel.add("hill", hillPanel);
        //
        JPanel vigenerePanel = new JPanel();
        vigenerePanel.setBackground(Color.BLUE);
        vigenerePanel.add(new JButton("button vigenere"));

        // add vigenerePanel known as "vigenere"
        centerPanel.add("vigenere", vigenerePanel);
        //
        JPanel emptyPanel = new JPanel();
        emptyPanel.setBackground(Color.WHITE);

        // add emptyPanel known as "empty"
        centerPanel.add("empty", emptyPanel);

        content.add(centerPanel, BorderLayout.CENTER);
    }

    public void vigenere_gui() {
        card.show(centerPanel, "vigenere");
    }

    public void hill_gui() {
        card.show(centerPanel, "hill");
    }

    public void reset_gui() {
        card.show(centerPanel, "empty");
    }
}

now implement your code

Comments
Thanks so much

Yes, CardLayout is a good option, but doing other's work for them is not. Simply suggest CardLayout and link to the tutorial for it, or give a "generic" example of it.

This article has been dead for over six months. Start a new discussion instead.