Using the same button in multiple frames - java

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.

Related

Swing: perform action on main JFrame

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...

The proper way to change content in a Java GUI application (not creating a new window each time)?

I need to make a Java desktop application for a client and the last time I did Java it was 2 years ago and a little odd.
My main query is regarding navigation between GUI.
In the past, I would just create a new JForm (JFrame maybe?) whenever a button was pressed and a new GUI form/window would open up.
This time, I'd like the GUI to be inside one JForm/JFrame with just the inner content changing, how most applications look when you press a button.
I assume this is done by putting all of my GUI elements in JPanels, and deleting/creating them when buttons are pressed on the same JForm?
If not how do I do it properly?
I'll also be using Netbeans GUI editor, if anyone has a better alternative for a Java GUI builder/IDE, let me know.
Thanks!
The simplest approach would be to use a CardLayout
This will allow you to add multiple components to the UI and control which one is actively visible
Another approach could be to create menus and menuItem and having multiple JFrames now you can get the same JFrame to be displayed whenever you click the same menuItme button. This way You can minimize the creation of JFrames.

Java: single frame vs multiple frames

Think about the classic installation process, where you have a "next" button and when you click it the content of the window changes. To represent this situation I thought of two possible solutions:
-when "next" is clicked destroy the current JFrame and create a new JFrame, maybe passing to his constructor useful information (e.g. actual window size, content inserted by the user in the current frame, ...)
-when "next" is clicked remove all the components from the current JFrame and add new components as needed
The first solution looks way better about OOprogramming, because I can keep separate classes for different frames and I can avoid huge methods that empty the frame and repopulate it. However the first solution sounds a bit "dirty" and I should pass lots of parameters to the new frame. To represent this situation I would choose the second solution.
Now think about a menu with an "option" component: in this situation I would create a new JFrame when "option" is clicked, so that I can populate it with option items. Is this a correct solution? Is there a way I can always know which one is the best solution? Are there any solutions I didn't think about?
Destroying the main JFrame would be silly -- not to mention jarring for the user. Just use a single JFrame and change its contents.
To implement an installer wizard, use a single JFrame containing one large JPanel on top and a smaller one containing the "Next", "Back", "Cancel" buttons along the bottom. When the Next or Back buttons are pressed, you replace the large JPanel. You can have many different JPanel subclasses, one for each "page" of the wizard.
There's a LayoutManager called CardLayout which is ideal for implementing this scenario -- it manages a "stack" of components, and only shows one of those components at a time. Use a BorderLayout in the JFrame. Into the center position put a JPanel with a CardLayout. Then add the individual pages of the wizard to that JPanel, so the CardLayout can manage them.
The CardLayout is well suited for this. You just swapout the JPanel contents when the "Next" button is pressed.

What is this component name

What is the name of the component in java Swing shown in the following link
http://www.scriptocean.com/template3.html
It is known as extended ListView in Android. But I want to know the same in Java Swing.
Do you mean this component ?
If so, to display it in Java, you have some choices.
If you want your items to be easily clickable (that's to say action senders), you would tend to use JButtons in a vertical BoxLayout 'ed JPanel
If you simply want to display items, then customize their display, you would undoubtly go the JList way. Also take a look at Swing tutorial, which is always of great help.
EDIT
Accordint o comment, to have an area below the button displaying content, you'll use the second solution with a twist. As all elements in Swing are in fact JComponents and can be put in thers, you'll use JPanel as JList elements. in each JPanel, you'll have ione button always visible and one sub-panel that is hidden at startup. When clicking the JButton, you'll simply show or hide the associated sub-panel. If you want to have some kind of effect, you can either
wait for the upcoming JavaFX transitions effects
Use Filthy Rcih Clients animations library (take a look at their links page).
There is no standard Swing component that behaves like in your example. But you can find something similar in the SwingX project : the JXTaskPane and JXTaskPaneContainer components.
Unlike your example, the sections are not exclusive. But you can achieve this exclusivity with a few lines of code.

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