Loading...
Logo
Processing Forum
Hi,

I'm trying to embed a PApplet in a JScrollPane within a JFrame, so that I can properly work on sketches that are larger than my screen. I tried embedding the PApplet in a fixed size JPane, but I can't get that to render properly, and when I simply display the PApplet within the JScrollPane, the PApplet ignores whatever size I set it at and instead assumes whatever size the JFrame is.

Has anyone else tried to do anything like this successfully? I'm open to any ideas, I'm just trying to create some type of scrolling capability.

controlP5 might be an option, but I'm working in a larger framework created in Java and I'm trying to maintain a consistent look and feel.

Replies(10)

I fixed it. The problem was my own stupid error. I had accidentally overwritten the setSize() function of the PApplet. Everything should work as expected if you don't do something stupid like that.

___________
http://justinlivi.net
Ok so I don't know if this has been written about anywhere else, (or if anyone cares) but I found the best way to do this is to create a window as follows:

Embed your Processing Sketch in a JInternalFrame
Set JInternalFrame resizable to false
turn off JInternalFrame UI with this bit of code:
Copy code
  1. BasicInternalFrameUI ui = (BasicInternalFrameUI)jinternalframe.getUI();
  2. jinternalframe.putClientProperty("titlePane", ui.getNorthPane());
  3. jinternalframe.putClientProperty("border", jinternalframe.getBorder());
  4. ui.setNorthPane(null);
(obviously in the above code, jinternalframe refers to your JInternalFrame and should be replaced with whatever name you have for it)

Embed that JInternalFrame within a JPanel
Create a JFrame
Within that JFrame embed a JScrollpane
Add your JPanel to the JScrollpane

This will create a fixed size sketch within a resizable window, allowing you to scroll to see the full sketch.
If anyone's interested further, I can post a well-commented example/tutorial thing.


___________
http://justinlivi.net
Actually, yes, I'd like to see your code.

Why are you using a JInternalFrame rather than a JPanel?  (I'm a Swing n00b, so take the question with a grain of salt).

I'm trying something similar, although I am working on a more elaborate Swing interface, with a scrollable panel in the interface.  I'm extending JPanel and adding a p5 sketch, like in Irags Kite:   http://wiki.processing.org/w/Swing_JSliders

My problem is I can't get the p5 sketch to sit behind the viewport.  The scrollbars work fine, but the z-index is all screwy.  Any ideas?
Actually the approach you're describing is the first one I tried, but I had the same error as you.

From what I can tell, JPanel exhibits some bizarre behavior when you embed an Applet inside it. I don't think it was ever designed to handle an embedded Applet. My next thought was JFrame, but since you cannot embed a JFrame within a JPanel, I decided to go with JInternalFrame.

I'll post my code as soon as I clean it up a bit.

Oh, one other note is that I switched from JScrollPane to ScrollPane because there were again some bizarre rendering issues and I didn't feel like exploring the reasons why. It could probably be made to work though with a bit of hacking.

___________
http://justinlivi.net
looks like we're not alone:

According to phi.lho, it's something about the PApplet being a heavyweight component, while JPanel only takes lightweight.
Ok here it is. I put it all in one function to keep it condensed.
Copy code
  1.     /*
  2.      * sketchw = width of the sketch
  3.      * sketchh = height of the sketch
  4.      * max_w = max width to create window
  5.      * max_h = max height to create window
  6.      * name = name of sketch
  7.      *
  8.      */
  9.     void create(int sketchw, int sketchh, int max_w, int max_h, String name) {
  10.         Dimension dimension = new Dimension(sketchw, sketchh); //will act as size of sketch
  11.         JFrame sketchWindow = new JFrame(name); //creates the main frame
  12.         JPanel sketchPanel = new JPanel(true); //holds JInternalFrame
  13.         JInternalFrame sketch = new JInternalFrame(); //holds the actual sketch
  14.        
  15.         //sketchPanel size is the larger of either sketchWindow or canvas
  16.         sketchPanel.setLayout(new BoxLayout(sketchPanel, BoxLayout.PAGE_AXIS)); //centers sketch - optional
  17.        
  18.         //determines size of sketchWindow
  19.         int width, height;
  20.         if (sketchw <= max_w)
  21.             width = sketchw;
  22.         else
  23.             width = max_w;
  24.         if (sketchh <= max_h)
  25.             height = sketchh;
  26.         else
  27.             height = max_h;
  28.        
  29.         //creates PApplet
  30.            Canvas canvas = new Canvas(); //Canvas is just a sketch which extends PApplet
  31.            canvas.setSize(dimension);
  32.            canvas.setMaximumSize(dimension);
  33.           
  34.            /*following five lines provided by Laurent Sauvage
  35.             *http://www.koders.com/java/fid0EF14FD482B41C1FBD92062767CBB1457E004E63.aspx
  36.             *Copyright (c) 2006, Laurent Sauvage
  37.             *All rights reserved.
  38.             */
  39.            BasicInternalFrameUI ui = (BasicInternalFrameUI)sketch.getUI();
  40.            sketch.putClientProperty("titlePane", ui.getNorthPane());
  41.         sketch.putClientProperty("border", sketch.getBorder());
  42.         ui.setNorthPane(null);
  43.         sketch.setBorder(null);
  44.        
  45.         //just basic options to keep the sketch the right size
  46.         sketch.add(canvas); //adds canvas to JInternalFrame
  47.         sketch.setSize(sketchw, sketchh);
  48.            sketch.setPreferredSize(dimension);
  49.            sketch.setMaximumSize(dimension);
  50.            sketch.setResizable(false);
  51.            sketch.setVisible(true);
  52.            sketch.pack();
  53.           
  54.            //centers the JInternalFrame using the BoxLayout
  55.            sketchPanel.add(Box.createVerticalGlue());
  56.            sketchPanel.add(Box.createHorizontalGlue());
  57.            sketchPanel.add(sketch); //adds JInternalFrame to JPanel
  58.            sketchPanel.add(Box.createHorizontalGlue());
  59.            sketchPanel.add(Box.createVerticalGlue());
  60.           
  61.            //uses ScrollPane instead of JScrollPane to circumvent rendering issues
  62.         ScrollPane scrollpane = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS); //follows Adobe's lead
  63.         scrollpane.add(sketchPanel); //adds JPanel to ScrollPane
  64.         sketchWindow.add(scrollpane);
  65.        
  66.         //there's probably a better algorithm for this, I'm just not sure what it is
  67.         //ensures window is set at proper size
  68.         sketchWindow.setPreferredSize(new Dimension(width+scrollpane.getVScrollbarWidth()+18,
  69.                                                     height+scrollpane.getHScrollbarHeight()+40));
  70.            sketchWindow.pack();
  71.           
  72.            //start sketch
  73.            canvas.init();
  74.            while (canvas.defaultSize&&!canvas.finished)
  75.             try {Thread.sleep(5);} catch (Exception e) {}
  76.            sketchWindow.setVisible(true);
  77.     } // create

___________
http://justinlivi.net
ok, I think I get it... you're stripping a JInternalFrame and treating as a JPanel?  I can't run your code obviously, but are you having good results?  Does the JInternalFrame occlude other lightweight components, or does it play better?  Do you have any screenshots?

it's interesting, certainly.  In the meantime, I decided to just avoid scrollbars entirely, unless I make them in P5.  But your experiment is good food for thought... and later on I might reconisder.
Yeah that's pretty much what I'm doing. And yeah it seems pretty solid and lets me have multiple scalable sketches open at the same time.

I would think you'd be able to run it if you changed Canvas to whatever your sketch is named. You just have to put this in a separate class written in Java. (I'm working in Eclipse... I don't think I made that clear, sorry.)

___________
http://justinlivi.net
All my lightweight components are in other heavyweight containers so I haven't tested that... you're not trying to embed swing components within Processing are you? Cause that's a recipe for a headache. I don't think it can be done successfully.

Here are some screen shots of the various states as well as how it interacts with my (mostly) placeholder menu bar.





___________
http://justinlivi.net
very cool.  I like the modularity of your approach

I have been having reasonably good luck making my scrollbars in p5 itself, but you've inspired me to give it another shot.