Is it possible to open JFileChooser over Canvas? - java

Is there any alternative way to open a JFileChooser in which a JFrame only contains a Canvas?

I don't think so.
Taking a look at the documentation of Canvas, seem the only component one can add is a PopMenu

not possible without dirty hacks, I'd suggest to use JPanel instead of Canvas,
lightweight Swing JComponents are behind AWT Components,
for painting to Swing JComponents to use paintComponent instead of paint
I'd suggest don't mixing AWT Components (Canvas) with Swing JComponents (JFrame or JFileChooser)

Related

Painting on a JButton in java

I am trying to create a mancala game board. For the purpose of the game, is there any way I can create a JButton and then just paint on it? I don't want to use setIcon property
You need to create a custom class which extends from JButton and then override it's paintComponent method
See Performing Custom Painting, Painting in AWT and Swing and 2D Graphics for more details
Remember though, a button typically has a lot going on, borders, content etc, this makes performing custom painting on them a little more tricky..

How to use Postprocessing on an awt or swing window

Is it possible to use Post Processing effects like gaussian blur or greyscale effects on an entire JFrame independent from its contents?
I would guess you are looking at the JLayer class. See How to Decorate Components with the JLayer Class for more information and examples.
Another option might be to use a Glass Pane. See the glass pane demo from How to Use Root Panes.

Is it appropriate to extend JPanel to create a drawing surface for a 2D game?

I'm converting a 2D game from an Applet to a JFrame. I know I should add a JPanel to a JFrame and use the JPanel as my drawing surface but I'm not sure exactly what I should be subclassing. I was trying it with JFrame but I've read that it's usually unnecessary and a bad idea to extend JFrame (I wasn't adding any extra functionality anyway, so that makes sense). I need to override the paintComponent() method of JPanel so right now I'm leaning towards extending JPanel. Is there a better way of creating a 2D game surface or is extending JPanel and adding that to a JFrame an appropriate way to do it?
No, there is no better way, that's a very good option.
If you would extend a JFrame (the other option), your animation would blink on most windows family OS.
JPanel is a pure swing component and reacts better in java than a peered component like a JFrame.
Your choice is good, go ahead and have fun programming.
update
My knowledge in this area has grown since I originally answered the question and I agree with #Snicolas that you should use a JPanel.
JPanel (because it's a swing component) has builtin support for double buffering (AWT does not) and has better performance (so I hear, but I have no first hand experience with this).
You may find this article useful: http://www.oracle.com/technetwork/java/painting-140037.html#swing
original answer:
I recommend you subclass Canvas or Component or JComponent. My preference is Canvas. You should read the 2d graphics tutorial.
update
Use Canvas because it receives all the user input events and doesn't have the baggage of being a container. read more. Panels don't receive all mouse input events, for instance.

Lightweight on top of heavyweight

I have a JFrame that has a JLayeredPane. The JLayeredPane contains a heavyweight component (Ardor3d AWT canvas). I am trying to display a JPanel on top of the heavyweight component. This works perfectly until I set the size of the frame to the size of my monitor. The heavyweight seems to draw over the Jpanel that I want on top.
Any ideas?
This might help you: http://java.sun.com/developer/technicalArticles/GUI/mixing_components/
Yes, this is why you should not mix heavyweight and lightweight components. Extend JComponent and override paintComponent() instead of using a canvas.
You mention Ardor3d, I assume that is some sort of 3rd party component. If there is not a Swing version I would suggest finding something else to meet your need.

How to automatically resize an AWT component after changing it's contents?

I have an AWT Label inside a Panel with FlowLayout. I want to dynamically change the Labels text and resize it to the required width.
I have only found answers to the Swing version of this problem (setPrototypeDisplayValue()), but I have to stick with AWT since this is a homework.
You should be able to call invalidate(), which will then tell the parent container (your Panel) to redraw itself.
http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Container.html#invalidate()

Categories