开发者

Issue updating a parent JFrame

First, let it be known that I'm new to java and it's quirks. I'm a seasoned programmer with various languages, which may be why I'm stuck...

I have an application that, possibly due to poor design, spawns new JFrames through the users' work-flow. My question is, if there is an event in a spawned JFrame, is it able to contact and pass data or an event to it's parent?

I have read that using a JDialog s开发者_如何转开发eems to be the way to design, but let's assume that's not an option. Essentially, JFrame1 contains a JTable with a list of data. An action spawns JFrame2 and a user "does something" that impacts the data in the list in JFrame1. Upon closing JFrame2, is there a way to control the JTable based on JFrame2's close event?

It's a pretty basic concept, I just can't seem to find the mechanism that would allow such an action.

Thanks!


You can use "listeners" to listen for various events.

It sounds like you might want to start with How to Write a Window Listener.

I have read that using a JDialog seems to be the way to design, but let's assume that's not an option.

Why? The code is the same and JDialogs where designed for this purpose. What is the specific requirement that says you need to use a JFrame?

An action spawns JFrame2 and a user "does something" that impacts the data in the list in JFrame1. Upon closing JFrame2, is there a way to control the JTable based on JFrame2's close event?

This is a common design. The user selects a row to change or update and a model dialog is created to display all the data so it can be changed. When the dialog is saved the data in the table is updated. If this is your requirement, then you can just pass in the TableModel to the dialog. Then when the dialog is closed you update the TableModel and the table will be repainted automatically.


You would have to capture the window closing event using a window listener. The window listener would also need a reference to the data that needs to be changed.


In addition to using Window.addWindowListener() on either a JFrame or a JDialog, consider using a model-view approach. Have the close event modify the table's data, rather than the table itself. Use AbstractTableModel as the model for the table, and listen for changes to the data.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜