Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to display components from another class in a class that extends JPanel ?  RSS feed

 
Gustavo Siqueira
Ranch Hand
Posts: 97
C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order, to a good concept of OO, I though in display contents of the class ''OpenFile" once called by the class "Initial" with "ActionListener" to open a file. Thus, "OpenFile" implements 'ActionListener' to open the file called by the class "Initial" that contains JFrame. Now, to display the contents of the file open by the user, I want display it in separate class. So, I created the class "ShowDocument" that extends JPanel to display the file open in the class "OpenFile". However, not is working creating a instance of 'ShowDocument'.

Thanks you in advance!
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's not creating an instance of ShowDocument, what is it doing precisely? I confess I didn't follow your description of the different classes either (who knows/does what). Can you post a code example describing your problem?
 
Gustavo Siqueira
Ranch Hand
Posts: 97
C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure: See an example:

The "Initial" class, add the "DisplayDocument" to your JFrame, once "Initial" extends JFrame. Therefore we will add:


In this same class, I have a button that will request to user to open file:



Thus, the "OpenFile" class, does all your functions to open the file required by the user:



Now, to have a good concept of OO, I though in display the file in the "DisplayDocument" class:



Thereby, I tryed created a instance from "DisplayDocument" or extends the "DisplayDocument" class to add components to it. However, not it worked!
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the example. That makes more sense. Still not sure what you mean though when you say it doesn't work. Does the code not compile, or does it throw an exception when it is run?

It seems to me like OpenFile needs to know about DisplayDocument so it can tell it what to display, or the Initial frame needs to act as a go-between between OpenFile and DisplayDocument. I assume something like that's in your code but not included in your example here.
 
Gustavo Siqueira
Ranch Hand
Posts: 97
C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry. Do you can me give an example to fix it the problem ?
O code is compiling, however, is not displayed the componentes within of classe "DisplayDocument".
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say DisplayDocument looks like this:


and frame has a reference to an instance of that DisplayDocument.

and you created your OpenFile, and it knows about your DisplayDocument.

then the OpenFile handler (actionPerformed) can say something like:


It requires that the OpenFile and DisplayDocument be able to communicate in some way. My method is one of the two I described earlier, and I notice that communication doesn't seem to be something that you're doing right now.
Also, note that when I created the DisplayDocument class, I added the text field to the panel and gave it some default text. This allows me to take the OpenFile side out of the picture so I can be sure that I've added the text box to the DisplayDocument correctly before I start worrying about class interaction.

Without knowing exactly what the problem is though, I have no idea whether this will be helpful. What are you expecting to see, and what are you actually seeing?
 
Gustavo Siqueira
Ranch Hand
Posts: 97
C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you not understood my issue ?
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that you're trying to display some component in a panel (or subclass thereof) and it isn't displaying. I'm not 100% clear what isn't displaying, whether it's your data or the component of the panel itself. You're talking about an OpenFile listener, but it sounds more like you would still be having your problem if the OpenFile class wasn't part of the picture; that it's the GUI that's not working. I think your complaint is with the DisplayDocument class, and you need that displaying correctly before you should be thinking about testing it with data (no sense trying to debug two problems at once).

The panel should have all components added to it when it is first initialized (in the constructor). I see in your example you have an empty DisplayDocument class declaration, and I assume that there's actual work being done in your real code. The DisplayDocument you have in your example adds no extra functionality; it is basically a fancy name for a JPanel. What is actually in that class? What components are expected to display in the DisplayDocument, and which ones aren't displaying? Please tie what you're expecting to see down to an actual line of code so we can spot the problem.

And if I'm not accurate about the problem being with the DisplayDocument class, if you could expand on where it is instead.
 
Gustavo Siqueira
Ranch Hand
Posts: 97
C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. We will return again. I have three class named: "Initial", "DisplayDocument" and "OpenFile"

Initial Class - That class have all functions to display a frame and menus to call another class. In other words, she extends JFrame.

DisplayDocument Class - That class is accountable for display the contents that the user opened in menu that contains in the "Initial" class, call "OpenFile".

OpenFile Class - That class implements ActionListener to listen the button "Open File" called by the user.

The issue is: I want display the contents of the file in the "DisplayDocument" class, just that.

In class "Initial" I already add the class DisplayDocuments. Therefore, when I run the program, that class already called.

Do you is with problem to understand my issue ?
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your "issue" is that you believe a class is a thing to be called once and only once. Whereas in Java you don't call classes at all. Generally you create an object of a class, and then you call methods of the class on that object. As many times as necessary. Tina gave you some good examples of that concept, and I think you should have another look at them.
 
Gustavo Siqueira
Ranch Hand
Posts: 97
C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem was solved! Thanks you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!