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.


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

Results 1 to 3 of 3

Thread: Variable from superclass using in JFrame class

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Thanked 0 Times in 0 Posts

    Default Variable from superclass using in JFrame class

    Hopes my title explains a lot, but if it's still a little vague. I'll try to explain it a bit more. I would like to pass a variable I have in my main to my JFrame. In my main I calculate which pictures I should show in my JFrame. Is it possible to make objects of these pictures in my main class and use them in my JFrame? something like my code below. Of course this doesnít work but itís to clarify my question

    public class JFrameTesting {
    	public static void main(String[] args) {
    		Image ImageVariable = Toolkit.getDefaultToolkit().getImage("C:\\1.jpg");
    		MyJFrame f = new MyJFrame();
    		f.setTitle("Drawing Graphics in Frames");
    	   static class MyJFrame extends JFrame {
    		private static final long serialVersionUID = 1L;
    		public void paint(Graphics g) {
    			g.drawImage(ImageVariable, 20, 50, this);

  2. #2
    Senior Member
    Join Date
    Jul 2013
    Thanked 121 Times in 105 Posts

    Default Re: Variable from superclass using in JFrame class

    There are several ways of doing this.

    But first of all, your JFrameTesting class is NOT a superclass for your MyJFrame class. The MyJFrame class is an inner class of your JFrameTesting class. There is a huge difference and you should remember this.

    Now as to how to get your desired results. A good solution would be to save the image within your MyJFrame class as a field. For this field you could write a Setter-method, that is, a method to set the fields value. From your main method you could call the Setter-method to set the field to your loaded image.

    If you dont know how fields or Setters work I would advice you to read some books or tutorials about java.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 697 Times in 679 Posts

    Default Re: Variable from superclass using in JFrame class

    Other comments:

    Why build your interface in the main() method? Use the interface's constructor.

    Overriding the paint() method is not recommended. Review Oracle's "Custom Painting" tutorial.

    In general, this is an awkward design that is likely being driven by you trying to make things work within the limited context of what you already know. Instead of making it up as you go, find a number of decent tutorials on writing graphical interfaces - the Oracle Swing tutorials are an okay place to start - and do them all. Come here with your work to ask for pointers and anytime you need help.

Similar Threads

  1. Replies: 18
    Last Post: March 30th, 2013, 09:11 AM
  2. a superclass variable can reference a subclass but here why cant cv access ???
    By prajwalfc in forum Object Oriented Programming
    Replies: 1
    Last Post: June 20th, 2012, 07:16 AM
  3. Overriding Superclass Static Methods, Using the Superclass Method
    By snowguy13 in forum Java Theory & Questions
    Replies: 10
    Last Post: December 21st, 2011, 08:30 AM
  4. Assigning a Value to a Class extending a Superclass
    By ubermoe in forum Object Oriented Programming
    Replies: 9
    Last Post: September 22nd, 2011, 11:17 AM
  5. [SOLVED] Help regarding Superclass variable can reference subclass object
    By rohan22 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 12th, 2011, 01:30 AM