aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Putting a JPanel from one class into a JFrame of a different class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Putting a JPanel from one class into a JFrame of a different class?" Watch "Putting a JPanel from one class into a JFrame of a different class?" New topic
Author

Putting a JPanel from one class into a JFrame of a different class?

John Jericho
Greenhorn

Joined: Apr 11, 2011
Posts: 22
Title explains it all really, but I was wondering if there was a way to actually do that? Alot of people suggested CardLayout to me, but it seems to be different to what I want and I read its heard to mix it with Menu (although I'm not sure myself). I need to solve this one thing and my program will be almost good to go. Thanks.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 563
It can be as simple as:

myFrame.add( new MyPanel() );

which adds a JPanel from another class, MyPanel, to another JFrame, myFrame.

But I suspect there's more to the story that is complicating the answer.

I recall your earlier question, and I remember someone suggesting using CardLayout, but I don't remember the details. If you're just asking the same question because you didn't like the last answer you got, you're wasting everyone's time. It would be more productive if you would continue the conversation in the previous thread to understand why or why not that might not be the best answer for your particular situation. That way, others reading will also learn something.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40064
    
  28
Greg Brannon, I think you have a point there about continuing the discussion on existing threads. Since we don't like duplicate threads, I shall close this thread.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40064
    
  28
I am so sorry; as you pointed out, you didn't ask about CardLayout yourself. We are obviously confusing you with somebody else who was told to use CardLayout. So sorry .

Am reopening this thread.

CR
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 563
Thanks, Campbell Ritchie.

I apologize for confusing you with another poster/topic. I'll be more thorough and check my memory next time.

I do suggest you provide more detail on what you're trying to do. As I stated above, the answer could be quite simple, but I suspect there's more to your question that would be helpful to those trying to answer your question.
John Jericho
Greenhorn

Joined: Apr 11, 2011
Posts: 22
Absolutely no problem, you guys must get a lot of these kind of questions

Essentially I'm making a Japanese Kanji game, the aim is to click on the correct polygons which are fashioned like "strokes" in the correct order. Each time a user clicks on a polygon a designated number (stroke number so to speak) is sent to an arrayList. The elements of this arrayList is compared with elements in an array that are the correct number order of strokes for that Kanji. That was all working fine, however my idea was that for the user to start playing they would need to select "New Session" from a JMenu (all they would see is a blank JFrame when starting the program) and that that would bring up the panel with the Kanji in the same JFrame. The ultimate aim of doing this is to create multiple JPanels that have different "Kanji" on them depending on what the user selects, I was just using the New Session Menu option as a test.
To make things easier I will post my code:

Main:



Class that has the Frame:



Class that hold the Kanji



One problem I faced was having to make the Kanji class extend JFrame, if I didn't then I would get an error for repaint(). As it is now, instead of putting a JPanel inside the Frame once clicked, it brings up a whole different frame (not sized) which has the JPanel/Kanji inside it.
John Jericho
Greenhorn

Joined: Apr 11, 2011
Posts: 22
Ok, so my guess is just to go with CardLayout then..
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 563
Don't give up. Be patient.

So what you're really asking is how to put a JFrame in a JFrame. I don't think CardLayout allows you to do that. Nothing does that I know of.

I think the solution is to fix your original design by either swapping JPanels using CardLayout - seems simple enough - or fixing the problem you had with repaint() when Kanji extended JPanel. What was that error?
John Jericho
Greenhorn

Joined: Apr 11, 2011
Posts: 22
The thing is I know that putting a Frame onto another Frame may not be possible, so thats why I was the KanjiRoku class to act as a Panel for the Frame that is in KanjiInterface. If I take off extends JFrame from KanjiRoku the error for repaint() says "The method repaint() is undefined for the type KanjiRoku.MouseCatcher", if I replace extends JFrame with JPanel then the error goes away but nothing at all shows up when New Session is clicked.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

That's right, you can't put one frame into another. So just use a JFrame for the main window which contains everything else, and use JPanel for components which you want to include in the main window.

I don't know exactly what you mean about repaint() -- because you didn't say anything about your problems with it. But I did notice that you were calling it from within a class which wasn't a GUI component and which didn't have a repaint() method, so maybe that was the problem.
John Jericho
Greenhorn

Joined: Apr 11, 2011
Posts: 22
I want to add the JPanel from a seperate class (KanjiRoku), I want it to pop up in the class with the Frame (KanjiInterface) when the user selects New Session from the Frame menu.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40064
    
  28
I think this thread would fit better in our GUIs forum, so I shall move it.

Why are you making a Frame implement ActionListener? That is usually poor, non-object-oriented design.
John Jericho
Greenhorn

Joined: Apr 11, 2011
Posts: 22
For the JMenu, the WindowListener is being used for when the user selects "New Session" which is what triggers the creation of an object (roku) which contains the Panel I want to insert
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40064
    
  28
It's perfectly all right to have a top-level container implement something like WindowListener, because that listens to the state of that very same container. But I don't think it is any good having it implement ActionListener, as your second class does.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Putting a JPanel from one class into a JFrame of a different class?