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

Thread: Canvas in JFrame, does not show content

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Canvas in JFrame, does not show content

    Hello guys,

    on a previous post I have asked for help and, later on, succeeded in developing a browser using SWT.
    Now, since I wanted to embed it, I have put it into a Canvas and now I need to add this canvas onto a JFrame.

    The problem is that the canvas is added (you can see the area it allocates), but the content of the canvas (the webpage), does not show up.
    But, if I create a whole new JFrame just for the canvas, then it works.
    But I need to have just 1 Frame with the browser in it.

    I am running a thread for the GUI and then a thread for the Browser.

    I will add, from a different class, the component Canvas and set its bounds. here is some code:

    		final Display display = new Display();
     
     
    	       Canvas embedded = new Canvas(); 
     
    	       GUI.mainFrame.add(embedded); 
     
     
    	       embedded.setBounds(0,0,300,300);
     
    	       final Shell shell = SWT_AWT.new_Shell(display, embedded); 
    	       shell.setLayout(new FillLayout(SWT.VERTICAL));


    Is there maybe a problem here?


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Canvas in JFrame, does not show content

    I actually realized that the canvas is shown only few times (have no idea why). It looks like a 50% chance.
    Is there a way to force the draw of a canvas or JPanel? (I put the canvas into a Jpanel)
    I thought there was something like repaint or redraw or simply refresh but nothing.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Canvas in JFrame, does not show content

    Without an SSCCE, I'm only guessing, but you might want to read through this: Mixing heavy and light components

    Also, I'm not sure what kinds of wonkiness you're doing with your threading. And what LayoutManager are you using? I see you're calling setBounds.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Canvas in JFrame, does not show content

    Just second the SSCCE advice. I would be interested to test this, but cannot given the small code snippet. It may have to do with mixing heavy and light components, but presume it has more to do with mixing Swing and SWT (I presume this is SWT?).

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Canvas in JFrame, does not show content

    Here it is, thanks for the link, I didnt know what a SSCCE was before:

    here is the code that I am using and that I am implementing on my other program:

     
    import org.eclipse.swt.*;
    import org.eclipse.swt.widgets.*;
    import org.eclipse.swt.awt.SWT_AWT;
    import org.eclipse.swt.browser.*;
    import org.eclipse.swt.layout.*;
     
    import javax.swing.*;
     
    import java.awt.*;
    import java.awt.Canvas;
    import java.awt.event.*;
    import java.awt.event.WindowEvent;
     
    public class Browser2 {
     
    	public static void main(String[] args) {
     
    		final Display display = new Display();
     
    	       JFrame frm = new JFrame("MyBrowser"); 
    	       Canvas embedded = new Canvas(); 
     
     
    	       frm.add(embedded, BorderLayout.CENTER);  
     
    	       frm.pack(); 
    	       frm.setBounds(0, 0, 800, 400);
     
     
    	       final Shell shell = SWT_AWT.new_Shell(display, embedded); 
    	       shell.setLayout(new FillLayout(SWT.VERTICAL)); 
     
    	      shell.pack();
     
    	       final Browser browser = installBrowser(shell, "http://javamagic.altervista.org/index.html"); 
    	       frm.addWindowListener(new WindowAdapter() 
    	       { 
    	           public void windowClosing(WindowEvent e) 
    	           { 
    	           e.getWindow().dispose(); 
     
    	           } 
    	       }); 
     
     
     
    		final BrowserFunction getLocation = new CustomFunction (browser, "sendInfo2Java");
     
     
    	       frm.setVisible(true); 
    	       while (frm.isDisplayable()) 
    	           if (!display.readAndDispatch()) 
    	               display.sleep(); 
     
    	}
     
     
    	static class CustomFunction extends BrowserFunction {
    		CustomFunction (Browser browser, String name) {
    			super (browser, name);
    		}
    		public Object function (Object[] arguments) {
     
     
    			System.out.println (arguments[0].toString());
     
    			Object returnValue = new Object[] {};
    			return returnValue;
    		}
    	}
     
     
    	public static Browser installBrowser(Shell parent, String homeURL) 
    	   { 
    	       Browser browser = new Browser(parent, SWT.EMBEDDED); 
    	       browser.setUrl(homeURL); 
    	       return browser; 
    	   } 
    }

    On the real project I have changed this main into a method and called using:

    	Runnable Map = new Runnable() {
    			  public void run() {
     
    				  BrowserMap.Init();
    			  }
    			};

    Some info:

    It is called AFTER all the other methods (DB and GUI).
    I basically put the Canvas into a JPanel and add the JPanel to the main frame.

    I am using swing for the GUI.

    And I am not using any layout manager, I am just setting statically their position on the frame (do you suggest me to use one?)
    I have tried this only on MacOSX, and I have found out that around 50% of the times the JPanel is not loaded.

    Extra: Whenever it is successfully shown, I have noticed that when I move around the map (from google.maps), the only way to refresh what is contained on the JPanel is to Resize the form, I have tried to repack the form, to revalidate the component etc... but nothing...
    Last edited by Nesh108; October 31st, 2011 at 03:55 PM.

  6. #6
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Canvas in JFrame, does not show content

    The problem on Windows does not seem to occur.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Canvas in JFrame, does not show content

    I don't have time right now to scour this article, but it looks like it might be beneficial to you: Eclipse Corner Article: Swing/SWT Integration

Similar Threads

  1. Show files name in JFrame
    By altisw5 in forum AWT / Java Swing
    Replies: 15
    Last Post: November 30th, 2011, 08:30 AM
  2. Beginner: Show Image in Label when Results Show Up
    By Big Bundy in forum Java Theory & Questions
    Replies: 3
    Last Post: April 4th, 2011, 02:43 PM
  3. How to get scores from user in Canvas J2ME?
    By elenora in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: April 4th, 2011, 04:10 AM
  4. JFrame declared as setAlwaysOnTop doesn't stay on top during slide show
    By ravindra_appikatla in forum AWT / Java Swing
    Replies: 1
    Last Post: March 30th, 2010, 09:08 AM
  5. make textbox in canvas
    By mahdi in forum Java ME (Mobile Edition)
    Replies: 2
    Last Post: October 6th, 2009, 07:10 AM