Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: applets

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default applets

    HI, Can someone help me. In the checkbox below, what I want to achieve is, as a selection is made, (say 120), the value 120 to be part of the drawLine method. Thank you for your time.

    [code = java]
    import java.applet.*; import java.awt.*; import java.awt.event.*; import java.lang.*;
    public class choice extends Applet implements ItemListener{
    Choice lang;
    String msg="";

    public void init(){
    lang = new Choice();

    lang.add("110");
    lang.add("120");
    lang.add("130");
    lang.add("140");
    lang.add("150");

    add(lang);

    lang.addItemListener(this);
    }

    public void itemStateChanged (ItemEvent e){
    repaint();
    }

    public void paint(Graphics g){
    msg = " ";
    msg = msg+lang.getSelectedItem();
    g.drawString(msg,100,40);
    }
    }
    [/code]


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: applets

    value 120 to be part of the drawLine method
    Put the value into a class variable and it will be available to any method in the class.

    Did you look at the post after you pressed the "Post Reply" button? Was it formatted correctly?
    Remove the spaces in the code tag.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: applets

    Sorry about the spaces in the tag. Will make sure not to repeat.

    Will you kindly show me how to put the value into a class variable, perhaps by modifying the code.



    import java.applet.*; import java.awt.*; import java.awt.event.*; import java.lang.*;
    public class choice extends Applet implements ItemListener{
    Choice lang;
    String msg="";
     
    public void init(){
    lang = new Choice();
     
    lang.add("110");
    lang.add("120");
    lang.add("130");
    lang.add("140");
    lang.add("150");
     
    add(lang);
     
    lang.addItemListener(this);
    }
     
    public void itemStateChanged (ItemEvent e){
    repaint();
    }
     
    public void paint(Graphics g){
    msg = " ";
    msg = msg+lang.getSelectedItem();
    g.drawString(msg,100,40);
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: applets

    how to put the value into a class variable
    For example msg is a class variable. You can assign a value to msg from anywhere in the class and any method will be able to access the contents of msg.


    What happened to the indentations of the statements? Now they all start in the first column????
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: applets

    Sorry, I copied the code from the first message, that is the reason. I think I got what you meant.

    BTW, may god bless you truly well for all the help you volunteer.

Similar Threads

  1. How to use CardLAyout without using applets???
    By divyarattan in forum AWT / Java Swing
    Replies: 4
    Last Post: August 9th, 2011, 11:19 AM
  2. New to applets
    By Asteroid555 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 30th, 2011, 03:48 PM
  3. Help With Applets
    By mukilan in forum AWT / Java Swing
    Replies: 12
    Last Post: February 2nd, 2011, 11:18 PM
  4. Applets
    By santiagomez in forum Java Theory & Questions
    Replies: 2
    Last Post: July 31st, 2010, 04:05 PM
  5. Help needed with applets
    By Brt93yoda in forum What's Wrong With My Code?
    Replies: 16
    Last Post: July 12th, 2010, 07:36 PM