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

Thread: repaint from another class!

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default repaint from another class!

    Hi, as the topic follows im trying to repaint a window from another class. the class Window handles an interface that displays pictures of movies. Via a JMenuBar you can add a new movie and its picture but in order for it to show in the Window i need to repaint certain aspects in the Window class GUI;

    The window class constructor:
    	public Window() {
    		addComponents();
    		configurFrame();
    		addMenu();
    	}

    The functions i want to repaint:
    	public void repaintWindow() {
    		this.getContentPane().validate();
    		this.getContentPane().repaint();
    	}

    From my other class where i add a movie to an ArrayList i have made a "private Window myWindow;" there i call the function repaintWindow via mywindow.repaintWindow(); But this gives me an NullPointerException related to the repaintWindow function.

    Any suggestions?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: repaint from another class!

    gives me an NullPointerException
    Please copy the full text of the error message and paste it here. It has important info about the error.
    To call a method in another class, the code needs a reference to the instance of the other class.

    BTW Window is the name of a Java SE class. Using Java SE class names will cause confusion for people reading the code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: repaint from another class!

    Errors you say:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at project.MediaHandler.addMovie(MediaHandler.java:19)
    	at project.AddMovieWindow.addMedia(AddMovieWindow.java:73)
    	at project.AddMovieWindow$ButtonListener.actionPerformed(AddMovieWindow.java:23)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$200(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: repaint from another class!

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at project.MediaHandler.addMovie(MediaHandler.java:19 )
    At line 19 the code improperly used a null value, possibly contained in a variable. Look at line 19 and find where the null value is and backtrack in the code to see where it came from.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: repaint from another class!

    It goes back to the function i call:
    	public void repaintWindow() {
    		this.getContentPane().validate();
    		this.getContentPane().repaint();
    	}

    I'm not sure if i can call the repaint. I haven't declared a paintComponent eg, is there any other way of refreshing the content?

    --- Update ---

    I could also add that these are the functions i'm trying to repaint:

    	private void addComponents() {
    		JPanel shelfs = buildShelfs();
    		this.add(shelfs);
    	}
     
    	private JPanel buildShelfs() {
    		JPanel mediaPanel = new JPanel();
    		int nrOfMedias = myMediaHandler.mediaList.size();
    		mediaPanel.setOpaque(false);
    		mediaPanel.setLayout(new GridLayout(3,6,22,30));
    		mediaPanel.setBounds(90, 25, 1020, 745);
     
    				java.net.URL where;
    				for(int i = 0; i < nrOfMedias; i++) {
    					String temp = myMediaHandler.mediaList.get(i).getImagePath();
    					try {
    						where = new URL(temp);
    						ImageIcon image = new ImageIcon(where);
    						JLabel imageLabel = new JLabel(image);
    						mediaPanel.add(imageLabel);
    					} catch (MalformedURLException e) {
    						// TODO Auto-generated catch block
    						System.out.println("Error loading user media picture");
    						e.printStackTrace();
    					}
    				}
     
    			java.net.URL where1;
    			try {
    				for(int i = nrOfMedias; i < 18; i++) {
    					where1 = new URL("https://dl.dropboxusercontent.com/u/16670644/Projekt/TempPic.png");
    					ImageIcon image1 = new ImageIcon(where1);			
    					JLabel imageLabel1 = new JLabel(image1);
    					mediaPanel.add(imageLabel1);
    				}
     
    			} catch (MalformedURLException e) {
    				// TODO Auto-generated catch block
    				System.out.println("Error loading TempPic that shows allowed media spots");
    				e.printStackTrace();
    			}
     
    			return mediaPanel;
    	}

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: repaint from another class!

    these are the functions i'm trying to repaint:
    I'm not sure what you mean by that. methods are not repainted.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: repaint from another class!

    A reference to the object being repainted in the class doing the repainting is all that is needed. If the object being repainted is a JPanel, provide a reference to that JPanel by passing it through the repainting class' constructor or using an appropriate getter/setter method.

    If you provide a simple example of two classes where one class is calling repaint() on an object created in another class, we can show you how to do what I've described. Pretty simple but can be hard to grasp at first.

  8. The Following User Says Thank You to GregBrannon For This Useful Post:

    sebbe605 (April 2nd, 2014)

  9. #8
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: repaint from another class!

    Okay, this is a tricky one. I tried to rebuild the shelf's by simply calling:
    	private void addComponents() {
    		JPanel shelfs = buildShelfs();
    		this.add(shelfs);
    	}

    And this as far as i can tell builds a new shelf with all its updates. However it lays beneath the "old" shelf so i really don't know how to work around this.

    I also changer the MediaHandlers constructor so i can access the Window class but i'm not sure if i'm doing it right.

    	private Window myWindow;
     
    	public MediaHandler(Window window) {
    		this.mediaList = new ArrayList<Media>();
    		myWindow = window;
    	}

    Thanks for the comments guys it helped me figure some things out but i'm still stuck on the repainting stuff.

  10. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: repaint from another class!

    I can't follow what you're doing or understand what you're trying to do. Please post code that we can run to demonstrate the problem.

Similar Threads

  1. Need help with repaint();
    By Saxy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 22nd, 2013, 07:55 PM
  2. How to repaint() a class in a JFrame, with a JTextfield user input
    By DANGEROUSSCION in forum Object Oriented Programming
    Replies: 1
    Last Post: March 23rd, 2013, 03:06 PM
  3. GUI Class repaint or redoing a pane
    By gillaw06 in forum AWT / Java Swing
    Replies: 0
    Last Post: October 26th, 2011, 09:54 PM
  4. Repaint doesn't repaint?
    By PotataChipz in forum AWT / Java Swing
    Replies: 6
    Last Post: January 18th, 2010, 09:56 PM
  5. Replies: 1
    Last Post: October 23rd, 2009, 03:17 PM