Results 1 to 2 of 2
  1. #1
    Majora94 is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Page not refreshing properly within a swing JApplet

    Hi, I started java about a week ago and have been trying to make a swing JApplet for a college assignment. The problem I am getting is that when you click a button on the main page, you can see the compnents of the next page rearanging slightly before they are displayed. Is this a problem with my code or does this have to happen?

    My JApplet ishere. You will see what I am talking about when you navigate to a page by clicking a button on the menu.

    Here is my code for any suggestions. (BTW am I even initialising properly? I'm still new and not quite sure so any help will be much appretiated)

    Java Code:
    import java.awt.*;
    import java.applet.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.event.*;
    
    public class main extends JApplet
    {
    	JPanel screen = new JPanel();
    	
    	int width;
    	
    	int menuOption = 0;
    	int numberOfOptions = 9;
    	String[] optionNames = {
    		"Local Variables", 
    			"Procedures", 
    				"Functions", 
    					"Loops (for)", 
    						"Loops (while)", 
    							"Conditional Statements", 
    								"Boolean Operators", 
    									"Perameter Passing", 
    										"Debugging"
    											};
    	
    	public void init()
    	{
    		
    		try
    		{
    			SwingUtilities.invokeAndWait(new Runnable() {
                	public void run() {
    					start();
                	}
            	});
    		}
    		catch(Exception e)
    		{
    			
    			System.out.println("Failure to init: "+e);
    		}
    	}										
    											
    	public main()
    	{
    		programSwitch();
    	}
    	
    	public void programSwitch()
    	{
    		screen.removeAll();
    		switch(menuOption)
    		{
    			case 0:
    				menu();
    				break;
    			case 1:
    				VariablesPage();
    				break;
    			case 2:
    				VariablesPage();
    				break;
    			case 3:
    				VariablesPage();
    				break;
    			case 4:
    				VariablesPage();
    				break;
    			case 5:
    				VariablesPage();
    				break;
    			case 6:
    				VariablesPage();
    				break;
    			case 7:
    				VariablesPage();
    				break;
    			case 8:
    				VariablesPage();
    				break;
    			case 9:
    				VariablesPage();
    				break;
    			default:
    				menu();
    				break;
    		}
    		revalidate();
    	}
    	
    	public void menu()
    	{
    		screen.setLayout(new BorderLayout());
    		add(screen);
    		
    		JPanel titlePanel = new JPanel();
    		screen.add(titlePanel, BorderLayout.NORTH);
    		
    		JLabel title = new JLabel("Basic Programming Tutorials");
    		titlePanel.add(title);
    		
    		JPanel menuPanel = new JPanel();
    		menuPanel.setLayout(new GridLayout(0,3));
    		screen.add(menuPanel, BorderLayout.CENTER);
    		
    		
    		JButton[] menuOptions = new JButton[numberOfOptions];
    		for(int i = 0; i < numberOfOptions; i++)
    		{
    			menuOptions[i] = new JButton(optionNames[i]);
    			menuPanel.add(menuOptions[i]);
    			
    			menuOptions[i].addActionListener(new ActionListener()
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					handleMenuSelect(e.getActionCommand());
    				}
    			});
    		}
    		
    	}
    	
    	public void handleMenuSelect(String option)
    	{
    		for( int i = 0; i < numberOfOptions; i++)
    		{
    			if( option == optionNames[i])
    			{
    				menuOption = i+1;
    				programSwitch();				
    			}
    		}
    	}
    	
    	public void VariablesPage()
    	{		
    		width = getSize().width;
    		screen.setLayout(new BorderLayout());
    		add(screen);
    		
    		JPanel titlePanel = new JPanel();
    		screen.add(titlePanel, BorderLayout.NORTH);		
    		
    		JLabel title = new JLabel(optionNames[menuOption-1]);
    		titlePanel.add(title);
    		
    		JPanel contentPanel = new JPanel();
    		contentPanel.setLayout(new BorderLayout());
    		screen.add(contentPanel, BorderLayout.CENTER);
    		
    		JPanel content = new JPanel();
    		content.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    		contentPanel.add(content, BorderLayout.NORTH);		
    		
    		JTextArea thankNote = new JTextArea();
    		thankNote.setText("This text area will contain information and tutorials about "+optionNames[menuOption-1]);
    		thankNote.setEditable(false);
    		thankNote.setLineWrap(true);
    		c.weightx = 1.0;
    		//c.anchor = GridBagConstraints.NORTHWEST;
    		c.gridx = 1;
    		c.gridwidth = 2;
    		c.fill = GridBagConstraints.BOTH;
    		c.insets = new Insets(10,10,10,10);
    		content.add(thankNote, c);
    		
    		JPanel pageControlPanel = new JPanel();
    		pageControlPanel.setBackground(Color.black);
    		c.weightx = 1.0;
    		c.gridx = 3;
    		c.gridwidth = 1;
    		c.ipadx = (width/3)-20;
    		c.fill = GridBagConstraints.BOTH;
    		c.insets = new Insets(10,10,10,10);
    		content.add(pageControlPanel, c);
    		
    		JButton homeButton = new JButton("HOME");
    		screen.add(homeButton, BorderLayout.SOUTH);
    		
    		homeButton.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				menuOption = 0;
    				programSwitch();
    			}
    		});
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Page not refreshing properly within a swing JApplet

    Where have you read about writing java applets?
    Normally applets do not use their constructors.
    Where is the start() method defined that you are calling in the run() method?

Similar Threads

  1. Frames are not woking properly within JApplet
    By mitra in forum Java Applets
    Replies: 1
    Last Post: 07-29-2011, 07:02 AM
  2. populating a list box based on another without refreshing the page
    By zahir in forum JavaServer Pages (JSP) and JSTL
    Replies: 8
    Last Post: 07-25-2011, 01:09 PM
  3. japplet - refreshing windows
    By Symbiot in forum New To Java
    Replies: 6
    Last Post: 05-29-2010, 06:12 PM
  4. refreshing the page automatically if the configuration file changes
    By anitha2324 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-07-2009, 03:08 PM
  5. how to stop refreshing page
    By cecily in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 01:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •