I want to create a java pane with a canvas on the left and column of other components on the right. I did this long ago but lost that code. Can anyone point me in the right direction to do this?

Recommended Answers

All 6 Replies

You have options here:
Populate the JPane with two JPanes left and right (use the default FlowLayout) then populate the left pane with the canvas and right pane with a vertical stack of components (use a BoxLayout or a GridLayout).
or
GridBagLayout will do it all. Steep learning curve but this is the one to use if you're really fussy about getting complex layouts just right, especially when they're resized.

commented: Many thanks for the response. I will try GridBagLayout. +0

Good choice! Any problems come back here and I'll help.
For starters I highly recommend Oracle's tutorial

commented: Thanks again. I will check it out. I need all the help I can get. +0

Thanks to JamesCherrill for the suggestion to use GridBagLayout. I've done that and I now have a canvas, a combo box, two buttons and a text field positioned where I want them. But, when I try at run time to use the canvas by getting a graphics context, I get an error that canvas isn't defined. How can I make the components inside the GridBagLayout visible? I tried making canvas public in GridBagLayout but that generated an error also.

Without seeing the code there's no way to know.
Why are you trying to get the graphics context? The usual way to draw stuff in Java is to subclass JPanel and override it's paintComponent to draw whatever is needed. Anything else can get very messy because you will be fighting Swing's double buffered smart painting, rather than working with it. Have a look at https://www.oracle.com/java/technologies/painting.html

commented: True, and to do that you need a graphics context. Trying to do that is when I get the error. Is there a way to post the code? +0

Forget it, I found it easier to do in HTML/Javascript and easier to distribute the final product.

All depends on what you need it for...
A web application needs a server to run on, lots more infrastructure than a standalone desktop application.
Of course if you're just interested in a single static page, then yeah, it might be easier to make a rough outline in html and just ship the html file. But that's not what a complete product would look like.

commented: I am interested in single page apps and HTML/Javascript seem better suited for this than Java. Not everyone has Java on their computer,.. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.