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 6 of 6

Thread: help with my object and code

  1. #1
    Junior Member
    Join Date
    Feb 2018
    Posts
    7
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default help with my object and code

    private void FourthActionPerformed(java.awt.event.ActionEvent evt) {                                       
            // TODO add your handling code here:
     
            //FrameExample FrmExmp1 = new FrameExample();
            //FrmExmp1.getContentPane().setBackground(Color.red);
            //FrmExmp.getContentPane().setBackground(Color.red);
            if (counter == 0) {
                this.getContentPane().setBackground(Color.blue);
                counter++;
            } else {
                if (counter == 1) {
                    this.getContentPane().setBackground(Color.cyan);
                    counter--;
                }
            }
            ShowText.setForeground(Color.red);
            ShowText.setText(inputtext.getText());
            ShowText.setFont(new Font("",Font.BOLD, 24));
     
        }
    FrameExample FrmExmp = new FrameExample();
    FrmExmp.setVisible(true);
    FrmExmp.getContentPane().setBackground(Color.cyan) ;

    these three lines are form the main.

    oky the question is main methods works fine and set the backgound of the frame cyan perfectly. when i try to do this on the button press it does not work with the object created in main() method. if you can not use the object created in main() method outside the main method then i created new object in the function with the name of FrmExmp1. which still doesnt work. it works with only this.getContantPane().
    i know this has the properities of previously created object.

    this has the properties of previously created obj and works. so that means we are to allowed to create the obj in method or what?
    Last edited by basitdogar; February 9th, 2018 at 02:52 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,381
    Thanks
    53
    Thanked 2,397 Times in 2,350 Posts

    Default Re: help with my object and code

    Can you make a small, simple program that compiles, executes and shows the problem?
    I don't understand what you are saying is the problem.

    Note: java programming conventions say that variable names should start with lowercase letters. Class names with uppercase letters.
    That makes it easier to read and understand code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    basitdogar (February 7th, 2018)

  4. #3
    Junior Member
    Join Date
    Feb 2018
    Posts
    7
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: help with my object and code

    Quote Originally Posted by Norm View Post
    Can you make a small, simple program that compiles, executes and shows the problem?
    I don't understand what you are saying is the problem.

    Note: java programming conventions say that variable names should start with lowercase letters. Class names with uppercase letters.
    That makes it easier to read and understand code.

    thanks for replying sir
    attachment is there to download its in net-beans.

    it works just want to know about the commented code in FourthActionPerformend() method
    thanks for telling me about objects i have changed the name to small letters.
    but my concern is why i cant use object it self rather using this in that method.

     
    private void FourthActionPerformed(java.awt.event.ActionEvent evt) {                                       
            // TODO add your handling code here:
     
            //FrameExample FrmExmp1 = new FrameExample();
            //FrmExmp1.getContentPane().setBackground(Color.red);
     
            if (counter == 0) {
                this.getContentPane().setBackground(Color.blue);
                counter++;
            } else {
                if (counter == 1) {
                    this.getContentPane().setBackground(Color.cyan);
                    counter--;
                }
            }
            ShowText.setForeground(Color.red);
            ShowText.setText(inputtext.getText());
            ShowText.setFont(new Font("",Font.BOLD, 24));
     
        }

    the commented code dont work why?
    Attached Files Attached Files

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,381
    Thanks
    53
    Thanked 2,397 Times in 2,350 Posts

    Default Re: help with my object and code

    why i cant use object it self rather using this in that method.
    If you have a variable that references an instance of a class (your object?) you can call its methods for what you want to do.
    theReference.theMethod();  // call a method in class referenced by theReference
        FrameExample frmExmp1 = new FrameExample();   // create a new instance of a class
        frmExmp1.getContentPane().setBackground(Color.red);  // calls a method in that class
    commented code dont work why?
    Why do you say that? What is the code supposed to do?

    Please post a SMALL example program that compiles, executes and shows the problem.
    No as attachments or links. Put the code here.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Member
    Join Date
    Apr 2014
    Posts
    81
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: help with my object and code

    Quote Originally Posted by basitdogar View Post
    thanks for replying sir
    attachment is there to download its in net-beans.

    it works just want to know about the commented code in FourthActionPerformend() method
    thanks for telling me about objects i have changed the name to small letters.
    but my concern is why i cant use object it self rather using this in that method.

     
    private void FourthActionPerformed(java.awt.event.ActionEvent evt) {                                       
            // TODO add your handling code here:
     
            //FrameExample FrmExmp1 = new FrameExample();
            //FrmExmp1.getContentPane().setBackground(Color.red);
     
            if (counter == 0) {
                this.getContentPane().setBackground(Color.blue);
                counter++;
            } else {
                if (counter == 1) {
                    this.getContentPane().setBackground(Color.cyan);
                    counter--;
                }
            }
            ShowText.setForeground(Color.red);
            ShowText.setText(inputtext.getText());
            ShowText.setFont(new Font("",Font.BOLD, 24));
     
        }

    the commented code dont work why?
    Hi,

    The commented-out code doesn't work because it's changing the color of a newly-created FrameExample object instead of the actual FrameExample object that you're seeing on the screen. It's basically making an invisible clone and changing that clone's color behind the scenes where nobody can see it.

    In the code that works, 'this.getContentPane()' and simply 'getContentPane()' are equivalent. You only need the 'this' keyword when there's ambiguity (an anonymous sublcass that has a function with the same name which you don't want to call, or a function argument named the same as a class member, things like that.)

    The logic simply adds 1 to 'counter', then removes 1 from 'counter', and changes the background depending on whether 'counter' is 1 or 0. Since you appear to only ever have two states (blue or cyan), you might consider re-factoring to use a boolean instead of an int. It'll still work either way, of course, but it's always a good idea to use the right tool for the job.

    Since 'ShowText' is a variable, not a class, it should be named 'showText' as Norm suggested. And 'inputtext' should be 'inputText'. It makes no functional difference, but it helps a lot with read-ability.

  7. The Following User Says Thank You to BinaryDigit09 For This Useful Post:

    basitdogar (February 9th, 2018)

  8. #6
    Junior Member
    Join Date
    Feb 2018
    Posts
    7
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: help with my object and code

    BinaryDigit09 and Norm
    both you are really helping as i am a self taut student i feel really blessed to have people like you.

    BinaryDigit09
    your reply does make a lot of help to me and am not confused about it any more. the way you tell me to name them or write them. thank you very much.
    thanks a lot i will pray for you to be successful in your life. thanks a lot Sir.

Similar Threads

  1. Want to add an object to an array whilst inheriting another object?
    By lewihenda93 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 31st, 2017, 02:30 PM
  2. code to move an object in grid
    By soccerstar in forum Java Theory & Questions
    Replies: 1
    Last Post: August 22nd, 2014, 08:55 PM
  3. [SOLVED] help with java game source code..
    By hemla in forum Object Oriented Programming
    Replies: 7
    Last Post: March 7th, 2013, 05:48 PM
  4. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  5. Object and text file stream code problmes
    By Kakashi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2011, 03:34 PM