Page 2 of 2 FirstFirst 12
Results 21 to 35 of 35

Thread: Java Applet

  1. #21
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Java Applet

    Look up the API?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  2. #22
    WillHorne is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Default Re: Java Applet

    Ive managed to find a way around my problem. sort of

    i can now get what i want printing next to the textbox. but only when the area that the JLabel is in is hidden when the Applet it minimized

    i cant really explain it but if you run the applet yourself them maybe you can help explain this

    run the App, maximize it, type 'River' into the textbox, click the button - nothing happens
    minimize the app, click the button - it works

    Java Code:
    import java.applet.Applet;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    	public class button extends Applet  implements ActionListener
    	{	
    		
    	    	 
    		//TEXT BOX AND BUTTON
    			 
    		     JButton okButton;								   					
    		     JTextField nameField;
    		     JLabel Resturant;
    		     JLabel Picnicarea;
    		     JLabel Playarea;
    		     JLabel Lake;
    		     JLabel River;
    		     JLabel NoMatch;
    		     
    		     {
    		    	setLayout(null);
    		        okButton = new JButton("Search");		        
    		        nameField = new JTextField("Search Term",100);
    		        Resturant = new JLabel("All Resturants Have Air-Conditioning");
    		        Picnicarea = new JLabel("All Picnin Areas are non-Smoking");
    		        Playarea = new JLabel("All Play Areas are for Under-12's");
    		        Lake = new JLabel("All Lakes are Non-Swimming");
    		        River = new JLabel("River is deep, Keep small children away from edge");
    		        NoMatch = new JLabel("No Match" + " - " + nameField.getText() );
    		     }
    
    		        {
    		          okButton.setBounds(140,20,160,25);
    		          nameField.setBounds(20,20,100,25);
    		          Resturant.setBounds(325,20,500,25);
    		          Picnicarea.setBounds(325,20,500,25);
    		          Playarea.setBounds(325,20,500,25);
    		          Lake.setBounds(325,20,500,25);
    		          River.setBounds(325,20,500,25);
    		          NoMatch.setBounds(325,20,500,25);
    		          
    		          
    		        } 
    
    		        {
    		      add(okButton);
    		      add(nameField);
    		      okButton.addActionListener(this);
    		     }
    		        
    									
    		 public void actionPerformed(ActionEvent e) {
    		        	
    		        	if (nameField.getText().equals("Resturant")) 
    		        		add(Resturant);
    		        	
    		        	else if (nameField.getText().equals("Picnic Area")) 	
    		        		add(Picnicarea);
    		        		
    		        	else if (nameField.getText().equals("Play Area")) 	
    		        		add(Playarea);
    		        		
    		        	else if (nameField.getText().equals("Lake"))	
    		        		add(Lake);
    		        		
    		        	else if (nameField.getText().equals("River")) 
    		        		 add(River);
    		        		
    		        	else 	
    		        		 add(NoMatch);
    		        		
    
    }
    				
    				
    				
    				
    				
    					
    					
    				}

  3. #23
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Java Applet

    You're adding a new thing to the applet window, so you would need to redraw the applet.
    Try a redraw() (or is it repaint()?).
    That's what's happening when you resize.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #24
    WillHorne is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Default Re: Java Applet

    thanks seems to have sorted it now

    how do i get it to remove the previous text when i enter another search

    as there just stacking up on eachother now
    Last edited by WillHorne; 03-01-2012 at 03:41 PM.

  5. #25
    WillHorne is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Default Re: Java Applet

    is there a way to remove the previous text when i enter another search

    as there just stacking up on eachother now

  6. #26
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Java Applet

    Which is why I said use a single blank JLabel whose text you set.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #27
    WillHorne is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Default Re: Java Applet

    I don't understand what you mean

    how do you have a blank JLabel and set all the if statements to it

  8. #28
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Java Applet

    Java Code:
    public class TestClass {
        private JLabel messageLabel = new JLabel("");
    
        void buildGUI() {
            .. gui building code
            somePanel.add(messageLabel);
            .. more gui building code
        }
    
        public void actionPerformed() {
            if (<some criteria>) {
                messageLabel.setText("Some criteria matched");
            } else {
                messageLabel.setText("");
            }
        }
    }
    Note the above is not intended to be runnable...check with the API for the correct names of methods etc.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #29
    WillHorne is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Default Re: Java Applet

    thanks all sorted



    could you explain why a
    Java Code:
    public void paint (Graphics g)
         {Graphics2D g2 = (Graphics2D) g;
         Graphics2D g2d = (Graphics2D)g;
    and a
    Java Code:
     public void actionPerformed(ActionEvent e) {
    are not working together

    the applet compiles and runs but the actionevent will not produce anything

  10. #30
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Java Applet

    I could have sworn I replied to this.
    What are you doing in your actionPerformed?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #31
    WillHorne is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Default Re: Java Applet

    the actionperformed is the button and the paint is the graphics of the applet



    they are two completely different classes but when the applet is run the button does not produce anything

  12. #32
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Java Applet

    Are you doing a repaint()?
    This could also be down to using AWT and Swing in this way.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #33
    WillHorne is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Default Re: Java Applet

    nope no repaint() being used

  14. #34
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Java Applet

    Then how does the GUI know to redraw itself in the actionPerformed?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #35
    WillHorne is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Default Re: Java Applet

    It doesn't need to redraw itself

    the actionPerformed sets text to a blank JLabel

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 8
    Last Post: 12-14-2011, 12:40 PM
  2. Replies: 30
    Last Post: 08-26-2011, 09:05 PM
  3. Java Applet to Applet communication
    By jsman in forum Java Applets
    Replies: 3
    Last Post: 04-05-2011, 02:23 PM
  4. applet class inside java.applet.* or java.awt.*
    By Manish87 in forum Java Applets
    Replies: 1
    Last Post: 09-27-2010, 02:15 PM
  5. Replies: 3
    Last Post: 07-23-2008, 08:07 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
  •