Results 1 to 16 of 16

Thread: Need Help

  1. #1
    incredibleX is offline Member
    Join Date
    Nov 2013
    Posts
    30
    Rep Power
    0

    Default Need Help

    There are two cases I've seen.
    Case 1:
    Java Code:
    import javax.swing.*;
    
    public class A{
    
    private JFrame f=new JFrame();
    
    A(){
    JOptionPane.showMessageDialog(f,"Hello There");
    }
    
    public static void main(String args[]){
           new A();
    }
    }
    case 2:

    Java Code:
    import javax.swing.*;
    
    public class A extends JFrame{
    
    A(){
    JOptionPane.showMessageDialog(?,"Hello There");
    }
    
    public static void main(String args[]){
           new A();
    }
    }
    In case two what to write in place of "?" to tell that i'm using frame "f".

    I hope you got my point.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: Need Help

    this. But don't use case 2.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Need Help

    In your 2nd example, your class A extends a JFrame. What that is essentially says is that "A is a JFrame".
    Therefore, you can pass the instance of A as a parameter, using the 'this' keyword.
    Keep in mind that in general it is a bad idea to reference the instance (with the this keyword) inside the constructor, because the object
    may be in an 'incomplete state'. I agree with jim, example 2 isn't the way to go.

  4. #4
    incredibleX is offline Member
    Join Date
    Nov 2013
    Posts
    30
    Rep Power
    0

    Default Re: Need Help

    JOptionPane.showMessageDialog(this,"Hello There"); will work same as JOptionPane.showMessageDialog(f,"Hello There"); ?

  5. #5
    incredibleX is offline Member
    Join Date
    Nov 2013
    Posts
    30
    Rep Power
    0

    Default Re: Need Help

    But in the following case
    Java Code:
    public static void main(String args[]){
    	try  
            {  
                UIManager.setLookAndFeel(new MetalLookAndFeel());  
                SwingUtilities.updateComponentTreeUI(f);  
            }  
            catch(Exception e)  
            {  
                e.printStackTrace();  
            }  
        new A();
    }
    what shoud i do as i don't want to make my JFrame static.
    Should i use "this" keyword here.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: Need Help

    You can't use "this" in a static context. And in your example there is no need to.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: Need Help

    Yes. But 1) it is risky because of what sehudson said, and 2) it is typically considered poor technique to extend JFrame.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    incredibleX is offline Member
    Join Date
    Nov 2013
    Posts
    30
    Rep Power
    0

    Default Re: Need Help

    Quote Originally Posted by jim829 View Post
    You can't use "this" in a static context. And in your example there is no need to.

    Regards,
    Jim
    Bro what frame name i can use if i have extended JFrame ?

    Java Code:
    public static void main(String args[]){
        try 
            {  
                UIManager.setLookAndFeel(new MetalLookAndFeel());  
                SwingUtilities.updateComponentTreeUI(  frame name  );  
            }  
            catch(Exception e)  
            {  
                e.printStackTrace();  
            }  
        new A();
    }

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: Need Help

    Sorry, I didn't see the f there. I was focused on the JOptionPane example. You don't need to update your component because it doesn't exist yet. The new L&F should take effect when you instantiate A.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    incredibleX is offline Member
    Join Date
    Nov 2013
    Posts
    30
    Rep Power
    0

    Default Re: Need Help

    Quote Originally Posted by jim829 View Post
    Sorry, I didn't see the f there. I was focused on the JOptionPane example. You don't need to update your component because it doesn't exist yet. The new L&F should take effect when you instantiate A.

    Regards,
    Jim

    i'm trying this :

    Java Code:
    public static void main(String args[]){
        try
            {  
                UIManager.setLookAndFeel(new MetalLookAndFeel());  
                SwingUtilities.updateComponentTreeUI(  A()  );  
            }  
            catch(Exception e)  
            {  
                e.printStackTrace();  
            }  
        new A();
    }
    but it is'nt working. Error is there.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: Need Help

    what does this do?

    Java Code:
    public static void main(String args[]){
        try
            {  
                UIManager.setLookAndFeel(new MetalLookAndFeel());  
                // SwingUtilities.updateComponentTreeUI(  A()  );  // omit this statement
            }  
            catch(Exception e)  
            {  
                e.printStackTrace();  
            }  
        new A();
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    incredibleX is offline Member
    Join Date
    Nov 2013
    Posts
    30
    Rep Power
    0

    Default Re: Need Help

    Quote Originally Posted by jim829 View Post
    what does this do?

    Java Code:
    public static void main(String args[]){
        try
            {  
                UIManager.setLookAndFeel(new MetalLookAndFeel());  
                // SwingUtilities.updateComponentTreeUI(  A()  );  // omit this statement
            }  
            catch(Exception e)  
            {  
                e.printStackTrace();  
            }  
        new A();
    }
    Regards,
    Jim
    This is what i get:

    Exception in thread "main" java.awt.IllegalComponentStateException: The frame is decorated
    at java.awt.Frame.setBackground(Unknown Source)
    at javax.swing.JFrame.frameInit(Unknown Source)
    at javax.swing.JFrame.<init>(Unknown Source)

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: Need Help

    It doesn't do that for me. So you must not be posting all of your relevant code. This works for me just fine (and I have included your earlier constructor).

    Java Code:
    public class A extends JFrame { // don't do this
       A(){
           JOptionPane.showMessageDialog(this,"Hello There");
       }
       public static void main(String args[]){
        try { 
               UIManager.setLookAndFeel(new MetalLookAndFeel());
         } catch(Exception e) { 
               e.printStackTrace(); 
         } 
         new A();
      }
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    incredibleX is offline Member
    Join Date
    Nov 2013
    Posts
    30
    Rep Power
    0

    Default Re: Need Help

    okay i have identified it in my case..

    is i using Napkin laf.

    try {
    UIManager.setLookAndFeel(new NapkinLookAndFeel());
    } catch(Exception e) {
    e.printStackTrace();
    }

    that error occurs.
    While in case of Metal laf it is okay.
    Why so?

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: Need Help

    I have no idea since I am unfamiliar with that. Sorry.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    incredibleX is offline Member
    Join Date
    Nov 2013
    Posts
    30
    Rep Power
    0

    Default Re: Need Help

    Quote Originally Posted by jim829 View Post
    I have no idea since I am unfamiliar with that. Sorry.

    Regards,
    Jim
    Error in NapKin Look and Feel (Swing / AWT / SWT forum at JavaRanch)
    Thats the problem i have same to same.
    But i don't understand the solution he gave.
    Please Review and explain.

Posting Permissions

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