PDA

View Full Version : Any Java Swing programmers here? (help needed!)



vegeta1215
05-02-2005, 10:35 PM
I'm working on a project for my Java class that involves making a program with swing components that lets you draw shapes onto a JPanel. I'm pretty much done, but there is one thing I'm trying to do for the project than I have not been able to figure out how to do.

Basically the app is made of a JFrame with a JMenuBar and a JPanel (which I added to the JFrame). What I'm trying to do is enable/disable menu items items when a certain action happens in JPanel (I extend JPanel and have my own class). I've tried using the getParent method in the JPanel, but I don't think it gives me a reference to the JFrame.

Anyone able to help me out?

Saffith
05-02-2005, 10:57 PM
Haven't tried it myself, but maybe getTopLevelAncestor (http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JComponent.html#getTopLevelAncestor()) instead of getParent? Sounds like more what you're looking for.

vegeta1215
05-03-2005, 12:29 AM
I managed to get it working - I had to chain getParent() together like four times to get a reference to my DrawingFrame cause the parent of my DrawingPanel was JPanel, whose parent was JLayeredPane, whose parent was JRootPane, whose parent was (finally!) DrawingFrame.

I like your's better though Saffith, it's much cleaner to read and understand, plus it works! Thanks a lot! :D

AlexMax
05-03-2005, 07:01 AM
This is why I havn't screwed around with GUI's in Java yet.

vegeta1215
05-03-2005, 10:20 AM
It's not too bad. We had to learn to write the GUI stuff ourselves, where in a real development environment you'd use an IDE or something to make the GUI, and just code in the the event handlers, etc.

theplustwo
05-03-2005, 11:38 AM
Yeah, I had a semester of GUI writing in Java both with and without Swing... it makes you appreciate the Windows Form designer. :)