开发者

Parsing data back to mainPanel (java/swing)

开发者 https://www.devze.com 2023-04-13 08:13 出处:网络
Set up: I have a mainPanel with a tabbedPane on it, i have a separate JPanel \'extra\', extra creates an objects and i wish to pass that object back through to the mainPanel where i can actually use i

Set up: I have a mainPanel with a tabbedPane on it, i have a separate JPanel 'extra', extra creates an objects and i wish to pass that object back through to the mainPanel where i can actually use it/add it to the data structure.

Frame > Panel > TabbedPane > Panel (Separate Class, instantiated as new object)

I tried to add a listener in mainPanel that checks if a boolean in PanelExtra changes and then runs a method etc, but it didn't work.

I would make a method in mainPanel to accept the object but i don't know how to refer back to it. (getRootPane() didn't return anything)

Also im not sure if im using correct terminology, while i was taught Java in a command line Unix environment, Swing is very new to me.

I tried Listeners, Observers and am currently considering an object created at root and passed DOWN through all objects (As java passes by reference, i could pass information back as far as i want) If that isn't an entirely stupid idea, let me know and we can all go about our lives. If there is a better way to do it that'd be great.

(File overview)

[programApp]>[programView>tabbedPane]>[panel] Panel is created using new, and exists in a separate class. I want to pass data back from Panel to programView (projectNameView 开发者_运维问答as it is in netbeans)

I will attempt to add as much information as i can.


You can use the MVC-pattern for that.

Actually there are couple of variants depending on your preferences or exact task. For very simple case you can just use class with static fields to pass objects. Another way is to define your own listeners like:

private myPanel extends JPanel implements Notification {

... and where you create that panel:

myPanel.addNotificationReciever(mainPanel);

Later you just call fireNotification or whatever and implement similar listener for your mainPanel.


SirVaulterScoff gave the best answer in my opinion, but I thought I'd add some more (can't add comments yet, so I'm creating a new answer).

You should also read the Observer-Observable pattern (Wikipedia article), which is used along with the MVC pattern. It will be useful when linking your MVC classes together, to make sure everything is as loosely coupled as possible.

As a side comment: MVC is really a pattern you should focus on if you are to create applications with a user interface. It will save you many headaches, and will make your application easier to maintain and extend.

0

精彩评论

暂无评论...
验证码 换一张
取 消