Swing: perform action on main JFrame - java

Say I have various JFrames open in the same application. Is there a way to perform some action (like update a JTable) when the user changes the focus on one frame to another (like clicking the bar on the top)?
If not is there a way to perform an action on one JFrame when she closes another JFrame?

Please read: The Use of Multiple JFrames, Good/Bad Practice? to see why your program design could very well be improved
As for your main question,
Is there a way to perform some action (like update a JTable) when the user changes the focus on one frame to another (like clicking the bar on the top)?
It's really a specific example of a more general question:
How do I change the state of one object through an event that occurs in another object
and there are several possible solutions
Easiest would be to have the code that handles the event have a reference to the first object, here one of your JFrames, and simply call a method on it when the event is triggered.
Better is to structure your program with an MVC (Model-View-Controller) type structure, and in the event code (the control code), change the state of the model. View listeners to the model, including the JFrame you wish to change, would then be notified and would change their appearance based on the model.
Some general recommendations:
Having a bunch of windows displayed and swapped is very annoying to the user. Have a look at my link above to look for other possible options.
You'll probably want to avoid having class's extend JFrame, as that forces you to create JFrames with that code. Much better is coding to the JPanel, not the JFrame, and then placing the JPanels created wherever they are needed, be it within a JFrame, or in another JPanel, or swapped via a CardLayout, or in a JTabbedPane, a JDialog, a JOptionPane...

Related

Using the same button in multiple frames

I'm studying Java Swing, and I created some JFrame windows, in some of them, I created a button and they have the same solution (like search or save information), however, all the time I've had to create from the begining or use ctrl+c ctrl+v. Is there a way to create a JButton and use the same button in multiple frames?
And for JTextfield, there's a way to create only once and use for multiples frames?
There's an example using a unique button and showing a "Hello World"?
Swing components can only be contained in one container (JPanel, etc.). However, most Swing components are backed by models, and those models can easily be shared between multiple component instance. In case of JButton there are actually two models: a ButtonModel and an Action. You can use add ActionListeners to the ButtonModel and that should work. However, I've never used that, since I prefer to use Action for buttons.

Dealing with Swing Actions in several UI JPanels without a reference to the JPanels themselves

My first problem is: "how do I limit the amount of Swing Actions (using the AbstractAction as a base for other actions) when the same actions are used in conjunction with different UI components that are referenced with different Swing JPanel objects that (seem to have) no direct reference to eachother"?
My second problem is: "how to keep referenced between Actions and UI components when the actionPerformerd() method of the action uses TWO or more different UI component references"? I need ui context within the actionPerformed() method at different places that have no direct relation (well, at least that is what I think).
Let me explain my design concerns I am dealing with...
I have one JFrame which holds all UI logic. This JFrame holds three different references to JPanel logic: a ControlPanel, a MainPanel and a StatusPanel (all extend JPanel).
The ControlPanel sets up the JMenu and items, the JToolBar items and this JPanel is added to the contentpane with all the approriate Actions installed.
The MainPanel is where the actual communication with the user takes place. This JPanel holds a CardLayout with a bunch of JPanels inside it.
The StatusPanel is actually a statusbar dealing with all kinds of text messages coming from differnt JPanels, but processed by a StatusBarManager object. No fancy stuff here, just boring gui stuff.
Now the problem part: I first programmed a bunch of extended AbstractActions for each action I could think of (not so good idea). Next I programmed an ActionFactory that hands out an Action obj for a particulair task, like: ActionFactory.getAction("file.new");
This seems like a better approach because I now can instantiate a FileAction object for all "file" operations with one difference: the implementation of the actionPerformed() method. This "seems" a good idea because when I call in a different JPanel the same ActionFactory.getAction("file.new"); I get the same Action object returned that already was instantiated before (saves a lot of duplicate objects).
Well this might seem a good idea, I still struggle with the context of the JPanels. When I select a "card" to display within the MainPanel using the JMenu and JMenuItems, I am in the ControlPanel ui context only and in the actionPerformed() method I do not have the MainPanel context to set the "card" using the CardLayout of that JPanel.
The same goes for when I am in the MainPanel (pressing a button) and have no context of a StatusPanel.
I have looked at these previous suggestions on stackoveflow, but somehow I can't make the connection. I am missing the clue...
Sort of same Q&A over Actions at SO:
here, here, here, here, here, here, here, here, and here .
Some suggested to use a framework for this like the appframework or eclipse rcp or the spring rcp, or others. BUT I prefer to not use any frameworks at all for several reasons other than what is available in the default JVM 6.
I hope someone can shine his/her light over this matter that helps me find a good solution for my problem. Thanks for your help in advance!
I think the main flaw in the design you describe is that the actions work directly against the UI, instead of against a model/controller.
If your action just updates a model, and each view part keeps itself in sync with the relevant part of the model, you avoid those dependencies between one action and several other view components.
This also allows to share the common logic. You move that logic from the action to the model/controller, and the action becomes basically a one-liner (calling that logic).

Changing into new window

What is an efficient method to have a window change into a different window? When the user presses the next button, I would want it to perform another method that would create this second window. What is the appropriate listener class for this scenario?
An example scenario for your question:
You may have a JFrame which is the starting point of your application, i.e. having single instance, main method, general initialization of components etc. You say you want to change windows. Let these windows be different JPanel objects that each of them are assigned to operate on different tasks. You can add these panels to your main frame. And changing these panels upon certain conditions will make your application capable of navigating between these panels/windows as well. So how to make this happen? Take a look at CardLayout and use it to navigate between your predefined panels on their container frame.
What is the appropriate listener class for this scenario?
Take a look at this post, I have demonstrated CardLayout usage via ActionListener.
What is the appropriate listener class for this scenario?
An ActionListener. See the links already provided in comments, for how to use one.

JPanels, Listeners and Threads

For a school project I'm writing a BlackJack card game in JAVA.
After a few steps within the program, my whole computer system slows down/stutters and some areas within the JFrame turn black. No repainting seems te be happening.
I will give you some program details in words rather then submitting the code.
I have done some extensive extending of almost every GUI component to give it the look and feel that I want. Almost every child of JComponent that I use has got its paintComponent rewriten with custom image backgrounds and anti-aliasing where applicable.
In my (custom) JFrame I load an extended version of JPanel. This panel holds all the GUI components for that screen. When I want to switch screen (go to a different part of the program), I switch to another extended version of JPanel. I do this by removing all elements from the JFrame and add the new panel. These different panels implements ActionListeners and FocusListeners.
At this point my thoughts are leaning towards a thread handling issue. My theory for the moment is this: When a panel is constructed for display in the JFrame (each on different stages within te program), the previous constructed panels aren't realy 'destroyed', and the Listeners of those panels remain active.
My question for you people is, wether this theory holds ground... And in that case, is there a way to 'kill' these panels or at least stop the listening?
If my theory doesn't make sense, what else could be causing these symptoms? Could it be the extensive overwriting of the paintComponent methods?
Any ideas would be highly appriciated!
Regards Wilco
When a panel is constructed for display in the JFrame (each on different stages within te program), the previous constructed panels aren't realy 'destroyed', and the Listeners of those panels remain active.
No. Events are only dispatched to the component that has focus. It a comonents doesn't have focus then it won't received system generated events like KeyEvents and MouseEvents. So if a panel isn't visible then it won't receive events.
I switch to another extended version of JPanel. I do this by removing all elements from the JFrame and add the new panel.
This is not the best design. It is better to use a Card Layout which was designed for this purpose.
Almost every child of JComponent that I use has got its paintComponent rewriten with custom image backgrounds and anti-aliasing where applicable
Then you probably have problems with your custom painting. What happens when you just use the standard components without custom painting?

Control of JLabel

I have built, with a GUI builder, a set of JLabels and 4 arrows in a JFrame. I want, when I press one of the arrows to be able to perform operations on the correspondent label. I.e when the control is on the first label the "right" arrow would "bring" the control on the right label. I also want to mention that due to GUI builder I can't(??) use array and increase/decrease the pointers. Any ideas?:)
It sounds like you are trying to pair your GUI too closely to your data. When someone clicks on a button, it should perform some action on your data. Once that action is complete, the GUI should be updated to reflect the new data. This is much easier than moving controls within the window. This is known as the Model View Controller pattern.
Read the section from the Swing tutorial on How to Use Key Bindings. In general, this allows you to define an Action that is executed when a KeyStroke is invoked. So you can have a different Action for each of the right/left/up/down arrow keys.

Categories